Skip to content

Better nullability round-tripping#2994

Open
Youssef1313 wants to merge 1 commit into
microsoft:mainfrom
Youssef1313:dev/ygerges/v3-tests
Open

Better nullability round-tripping#2994
Youssef1313 wants to merge 1 commit into
microsoft:mainfrom
Youssef1313:dev/ygerges/v3-tests

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

@baywet Until we finalize the discussions and decisions in #2967, I think this PR is an improvement to the current situation.

  • It fixes a bug in deserialization where a null in enum array would be dropped (the change in src/Microsoft.OpenApi/Reader/JsonNodeHelper.cs fixes this bug)
  • It emits "nullable": true when we detect a null type, even if we haven't serialized a type (i.e, we allow emitting a redundant nullable property)
  • We detect the pattern of enum: [ null ], and deserialize it JsonSchemaType.Null. This makes round-tripping behavior better.

@Youssef1313
Youssef1313 requested a review from a team as a code owner July 25, 2026 14:54
Copilot AI review requested due to automatic review settings July 25, 2026 14:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 null elements when deserializing JSON arrays (notably enum) by mapping them to JsonNullSentinel.
  • Improve V3 schema deserialization by recognizing the pattern enum: [ null ] and converting it into Type = JsonSchemaType.Null (clearing Enum to avoid retaining a compatibility representation).
  • Adjust OpenAPI 3.0 schema serialization to emit "nullable": true whenever 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants