OpenAPITools / OpenAPITools/openapi-generator

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

Open
#22,474 4 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
  • 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

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.

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 with the C# generator output for the provided OpenAPI field and compare its handling of required and nullable properties with related issue #4816. Done means an optional nullable field such as role remains string? without generating the unnecessary IsSet validation or ArgumentNullException logic.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.