Skip to content

[REQ][csharp-netcore] Support nullable value types #21140

Open
@mlhDevelopment

Description

@mlhDevelopment

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

.net core v8 introduced nullable value types. If a value is returned as null:

{
    "age": null
}

or explicit undefined:

{
    "age": undefined
}

or missing undefined ({ }) then openapi-generator will set that value to the default for the type. This is appropriate for reference types but introduces ambiguity for value types. E.g. If the age of the cheese is within the past year, the API may response { "age": 0 } but if the age is unknown or not applicable, { } the value of age will be set to 0 in both scenarios.

Describe the solution you'd like

Using the test case below,

Requested State: In OptionalPayload.cs, the property Age is of datatype int?

Current State: In OptionalPayload.cs, the property Age is of datatype int

Regression Scenario: In RequiredPayload.cs, the property Age is of datatype int and should remain int

Describe alternatives you've considered

  • Stop using openapi-generator
  • Manually editing all openapi-generator output

Additional context

Consider the following YML:

openapi: 3.0.3
info:
  title: Sample API
  version: 1.0.0
  description: Test optional primitive
servers:
  - url: https://example.com/api/v1
    description: Fake service
paths:
  /optional:
    get:
      summary: Get an optional integer value
      operationId: getOptionalAge
      responses:
        "200":
          description: Successful optional integer value
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/OptionalPayload"
  /required:
    get:
      summary: Get a required integer value
      operationId: getRequiredAge
      responses:
        "200":
          description: Successful required integer value
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/RequiredPayload"
components:
  schemas:
    OptionalPayload:
      type: object
      properties:
        age:
          type: integer
          description: Optional Age
    RequiredPayload:
      type: object
      properties:
        age:
          type: integer
          description: Required Age
      required:
      - age

Using CLI generator v2.19.1 (@openapitools/[email protected]) with the following:

npx @openapitools/openapi-generator-cli generate -i .\sample.yml -g csharp -o .\output --skip-operation-example --enable-post-process-file -p targetFramework=net8.0 -p library=httpclient

The property Age in OptionalPayload.cs should be of type int? and be set to null if received the payload { }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions