-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Generation: Non-Required Open Api 3.0 scalar properties are non nullable #2313
Comments
We currently face the same problem. Is there some setting we need to tweak? |
We’d need to set defaultvaluehandling to ignore, correct? |
The problem is that the generated property is not nullable (the type is If the generated property is an object or a string, it can be set to null. If it is a number ( |
The reason it is not nullable is because it is not specified as nullable in the spec, you'd need:
|
But yes you could argue that non-required properties also should be nullable and just ignored when null whereas nullable properties should serialize "null", right? |
That's what I mean. It's easy to deal with nullable and required properties (null is serialized as null) and for nun-nullable and non-required properties (null is left out/ignored). This is already handled very well by the generated Json.NET attributes. But just not for value types. Just change It only get's difficult for nullable and non-required properties; should null result in null or in a missing property. But those combination is anyway a bit useless ;) |
I figured out where the problem is. Namely, the code in the NJsonSchema project needs to be updated, specifically the IsNullable method of the JsonSchemaProperty class, see https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/JsonSchemaProperty.cs The relevant snippet currently reads:
It should read:
|
The problem here is that this is a huge breaking change for some... Wouldnt it be possible to generate DefaultValueHandling
And then leaving it to the default (0) would ignore it in the JSON? |
My proposal: We should not fix JsonSchemaProperty class (because the schema is not really nullable) but just enhance the C# generator with an additional setting (to avoid breaking changes). |
@RicoSuter: I think you‘re right. The property is optional and not nullable. That’s a difference C# can’t make. Making it configurable makes sense (not breaking other people’s code), although I think the defaulting to enabled would be the correct behaviour. |
It makes sense to change this default for the C# generator - but only in the next major version (breaking change) => RicoSuter/NJsonSchema#1045 |
Do you think GenerateNullableOptionalProperties is a good name? Any ideas? Can you review the PR? |
I'll try to review it until Friday 👍 |
Okay, that looks like a good solution. Maybe GenerateOptionalPropertiesAsNullable is a better name for the setting? |
I defined an Open API 3.0 spec with optional parameters. Optional parameters mean in my understanding, that they are nullable in C# and omitted in JSON serialization if they are null in C#.
But when I generate C# code, now the generated properties are never nullable, and I have no idea how to specify now not to serialize them. Am I doing something wrong or is this a major issue?
If I set the same spec to Open API 2.0, it behaves like I expect. The integers get generated as
int?
as I expect.See this spec:
Generated code:
I would like to send this Json as a response (set required
id
and leave outid2
)The text was updated successfully, but these errors were encountered: