Open bstordrup opened 1 year ago
For generating a nullable contract you need to set "generateNullableReferenceTypes": true,
if I am not wrong
Doing so will mark the property as string?
(and also other properties as int?
).
But even without this, the string type can be null and is in cases also containing null
in the response from the API in question. And that fails because the generated property is decorated with Required = NewtonSoft.Json.Required.DisallowNull
Might be a corner case that a non-required string field in the response should not be marked with DisallowNull, but with Default instead.
The generated code is correct as none of your properties allow null, you'd need to set the type to ["string", "null"] to make it nullable
So, the definition for corelationId should be
"correlationId": {
"type": ["string", "null"]
},
in the Json behind the Swagger definition?
But that means that if I do not have control over the API, I'm not able to generate a correct class definition. Because the string can have the value "null" even though the API Json definition does not have the "null" part of the definition. As it it not referenced in the "required" list at the top of the definition.
Since you do not have necessary information in source document you can't do much in NSwag.
You can set Nullability or you can specify Required properties. One generates nullable types directly, the other works with generateNullableReferenceTypes
.
When the source document is not correct you can write your preprocessor which by some rules fix the source document.
Ah this is not JSON Schema.. in OpenAPI you can set “nullable” to true on the property
I have a schema that contains a MasterData like this:
Only the properties mentioned in the "required" property are required.
But in the Dto class generated, all properties are generated with the NewtonSoft.Json.Required.DisallowNull value for the JsonProperty.Required property in the JsonProperty attribute:
What do I need to set to get the generator to respect the "required" property list? My code generators settings are:
It seems like the generator does not handle a string based property as a property that can contain null.