Nullable int property on response model for element that will always be present.
I have a response-payload model with this property:
"_count": {
"type": "integer",
"readOnly": true,
"default": 0,
"description": "This property shows the number of databases returned as part of list operation."
},
Since it's a response model, whether it's required or not is not a big client-side concern, but getting good code generated is. The element will always be there, and it doesn't make any sense for it to be writable.
However, Swagger won't allow me to make it required and read-only, so I thought adding a default value might force it to use a non-nullable value type, but that's not the case:
/// <summary>
/// This property shows the number of databases returned as part of
/// list operation.
/// </summary>
[JsonProperty(PropertyName = "_count")]
public int? _count { get; private set; }
As far as I can tell, there is no means to force use of a regular value type here. I suggest that a readOnly element with an explicit default value be used to indicate that 'null' will never be an issue.
Duplicate https://github.com/Azure/autorest/issues/513
These are great suggestions! Please look for issues that have already been opened and +1 them; It may save both of us a bunch of time.
@olydis -- can you write a unit test to reproduce this (if it still happens)?
Generate a few variations in a response type (read-only/not-read-only, with/without default value, required/not required) so we can see what variations are getting generated as what types, and then we can see how we want it fixed.
I agree that the presence of a default value on a value type would seem to mean that it shouldn't be nullable, and the default value is the acceptable value if the service didn't return the value.
The other option, would be to allow "x-nullable":false to forcibly say "no damn nullable, thank you very much" ...
@fearthecowboy Given that x-nullable is (or will soon be?) a thing, IMO it would be better to use it to control these client language-binding decisions, rather than overloading the semantics of standard Swagger elements too much.
Quickly reproduced the behavior, here is a full list of what gets generated when:
required |
default |
readOnly |
Nullable |
|---|---|---|---|
| no | no | no | yes |
| no | no | yes | yes |
| no | yes | no | yes |
| no | yes | yes | yes |
| yes | no | no | no |
| yes | yes | no | no |
So as expected, Nullable currently disappears only on required members.
@brjohnstmsft that's kinda what we're thinking. although, one could certainly argue that the default for not required / default provided should be not-nullable and if you still want it that way, set x-nullable:true ...
@fearthecowboy Yep, given that AutoRest already assumes that required == not-nullable, that's a reasonable position to take. I'm fine either way.
We reviewed this in standup this morning;
Given that this is an issue for creating a 'more usable client code', we're not going to change the current behavior based on what's out, but support an explicit "x-nullable":false to force the code generator to use a non-nullable type.
Which means that it should default to default(T) rather than null.