(C#, Java) We need a way to model non-nullable readOnly properties
Sometimes read-only properties (i.e. -- properties that are only ever in a response body) are guaranteed by the REST API to never be null. When these properties map to value types in .NET or primitive types in Java, they should be modeled as non-nullable types. Currently all AutoRest-generated properties are nullable.
This issue is related. Assuming we fix that issue, we'll be able to ensure that constructor parameters and model properties in C# and Java are non-nullable if they are marked as required in the Swagger. This would also be very useful for some readOnly properties, but there is no way to express this in Swagger today.
I see two short-term options that don't involve changes to Swagger itself:
- Extend AutoRest to allow properties to be both
requiredandreadOnlyat the same time. - Add a new extension, like
x-ms-required-in-response(naming suggestions welcome) that would behave likerequiredwith respect to nullability, but forreadOnlyproperties.
Either way, we'd also need to fix the related issue.
The downside of option 1 is that it would break the rules for readOnly in the Swagger specification:
Relevant only for Schema "properties" definitions. Declares the property as "read only". This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request. Properties marked as readOnly being true SHOULD NOT be in the required list of the defined schema. Default value is false.
The semantics of readOnly + required do seem muddy, and I buy that they ought to be mutually exclusive. For this reason I tend to favor option 2.
@devigned @Azure/adx-sdk-team Thoughts?
Related #375
Also see: https://github.com/Azure/autorest/issues/816. Duplicate issue, with a third option proposed.
C# supports x-nullable now.
Java may be a bit yet.
Just tested this on Java - works for me! Example:
someModel:
type: object
properties:
prop:
type: integer
format: int32
readOnly: true
x-nullable: false # toggle me :-)
Will therefore close this issue, please reopen if something is missing.