(C#) Dictionary value types are always nullable
This issue probably affects generated Java code as well; I haven't checked.
If you have Swagger like this:
"TextWeights": {
"properties": {
"weights": {
"type": "object",
"additionalProperties": {
"type": "number",
"format": "double"
},
}
},
"required": [
"weights"
],
},
AutoRest generates a backing type in C# of IDictionary<string, double?> instead of IDictionary<string, double>. This is reasonable in cases where the REST API might not provide a value for every key, but this is not always the case. Sometimes the REST API guarantees that no values will be null, in which case it would be better for the client programming model to use a non-nullable primitive type.
Unfortunately I don't see an extensibility point in additionalProperties that would allow us to add an extension to solve this.
@devigned @Azure/adx-sdk-team Any thoughts on this? As a workaround, I'm going to use x-ms-external to suppress generation of any types affected by this issue, but it's a long-term maintenance problem whenever I do this.
Since there is no way to express whether collection or dictionary elements should be nullable or not, I'd like to close this issue here and recommend opening it in OpenAPI project.
Arguably if a collection or dictionary property is required, its elements ought not to be nullable. If we can't make it configurable via Swagger, what about making it a configuration setting of the C# code generator itself? That would work for our scenarios.
@stankovski Thoughts on my proposal to make this a config setting of the generator instead of a part of the Swagger?
I agree that it would be nice to make this behavior configurable, however I'm wary of adding too many configurations making the code hard to maintain and the tool difficult to use. How about I reopen this issue and track it along other requests for enhancements.
Hey @brjohnstmsft - I'm working on adding support for an x-nullable extension.
Since the model refactor, I don't think it would be terribly tricky to include support in additionalProperties to support x-nullable .
And on dictionaries and sequences too...
@fearthecowboy That would be awesome, thanks!