Skip to content
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

Open
5 of 6 tasks
kevinoid opened this issue Dec 17, 2019 · 11 comments
Open
5 of 6 tasks

[BUG][C#] Should use nullable types for non-required properties #4816

kevinoid opened this issue Dec 17, 2019 · 11 comments

Comments

@kevinoid
Copy link
Contributor

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
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
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.

Command line used for generation

java -jar openapi-generator-cli.jar generate -g csharp-netcore -i openapi.yaml -o generated

Steps to reproduce
  1. Ensure the API returns at least one open-ended range (i.e. a DateRange object without an end property).
  2. Call 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.
  3. Note that there is no way to call AddDateRange with an open-ended range, since End 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. (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 #3725 (comment).

Suggest a fix

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

@auto-labeler
Copy link

auto-labeler bot commented Dec 17, 2019

👍 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.

@josephmulholland
Copy link

I have noticed the exact same issue.

I have pinpointed where the issue was introduced:

  • Version v4.0.3 non-required properties are generated as nullable types
  • Version v4.1.0 non-required properties are generated as NON-NULLABLE

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

@Otiel
Copy link

Otiel commented May 26, 2020

@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

#3537 (comment)

Does that mean that openapi-generator will always require x:nullable:true from now on? Or could this issue be fixed?

@jackmahoney
Copy link

Hi has anyone had any luck with this? Currently facing same issue

@WereDev
Copy link

WereDev commented Dec 3, 2020

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.

@slarti-b
Copy link
Contributor

@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
#3537 (comment)

Does that mean that openapi-generator will always require x:nullable:true from now on? Or could this issue be fixed?

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.

@davidmoten
Copy link

Geez, can we get a fix for this please? It's a pretty major bug to my eyes.

@ozomer
Copy link

ozomer commented Sep 13, 2021

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 PatchRequest() object.

@wing328
Copy link
Member

wing328 commented Sep 13, 2021

Requiring a vendor extension (and updates to API definition) doesn't seem right.

In OpenAPI spec 3.0, nullable is part of the official spec.

One possible way is to introduce an option to use nullable type without using nullable in the spec.

Let me know if anyone wants to contribute such enhancement or sponsor it.

@slarti-b
Copy link
Contributor

Requiring a vendor extension (and updates to API definition) doesn't seem right.

In OpenAPI spec 3.0, nullable is part of the official spec.

Yes, but for Swagger 2.0 it's a vendor extension. From the upgrade note:

To make a property nullable in OpenAPI/Swagger spec 2.0, please use x-nullable: true

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 nullable as specifying that a field can be null _even though it has a default value. Otherwise you end up with a catch-22: A field which is not required, has no default value but is not nullable - what is that? In practice, you have to give a value (since null will fail) so you're violating the API spec and making the field required. Or am I missing something - what value can be given to such a field?

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.

@smargoli2
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants