Skip to content

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

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

Open
3 of 5 tasks
CampionFabio opened this issue Apr 15, 2025 · 2 comments

Comments

@CampionFabio
Copy link

CampionFabio commented Apr 15, 2025

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.

@CampionFabio CampionFabio changed the title [BUG] [csharp] C# Models from schema with a property attribute "nullable": true generate properties not nulable [BUG] [CSHARP] Models from schema with a property attribute "nullable": true are generating properties not nullable Apr 15, 2025
@devhl-labs
Copy link
Contributor

I don't think its likely to get fixed. Consider migrating to the generichost library.

@CampionFabio
Copy link
Author

CampionFabio commented Apr 23, 2025 via email

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

No branches or pull requests

2 participants