OpenAPITools / OpenAPITools/openapi-generator

[BUG] [CSHARP] Models from schema with a property attribute "nullable": true are generating properties not nullable

Open
#21,090 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [X ] Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When properties are configured to be both required and nullable, the generated C# model does not allow a null value for that property.

openapi-generator version

Used
"@openapitools/openapi-generator-cli": "^2.18.4"
with

openapi-generator-cli version-manager set 7.12.0
OpenAPI declaration file content or url
{
  "openapi": "3.0.4",
  "info": {
    "title": "Required Issue Example",
    "version": "v1"
  },
  "paths": {
    "/api/Example": {
      "post": {
        "tags": [
          "Example"
        ],
        "requestBody": {
          "content": {
            "application/json-patch+json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Example"
                  }
                ]
              }
            },
            "application/json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Example"
                  }
                ]
              }
            },
            "text/json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Example"
                  }
                ]
              }
            },
            "application/*+json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Example"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Example": {
        "required": [
          "requiredNullable",
          "requiredNotNullable"
        ],
        "type": "object",
        "properties": {
          "requiredNullable": {
            "type": "string",
            "nullable": true
          },
          "requiredNotNullable": {
            "type": "string"
          }
        },
        "additionalProperties": false
      }
    },
    "securitySchemes": {
      "JWT": {
        "type": "http",
        "description": "Please enter in a valid token",
        "scheme": "Bearer",
        "bearerFormat": "Json Web Token,"
      }
    }
  }
}
Generation Details

The problem is that the property "requiredNullable" does not allow the null value, even though it is configured in the json as:

"properties": {
  "requiredNullable": {
    "type": "string",
    "nullable": true
    },
...

Constructor generated:

public Example(string requiredNullable = default(string), string requiredNotNullable = default(string))
{
    // to ensure "requiredNullable" is required (not null)
    if (requiredNullable == null)
    {
        throw new ArgumentNullException("requiredNullable is a required property for Example and cannot be null");
    }
    this.RequiredNullable = requiredNullable;
    // to ensure "requiredNotNullable" is required (not null)
    if (requiredNotNullable == null)
    {
        throw new ArgumentNullException("requiredNotNullable is a required property for Example and cannot be null");
    }
    this.RequiredNotNullable = requiredNotNullable;
}

This block (from above) will demand it to be not nullable:

if (requiredNullable == null)
    {
        throw new ArgumentNullException("requiredNullable is a required property for Example and cannot be null");
    }
Steps to reproduce

This is the command used to generate the Example.cs class:

openapi-generator-cli generate -i open-api-issue.json -g csharp -o ./example-client -p disallowAdditionalPropertiesIfNotPresent=false -p targetFramework=net8.0 -p packageName=ExampleClient -p netCoreProjectFile=true -p nullableReferenceTypes=true -p useCollection=true -p library=httpclient -p useSingleRequestParameter=true
Related issues/PRs

No

Suggest a fix

if the property is flagged with "nullable": true in the ref json , the generated C# model should not enforce the parameter to not be nullable.

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 supplied openapi-generator-cli command with the embedded specification and inspect the generated example-client/Example.cs. Compare requiredNullable with requiredNotNullable when nullableReferenceTypes=true; done means the nullable property accepts null while the non-nullable required property remains enforced, with regression coverage for the reproduced case.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.