Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating dictionary properties for csharp models #1791

Closed
morinow opened this issue Aug 23, 2022 · 5 comments
Closed

Generating dictionary properties for csharp models #1791

morinow opened this issue Aug 23, 2022 · 5 comments
Assignees
Labels
duplicate This issue or pull request already exists

Comments

@morinow
Copy link

morinow commented Aug 23, 2022

It seems to me that it's not possible to generate model properties of type dictionary for csharp.
I have the following schema, which includes an object containing a property of type dictionary<string, string> called 'labels':

"components":{
      "schemas":{
         "Resource":{
            "type":"object",
            "additionalProperties":false,
            "properties":{
               "labels":{
                  "type":"object",
                  "description":"Arbitrary resource labels.",
                  "nullable":true,
                  "additionalProperties":{
                     "type":"string"
                  }
               }
            }
         }

which generates the following model with an extra class for the 'labels' property instead of using a dictionary type:

public class Resource_labels : IParsable {
        /// <summary>
        /// Creates a new instance of the appropriate class based on discriminator value
        /// <param name="parseNode">The parse node to use to read the discriminator value and create the object</param>
        /// </summary>
        public static Resource_labels CreateFromDiscriminatorValue(IParseNode parseNode) {
            _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
            return new Resource_labels();
        }
        /// <summary>
        /// The deserialization information for the current model
        /// </summary>
        public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() {
            return new Dictionary<string, Action<IParseNode>> {
            };
        }
        /// <summary>
        /// Serializes information the current object
        /// <param name="writer">Serialization writer to use to serialize this model</param>
        /// </summary>
        public void Serialize(ISerializationWriter writer) {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));
        }
    }

public class Resource : IParsable {
        /// <summary>Arbitrary resource labels.</summary>
        public Resource_labels Labels { get; set; }
        /// <summary>
        /// Creates a new instance of the appropriate class based on discriminator value
        /// <param name="parseNode">The parse node to use to read the discriminator value and create the object</param>
        /// </summary>
        public static Resource CreateFromDiscriminatorValue(IParseNode parseNode) {
            _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
            return new Resource();
        }
        /// <summary>
        /// The deserialization information for the current model
        /// </summary>
        public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() {
            return new Dictionary<string, Action<IParseNode>> {
                {"labels", n => { Labels = n.GetObjectValue<Resource_labels>(Resource_labels.CreateFromDiscriminatorValue); } },
            };
        }
        /// <summary>
        /// Serializes information the current object
        /// <param name="writer">Serialization writer to use to serialize this model</param>
        /// </summary>
        public void Serialize(ISerializationWriter writer) {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));
            writer.WriteObjectValue<Resource_labels>("labels", Labels);
        }
    }

Am I missing something here? I could not find anything related to this in the issues or docs.

@ghost ghost added the Needs: Triage 🔍 label Aug 23, 2022
@baywet
Copy link
Member

baywet commented Aug 23, 2022

Hi @morinow ,
Thanks for reaching out and for trying kiota.
You are correct, this is not supported today (in any language).
The reason being dictionaries are fully supported with OpenAPI 3.1, which OpenApi.Net, the library Kiota relies on to parse OpenAPI descriptions doesn't support it yet.
We have an issue tracking this. #62
Let me know if you need any other information otherwise I'll close this one as a duplicate.

@baywet baywet self-assigned this Aug 23, 2022
@morinow
Copy link
Author

morinow commented Aug 23, 2022

@baywet Thanks for the quick response. Sad to hear, that this is an actual limitation. Is there any realistic chance of this being resolved in the near future (if you can even tell)? This seems like a huge blocker for any API, that uses dictionary properties. Kiota looks great overall, which is why it's kind of frustrating.
Other than that, thanks for the clarification.

@baywet
Copy link
Member

baywet commented Aug 23, 2022

They have an issue tracking it and the team is actually working on it (I'm involved with reviewing the pull requests).

Would converting the property to an array of string be an ok workaround for the time being so you're unblocked?

@morinow
Copy link
Author

morinow commented Aug 24, 2022

@baywet

They microsoft/OpenAPI.NET#795 and the team is actually working on it (I'm involved with reviewing the pull requests).

Sounds great. I'll keep an eye on it.

Would converting the property to an array of string be an ok workaround for the time being so you're unblocked?

Don't think so tbh. But I'll just stick with the old generator then for now, until the upgrade has been done.

@baywet
Copy link
Member

baywet commented Aug 24, 2022

Thanks for the additional details, closing this one as we already have issues tracking this feature.

@baywet baywet closed this as completed Aug 24, 2022
@baywet baywet added duplicate This issue or pull request already exists and removed Needs: Attention 👋 labels Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants