Skip to content

OpenAPI: Prune null from enum/type in componentized schemas - #65251

Merged
Youssef1313 merged 8 commits into
dotnet:mainfrom
desjoerd:fix/gh-65017
Sep 3, 2026
Merged

Youssef1313 merged 8 commits into
dotnet:mainfrom
desjoerd:fix/gh-65017

Conversation

@desjoerd

@desjoerd desjoerd commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor

OpenAPI: Prune null from enum/type in componentized schemas

Improve handling of nullable enums and componentized types in OpenAPI schema generation.

  • Remove "null" from type/enum arrays and use nullable/oneOf for nullability.
  • Add tests to verify correct schema references and nullability for parameters and request bodies.

Description

Enums will emit null as a valid option when they are Nullable, this works well when they are inlined, but when it becomes a component it can also be used in a Non-Nullable context. The best way to solve this is to make it never nullable and use "OneOf" with references to add the null value as an option.

@desjoerd
desjoerd requested a review from a team as a code owner January 28, 2026 13:58
Copilot AI review requested due to automatic review settings January 28, 2026 13:58
@github-actions github-actions Bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 28, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jan 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 schema generation for nullable enums (and componentized schemas) by ensuring null is not emitted as an enum value/type in shared component schemas, and by validating correct nullability handling via additional tests.

Changes:

  • Added test coverage for nullable enum request bodies to ensure nullability is represented via oneOf (including a null schema) while keeping the referenced enum component non-nullable.
  • Added regression test to ensure enum component schemas don’t gain null enum values based on parameter ordering.
  • Updated JSON-schema post-processing to prune null from componentized schema type/enum representations.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.RequestBodySchemas.cs Adds a request body test asserting nullable enum properties are represented via oneOf + $ref, and the enum component does not include null.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs Adds a regression test ensuring parameter ordering doesn’t cause null to appear in componentized enum schemas.
src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs Adjusts pruning logic for componentized schemas to remove null from type/enum structures.

@desjoerd desjoerd added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Jan 28, 2026
@desjoerd

Copy link
Copy Markdown
Contributor Author

@captainsafia another ordering issue around nullable and OneOf. Maybe this can be considered for a portback to release/10 (10.0.4).

@martincostello

Copy link
Copy Markdown
Member

@desjoerd FYI Safia is no longer on the .NET team.

@desjoerd

Copy link
Copy Markdown
Contributor Author

I missed that 😮.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 5, 2026

@Youssef1313 Youssef1313 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.

@desjoerd There have been many recent changes in this area.

I wonder if this is no longer an issue in current main, and then we might only need to find some minimal and low-risk fix for .NET 10 only.

Could you check if current main behavior is already correct now?

Improve handling of nullable enums and componentized types in OpenAPI schema generation.
- Remove "null" from type/enum arrays and use nullable/oneOf for nullability.
- Add tests to verify correct schema references and nullability for parameters and request bodies.
Remove the obsolete parameter test and verify nullable and non-nullable properties safely share the same enum component schema by using OneOf
@desjoerd

Copy link
Copy Markdown
Contributor Author

@Youssef1313 I checked if the issue still persists by reverting the fix to be like main and running the unit test. The issue is still there, causing the shared Status enum schema to have null. This is a problem for the "non-nullable" Status property which references the same schema. I've updated the test to show the issue more clearly. I've also rebased the branch against main

@desjoerd
desjoerd requested a review from Youssef1313 August 14, 2026 13:44
Remove `null` from componentized enum schemas, since nullable properties
already represent it through `oneOf`. Update the OpenAPI 3.0–3.2 camel-
and Pascal-case baselines.
@desjoerd
desjoerd requested a review from Youssef1313 August 14, 2026 20:45
Replace individual object-model assertions with a JSON comparison of the
generated component schemas, making the expected nullable and shared enum
schema structure easier to read and more complete.

@Youssef1313 Youssef1313 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.

LGTM

@Youssef1313 Youssef1313 removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 3, 2026
@Youssef1313
Youssef1313 requested a review from davpetr as a code owner September 3, 2026 06:27
@Youssef1313

Copy link
Copy Markdown
Member

@desjoerd I unlinked #65017 because #65017 needs a targeted fix for release/10.0 and is already working correctly in .NET 11+ as far as I know. The case being fixed here is slightly different.

And anyways, due to major other changes that happened in .NET 11, many OpenAPI bug fixes can't be simple backports due to them building on top of other fixes that are not backported.

@Youssef1313
Youssef1313 merged commit 00a3fe4 into dotnet:main Sep 3, 2026
27 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member feature-openapi

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants