OpenAPI: Prune null from enum/type in componentized schemas#65251
OpenAPI: Prune null from enum/type in componentized schemas#65251desjoerd wants to merge 3 commits intodotnet:mainfrom
Conversation
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.
c3098f5 to
3f3e90f
Compare
There was a problem hiding this comment.
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 anullschema) while keeping the referenced enum component non-nullable. - Added regression test to ensure enum component schemas don’t gain
nullenum values based on parameter ordering. - Updated JSON-schema post-processing to prune
nullfrom componentized schematype/enumrepresentations.
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. |
...NetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs
Show resolved
Hide resolved
...tCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.RequestBodySchemas.cs
Outdated
Show resolved
Hide resolved
|
@captainsafia another ordering issue around nullable and OneOf. Maybe this can be considered for a portback to release/10 (10.0.4). |
|
@desjoerd FYI Safia is no longer on the .NET team. |
|
I missed that 😮. |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
OpenAPI: Prune null from enum/type in componentized schemas
Improve handling of nullable enums and componentized types in OpenAPI schema generation.
Description
Enums will emit
nullas a valid option when they areNullable, 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 thenullvalue as an option.Fixes #65017