openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] [csharp] Handling of nullable and required properties

Open Bouillie opened this issue 3 months ago • 4 comments

Is your feature request related to a problem? Please describe.

With the current csharp generators (except generichost), there is a confusion between required and nullable fields. Nullable fields and non-required fields are both using the csharp nullable operator "?". It leads to several problems.

  • When a field is defined as not required and nullable, it's impossible to know if its value is missing or null.
  • Model validation is sometimes incorrect (https://github.com/OpenAPITools/openapi-generator/issues/21090)

Describe the solution you'd like

Make a distinction between required and nullable fields. The solution used by generichost, using an Option<T> struct for required fields seems good.

Describe alternatives you've considered

An alternative is moving to generichost but it's not possible for my project.

Bouillie avatar Oct 30 '25 11:10 Bouillie

I have planned to modify templates to use the "required" keyword for required properties for .NET 7 and later, and remove the constructor that take all the properties as arguments. This constructor has lead to many errors in our projects. I can make a pull-request for this later, and refer to this issue if you like.

jornhd avatar Nov 05 '25 10:11 jornhd

@jornhd I don't think removing the constructor is a good thing, we still need some validation in the constructor. Still, it needs to be fixed

Bouillie avatar Nov 12 '25 14:11 Bouillie

Related to

  • #17159
  • #15735

johnthagen avatar Nov 20 '25 17:11 johnthagen

Just wanted to add that even with the generichost client, required and nullable fields in a response cause deserialization errors as of version 7.10.0

  • https://github.com/OpenAPITools/openapi-generator/issues/15735#issuecomment-3559241906

This forced us to have to roll back changes to make our API more precise when responses would always return a value (i.e., required) but it could be null (i.e., nullable).

johnthagen avatar Nov 20 '25 17:11 johnthagen