Skip to content

Fix issue with nullable values not validated when value is null Bug: 20525#20549

Merged
wing328 merged 7 commits intoOpenAPITools:masterfrom
janrhijn:fix-nullable-properties-not-read
Mar 23, 2025
Merged

Fix issue with nullable values not validated when value is null Bug: 20525#20549
wing328 merged 7 commits intoOpenAPITools:masterfrom
janrhijn:fix-nullable-properties-not-read

Conversation

@janrhijn
Copy link
Copy Markdown
Contributor

@janrhijn janrhijn commented Jan 28, 2025

Fix for this #issue #20525. This change removes the if (utf8JsonReader.TokenType != JsonTokenType.Null) check and instead implements a ternary check utf8JsonReader.TokenType == JsonTokenType.Null ? null : utf8JsonReader.GetXXX() in the constructor of the Option. This results that when a property has a null value the Option is initialized (and IsSet = True, with Value = null) which fixes the validation for nullable properties with a null value.
From this:

case "int":
  if (utf8JsonReader.TokenType != JsonTokenType.Null)
    varInt = new Option<int?>(utf8JsonReader.GetInt32());
  break;

to this

case "int":
  varInt = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? null : utf8JsonReader.GetInt32());
  break;

File with code change: modules/openapi-generator/src/main/resources/csharp/libraries/generichost/JsonConverter.mustache

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request. @mandrean (2017/08) @shibayan (2020/02) @Blackclaws (2021/03) @lucamazzanti (2021/05) @iBicha (2023/07)

@janrhijn janrhijn marked this pull request as ready for review January 28, 2025 15:48
@janrhijn janrhijn changed the title Fix issue with nullable values not read when value is null Bug: 20525 Fix issue with nullable values not validated when value is null Bug: 20525 Jan 28, 2025
@devhl-labs
Copy link
Copy Markdown
Contributor

Looks good, please resolve the conflicts @janrhijn

@janrhijn
Copy link
Copy Markdown
Contributor Author

janrhijn commented Feb 4, 2025

Looks good, please resolve the conflicts @janrhijn

I have resolved the conflicts

@wing328
Copy link
Copy Markdown
Member

wing328 commented Feb 18, 2025

can you please review the CI test failure when you've time?

@janrhijn
Copy link
Copy Markdown
Contributor Author

janrhijn commented Feb 20, 2025

can you please review the CI test failure when you've time?

Fixed!
In order to fix the Target-Typed Conditional Expression are not supported (CS8957) error, I explicitly cast the null to the specific type:

case "int":
  varInt = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? (int?)null : utf8JsonReader.GetInt32());
  break;

@marcselman
Copy link
Copy Markdown

It looks like this fix is ready to be merged.
I'm waiting for this fix since I am experiencing the same issue.
When can we expect this to be released?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Mar 23, 2025

all tests (with updated samples) passed via #20951

@wing328 wing328 merged commit 2ec6b53 into OpenAPITools:master Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants