-
Notifications
You must be signed in to change notification settings - Fork 10.6k
OpenAPI: Prune null from enum/type in componentized schemas #65251
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
base: main
Are you sure you want to change the base?
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.
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 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 😮. |
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