-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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
In OpenAPI specifications, the optional nature of a field can be expressed in two ways:
Not listed in the required array: This indicates that the field is optional.
Explicitly marked as nullable: true: This indicates that the field can accept null values.
However, when a field is not listed in the required array, it is already considered optional by definition. Explicitly adding nullable: true in such cases may be redundant.
Generation Details
Despite this, the OpenAPI Generator generates code that includes unnecessary IsSet validation logic for nullable fields. For example:
[JsonPropertyName("role")]
public string? Role { get; set; }
The string? type indicates that the field is nullable.
However, the generator still adds IsSet validation logic:
if (role.IsSet && role.Value == null)
throw new ArgumentNullException(nameof(role), "Property is not nullable for class UserLoginSuccessDto.");
This behavior is inconsistent with the semantics of OpenAPI and leads to overly strict validation.
Steps to reproduce
In openapi def file:
role:
type: string
nullable: true
(!: role is not in required fields list)
use csharp generator and check auto-generated class file.
Related issues/PRs
Suggest a fix
######Expected Behavior
If a field is not listed in the required array, it should be treated as optional without additional [IsSet] validation.
If the field is explicitly marked as nullable: true, the generator should respect this and allow null values without throwing exceptions.
######Actual Behavior
The generator adds unnecessary [IsSet] validation logic for nullable fields, even when they are not required.
This results in overly strict validation that contradicts the OpenAPI specification.
The key is: generator determine a field not in required list, so generator mark this field as nullable in cs def
string? Role
but with a confusing validation, unless mark this field with nullable: true.