Skip to content

Nullable fixes - #2358

Merged
Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
bkoelman:nullable-fixes
May 19, 2025
Merged

Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
bkoelman:nullable-fixes

Conversation

@bkoelman

Copy link
Copy Markdown
Contributor

This PR updates the deserializers to translate incoming nullable and x-nullable properties to the OpenApiSchema.Type property. And then translate back into separate properties in the V2/V3 serializers, preserving the same ordering as in the current 1.x versions.

I chose this approach to avoid having multiple versions of the truth internally. For example, a V2 document may be loaded that contains x-nullable: true, then the OpenApiSchema.Type is modified from user code, and the file is written back. It would be unexpected to have the presence of x-nullable: true in the original document override the OpenApiSchema.Type change from code. I'm assuming that consumers upgrading to v2.x are going to manipulate the OpenApiSchema.Type property to add/remove nullability, and then write out the version as configured (which is an implementation detail).

But for backwards compatibility, the presence of x-nullable in the OpenApiSchema is still taken into account, in case it gets explicitly added by user code.

The only downside is that we lose the relative position of x-nullable: true if multiple extensions are used. I believe this to be an acceptable tradeoff.

Furthermore, this PR simplifies and optimizes the logic to write the schema type. When writing V2/V3, multiple non-null types still result in not writing the type constraint. Writing nullability in V2/V3 is handled separately from writing the schema type.

Tests are added to verify nullable/x-nullable with/without a schema type round-trip in V2/V3.

Fixes #2342, fixes #2344.

@bkoelman
Bart Koelman (bkoelman) requested a review from a team as a code owner May 17, 2025 02:27
@bkoelman

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@bkoelman

Copy link
Copy Markdown
Contributor Author

Tagging Maggie Kimani (@MaggieKimani1) and Vincent Biret (@baywet) for review, based on commit history.

@baywet Vincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is great work and that cleans up the code quite a bit! Thanks for the contribution!

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

Labels

None yet

2 participants