Skip to content

Conversation

@desjoerd
Copy link
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.

Fixes #65017

@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
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.
Copy link
Contributor

Copilot AI left a comment

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
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
Member

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

@desjoerd
Copy link
Contributor Author

I missed that 😮.

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.

OpenAPI Specification generation adds null value to enum based on parameter order

2 participants