openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [C#] Generator adds unnecessary IsSet validation for nullable fields not in required list

Open ngyyuusora opened this issue 2 months ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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

https://github.com/OpenAPITools/openapi-generator/issues/4816

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.

ngyyuusora avatar Dec 01 '25 19:12 ngyyuusora

IsSet is necessary. We use it to know how to serialize the class. If it is null we need to know if it should be written to the json or not such as {"foo": null} or {}. Though if your property is nullable and not required, it probably shouldn't get enforce to be non-null. Please share a minified version of your yaml along with the generate command you use.

devhl-labs avatar Dec 05 '25 06:12 devhl-labs