OpenAPITools / OpenAPITools/openapi-generator

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

Open
#21,140 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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/openapi-generator-cli@2.19.1) 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 { }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided CLI generation command against sample.yml and compare OptionalPayload.cs with RequiredPayload.cs. Trace the csharp-netcore generator entry points that produce those model properties. Done means optional Age is int? and remains null for {}, while required Age remains int.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.