Better nullability round-tripping#2994
Open
Youssef1313 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves OpenAPI 3.0 compatibility and “round-tripping” of nullability for schemas, addressing a regression where nullability could be lost when serializing and re-reading documents.
Changes:
- Preserve
nullelements when deserializing JSON arrays (notablyenum) by mapping them toJsonNullSentinel. - Improve V3 schema deserialization by recognizing the pattern
enum: [ null ]and converting it intoType = JsonSchemaType.Null(clearingEnumto avoid retaining a compatibility representation). - Adjust OpenAPI 3.0 schema serialization to emit
"nullable": truewhenever a null type is detected, even if"type"is not serialized, and update/add tests accordingly.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaV30CompatibilityTests.cs | Adds focused compatibility tests covering nullable enum round-trip and type: null compatibility behavior for OAS 3.0 serialization/deserialization. |
| test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs | Updates expected OAS 3.0 output to include "nullable": true alongside enum: [ null ] compatibility schemas. |
| src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs | Converts enum: [ null ] into JsonSchemaType.Null to improve model round-tripping. |
| src/Microsoft.OpenApi/Reader/JsonNodeHelper.cs | Fixes array deserialization to retain null elements by using JsonNullSentinel instead of dropping them. |
| src/Microsoft.OpenApi/Models/OpenApiSchema.cs | Always attempts to serialize nullable for OAS 3.0 when a null type is present (even if type is omitted), and simplifies type serialization helper. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@baywet Until we finalize the discussions and decisions in #2967, I think this PR is an improvement to the current situation.
src/Microsoft.OpenApi/Reader/JsonNodeHelper.csfixes this bug)"nullable": truewhen we detect a null type, even if we haven't serialized a type (i.e, we allow emitting a redundant nullable property)enum: [ null ], and deserialize itJsonSchemaType.Null. This makes round-tripping behavior better.