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.
v4.0.0 and later
Example OpenAPI 3.0.2 document
openapi: '3.0.2'
info:
title: non-required property example
version: '1.0.0'
components:
schemas:
DateRange:
description: A possibly open-ended date range.
type: object
properties:
start:
type: string
format: date-time
end:
type: string
format: date-time
required:
- start
paths:
/date-ranges:
get:
operationId: getDateRanges
responses:
default:
description: Get date ranges
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/DateRange'
post:
operationId: addDateRange
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/DateRange'
responses:
'201':
description: Success
Note that end is not declared nullable: true because end is never null in the JSON produced or consumed by the API. It is either a date string, or not present.
java -jar openapi-generator-cli.jar generate -g csharp-netcore -i openapi.yaml -o generated
DateRange object without an end property).GetDateRanges and note that End for the open-ended range is DateTime(1900-01-01), which is problematic since it is indistinguishable from "end":"1900-01-01" and likely violates the constraint that End is not before Start.AddDateRange with an open-ended range, since End will always have a value.The regression occurred between v3.0.2 and v4.0.0. Bisect says the first bad commit is 3744273312 (v4.0.0), so I'm obviously doing something wrong. (Maybe cli is using published core of same version, rather than locally-built version?) Advice on how to bisect would be appreciated.
The issue was also discussed in https://github.com/OpenAPITools/openapi-generator/issues/3725#issuecomment-545039145.
I believe nullable types should be used for properties which are either nullable or not required, since null in C# is a reasonable representation of both JSON properties which are null and properties which are not present.
Thanks for considering,
Kevin
馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your 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:
Upgrade Note
To make a property nullable in OpenAPI/Swagger spec 2.0, please use
x-nullable: true_https://github.com/OpenAPITools/openapi-generator/pull/3537#issuecomment-523529169_
Does that mean that openapi-generator will always require x:nullable:true from now on? Or could this issue be fixed?
Hi has anyone had any luck with this? Currently facing same issue
Also having this issue. I have found that I have to use nullable: true instead of relying on the required which sadly means a lot of updates to our OpenApi specs if the issue can't be found.
Most helpful comment
@wing328 I've found your following statement in the PR that probably introduced this regression:
Does that mean that openapi-generator will always require
x:nullable:truefrom now on? Or could this issue be fixed?