-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[BUG][C#] Should use nullable types for non-required properties #4816
Comments
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
I have noticed the exact same issue. I have pinpointed where the issue was introduced:
As mentioned by @kevinoid a non-required Date property is populated instead with the Min value for a date i.e. 1900-01-01 when not present in a request |
@wing328 I've found your following statement in the PR that probably introduced this regression:
Does that mean that openapi-generator will always require |
Hi has anyone had any luck with this? Currently facing same issue |
Also having this issue. I have found that I have to use |
Can anyone comment on this question? It seems to me that if a property is not required and has no default then it must, by definition, could be null. What other alternative is there? Requiring a vendor extension (and updates to API definition) doesn't seem right. |
Geez, can we get a fix for this please? It's a pretty major bug to my eyes. |
We have a problem that might be related. We have a PATCH request where the body has an optional nullable string - i.e. it could be a string, it could be null, or it could be undefined. We have no way of choosing undefined when we create the |
In OpenAPI spec 3.0, One possible way is to introduce an option to use nullable type without using Let me know if anyone wants to contribute such enhancement or sponsor it. |
Yes, but for Swagger 2.0 it's a vendor extension. From the upgrade note:
This to me, does not seem right. Also, an optional parameter without a default value must be nullable. Nothing else makes sense. The API spec for such a field says "this does not have to have value" which is exactly the same meaning as "this is nullable". Anything else is overriding the spec to effectively make the field required. I see the At a bear minimum there should be a generation-time flag to make it behave like this. As I see it, for v2 we are requiring a vendor extension and even for v3 we are requiring that the spec is written in a specific way to get around the generator implementation. |
Bumping this. Other generators offer a property when generating code for 'Generate optional properties as nullable'. Can this be added as a feature to OpenApi Generator? |
Bug Report Checklist
Description
The C# generators currently generate model classes using non-nullable types for properties which are not required, which can't represent instances where those properties are not present.
openapi-generator version
v4.0.0 and later
OpenAPI declaration file content or url
Example OpenAPI 3.0.2 document
Note that
end
is not declarednullable: true
becauseend
is nevernull
in the JSON produced or consumed by the API. It is either a date string, or not present.Command line used for generation
java -jar openapi-generator-cli.jar generate -g csharp-netcore -i openapi.yaml -o generated
Steps to reproduce
DateRange
object without anend
property).GetDateRanges
and note thatEnd
for the open-ended range isDateTime(1900-01-01)
, which is problematic since it is indistinguishable from"end":"1900-01-01"
and likely violates the constraint thatEnd
is not beforeStart
.AddDateRange
with an open-ended range, sinceEnd
will always have a value.Related issues/PRs
The regression occurred between v3.0.2 and v4.0.0. Bisect says the first bad commit is 3744273 (
v4.0.0
), so I'm obviously doing something wrong. (Maybecli
is using publishedcore
of same version, rather than locally-built version?) Advice on how to bisect would be appreciated.The issue was also discussed in #3725 (comment).
Suggest a fix
I believe nullable types should be used for properties which are either
nullable
or notrequired
, sincenull
in C# is a reasonable representation of both JSON properties which arenull
and properties which are not present.Thanks for considering,
Kevin
The text was updated successfully, but these errors were encountered: