fix(openapi): emit valid 3.0 schemas when downgrading from 3.1#8225
Merged
Conversation
LegacyOpenApiNormalizer used a shallow scan that converted type: ['x', 'null'] to anyOf at the top-level properties only. This produced invalid 3.0 output: orphan items keys, type: 'null' entries (not a valid 3.0 type), and no recursion into nested embeddables, items, allOf/oneOf/anyOf, or path-level inline schemas. Rewrite as a recursive walk that: - maps nullable types to type + nullable: true (3.0 idiomatic); - preserves items/properties/additionalProperties on nullable arrays and objects; - recurses into properties, patternProperties, items, additionalProperties, allOf, oneOf, anyOf, not, contains, propertyNames, if/then/else; - walks path operation requestBody, responses, and parameter schemas; - converts examples to example recursively. Refs api-platform#6194.
The 3.0 downgrade now emits the idiomatic type + nullable: true shape instead of anyOf with a type: null branch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LegacyOpenApiNormalizer(triggered by?spec_version=3.0.0andapi:openapi:export --spec-version=3.0.0) produced invalid OpenAPI 3.0 output because it only ran a shallow scan overcomponents.schemas.*.properties.*. Problems with the previous code:type: ['array', 'null']+items→anyOf: [{type:array},{type:null}]with an orphanitemssibling (invalid in 3.0).type: 'null'entries leaked through —nullis not a valid type in OpenAPI 3.0.properties.properties),items,additionalProperties,allOf/oneOf/anyOf.requestBody.content.*.schema,responses.*.content.*.schema,parameters.*.schema) were never visited.examples→exampleconversion was also shallow.Fix
Rewrite as a recursive schema walk:
type: X, nullable: true(3.0 idiomatic), preservingitems/properties/additionalProperties.anyOf+nullable: true.properties,patternProperties,items,additionalProperties,allOf,oneOf,anyOf,not,contains,propertyNames,if/then/else.paths.*.{method}.requestBody.content.*.schema,responses.*.content.*.schema,parameters.*.schema.examples→exampleapplied recursively.Test plan
src/OpenApi/Tests/Serializer/LegacyOpenApiNormalizerTest.php(10 cases) covering: untouched 3.1 default, nullable scalar, nullable array with items, nested embeddable, items recursion,allOf/oneOf/anyOf,additionalProperties, multi non-null union →anyOf, recursiveexamples→example, path operation request body walk.vendor/bin/phpunit src/OpenApi/Tests/Serializer/LegacyOpenApiNormalizerTest.php— 10/10 pass.vendor/bin/phpunit src/Symfony/Tests/Action/DocumentationActionTest.php— 11/11 pass.OpenApiFactoryTest::testInvokedescription text) is pre-existing on4.3, unrelated to this change.Refs #6194 — addresses the embeddable nullable array case reported there for users who use the 3.0 downgrade as a workaround. The root issue (
zircote/swagger-phpnot supporting OpenAPI 3.1) remains upstream.