autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Required value types should not be nullable if they are required

Open devigned opened this issue 11 years ago • 7 comments

Required query parameter still convert to nullable type:

{
  "name": "timeout",
  "in": "query",
  "required": true,
  "type": "integer",
  "format": "int32",
},

The generated C# code:

int? timeout,

caused by CodeNamer.NormalizeType function.

devigned avatar Oct 02 '15 23:10 devigned

It would be great to see this fixed. Let me know if I can help.

bitsforthought avatar Oct 16 '15 00:10 bitsforthought

@xingwu1 and @stankovski where did we land on this in our last conversation?

devigned avatar Oct 19 '15 23:10 devigned

All parameters are validated during the runtime, so the issue here is about compile time validation. Since some data types in C# (such as strings or classes) can not be made non-nullable we will always end up with a mix of compile time and runtime validation. Our decision to have all parameters as nullable has simplified the code generation logic significatly, as such unless there is a strong case for adding non-nullable primitives back I would recommend against it.

stankovski avatar Oct 23 '15 00:10 stankovski

@devigned please re-open if you want to persue this change

stankovski avatar Oct 23 '15 00:10 stankovski

@devigned @stankovski Just to clarify, are we only talking about parameters here, or properties too? I would love for my customers to not have to worry about nullability of model properties in situations where it doesn't make sense.

As a matter of principle, we should make decisions based on what's best for the developer experience and not ease of implementation.

bitsforthought avatar Oct 23 '15 01:10 bitsforthought

Totally agree @brjohnstmsft, actually compare to validation of assignment of required primitive type, I think the accurate of model properties type is more important.

xingwu1 avatar Oct 23 '15 18:10 xingwu1

@xingwu1 Exactly -- I don't actually need the generated code to validate model properties, since the REST API already does this.

bitsforthought avatar Oct 23 '15 18:10 bitsforthought