-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Open
Labels
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.