Skip to content

Nullable fixes #2358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2025
Merged

Nullable fixes #2358

merged 2 commits into from
May 19, 2025

Conversation

bkoelman
Copy link
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 bkoelman requested a review from a team as a code owner May 17, 2025 02:27
@bkoelman
Copy link
Contributor Author

@microsoft-github-policy-service agree

@bkoelman
Copy link
Contributor Author

Tagging @MaggieKimani1 and @baywet for review, based on commit history.

Copy link
Member

@baywet baywet left a comment

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!

@baywet baywet enabled auto-merge May 19, 2025 13:48
@baywet baywet merged commit 6c82aa6 into microsoft:main May 19, 2025
9 checks passed
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.

Position of nullable property changed in output document Redundant type: object emitted in 2.x preview
2 participants