Repository navigation
OpenAPI: Prune null from enum/type in componentized schemas - #65251
Conversation
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. |
|
@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. |
Youssef1313
left a comment
There was a problem hiding this comment.
@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
|
@Youssef1313 I checked if the issue still persists by reverting the fix to be like |
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.
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.
|
@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. |
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.