autorest icon indicating copy to clipboard operation
autorest copied to clipboard

(C#) Dictionary value types are always nullable

Open bitsforthought opened this issue 10 years ago • 6 comments

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.

bitsforthought avatar Jan 09 '16 00:01 bitsforthought

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.

stankovski avatar Feb 12 '16 23:02 stankovski

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.

bitsforthought avatar Feb 16 '16 19:02 bitsforthought

@stankovski Thoughts on my proposal to make this a config setting of the generator instead of a part of the Swagger?

bitsforthought avatar Feb 26 '16 02:02 bitsforthought

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.

stankovski avatar Feb 26 '16 05:02 stankovski

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 avatar Sep 28 '16 23:09 fearthecowboy

@fearthecowboy That would be awesome, thanks!

bitsforthought avatar Sep 29 '16 20:09 bitsforthought