Closed AnReZa closed 6 days ago
?
does not mean nullable it means optional. For nullable you should use | null
model TestStandSetupParameter {
unloadPosition: int32;
loadPosition: int32;
motorSizeCode?: int32 | null;
}
however this more sounds an issue with nswag as you usually do not want null
over the wire and the openapi is more what you'd want to represent
So we simply need to add the | null
to any nullable property and it'll work?
if you do that it will indeed make the property nullable, you are saying by that that the property can accept the null type on top of string.
But as said above this is often not waht you want except for json merge patch.(Json payloads over http are not filled with null
they are just not providing the properties)
@timotheeguerin You are right. For some reason, the API explicitly returns null right now, while in reality it could simply drop the property. Do you know the setting for the OpenAPI c# server-side compiler by coincidence?
If you are using dotnet I think this one https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.ignorenullvalues?view=net-8.0
I'll close thisd issue @AnReZa , please reopen or create a new one if there is something else
Describe the bug
We're having the issue, that the typespec compiler doesn't set the
nullable: true
property inside the openapi.yaml file for nullable model properties. If we try to add the openapi.yaml as a web service reference in a c# projects, it doesn't generate any c# property as nullable. Therefore, we get an exeption whenever our API rightfully returnsnull
for nullable values.Our TypeSpec model
See that
motorSizeCode
is marked as nullable using the?
decorator.The generated YAML file
See, that the
motorSizeCode
is not decorated withnullable: true
.NSwag-generated c# code
Using the built-in web service reference wizard in Visual Studio 2022, version 17.11.0.
As you can see, the
MotorSizeCode
property is not recognized as nullable in the generated c# class. It even has theRequired = Newtonsoft.Json.Required.DisallowNull
attribute.Weirdly, we're using the OpenAPI generator to generate the server-side implementation and there, everything works fine and the properties are marked as nullable when not set to b required.
What do we need to change in order to make that work as intended?
Reproduction
Simply generate the YAML file from the given tsp model or copy it into the online tool.
Checklist