Repository navigation
fix(resolver): allow circular reference placeholders in allOf for Ope… - #4312
jochenberger wants to merge 1 commit into
Conversation
Merging circular reference placeholders in allOf can leave the element graph cyclic. For polymorphic hierarchies (Base.oneOf lists its children, each child extends Base via allOf) the resolver then overflows the stack, depending on the order of schemas in components.schemas. Leave an allOf as is if it contains a circular replacement or a cyclic element graph. Other items are still merged, and non-object items are still reported as errors. Applies to the OpenAPI 3.1 and 3.2 strategies. Fixes swagger-api#3858 Refs swagger-api/swagger-ui#10583
d3ec029 to
78db5bf
Compare
|
Tested this PR against a large real-world definition (243 schemas, ten polymorphic hierarchies of the form
It depends on the order of schemas in reproopenapi: 3.1.0
paths:
/x:
put:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/A'
responses:
'204':
description: ok
components:
schemas:
A:
allOf:
- $ref: '#/components/schemas/Base'
Base:
oneOf:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B'
B:
allOf:
- $ref: '#/components/schemas/Base'I've pushed a revised commit that no longer merges an I haven't found out why merging a placeholder ends up cyclic. If you'd rather keep merging, I'm happy to dig into that instead. |
Description
When resolving OpenAPI 3.1/3.2 definitions with
useCircularStructures: false(the default), the dereference strategy runs withcircular: 'replace'. Each detected circular reference is replaced by thecircularReplacerwith a genericObjectElementthat contains only$ref. This is intentional, so the placeholder carries no Schema Object semantics.The
AllOfVisitorthen checksallOf.content.every(isSchemaElement). That check rejects the very placeholders the resolver inserted, so any circular reference insideallOfproducesTypeError: Elements in allOf must be objectsand leavesallOfunmerged.This PR makes the
AllOfVisitor(in both theopenapi-3-1-swagger-clientandopenapi-3-2-swagger-clientstrategies) accept these circular-reference placeholders: non-semanticObjectElements containing$ref. They are then merged like any otherallOfitem. The result is a schema that keeps its$refalongside its sibling keywords, which matches how the OpenAPI 2.0/3.0 specmap resolver already behaves. Genuinely invalidallOfitems (e.g. primitives) are still reported as errors.Motivation and Context
Fixes #3858
Refs swagger-api/swagger-ui#10583 (same root cause, reported against Swagger UI 5.28.0)
Minimal reproduction from #3858:
A more realistic case from swagger-ui#10583, a self-referencing schema whose
allOfadds constraints:Before this change,
friends.itemsstayed as an unmergedallOfand an error was reported. After this change, it resolves to{ $ref: '…/User', type: 'object', required: ['id'] }with no errors.How Has This Been Tested?
Added
test/bugs/3858.js. It runs for both OpenAPI 3.1.0 and 3.2.0 and goes through the public API (SwaggerClient.resolveandSwaggerClient.resolveSubtree). The public API is needed here because the existing visitor unit tests use apidom's defaultcircular: 'ignore'mode and never hit the swagger-clientcircularReplacer.The tests cover:
allOfdefinition from OpenAPI 3.1.0: error when resolving schemas with allOf and circular references #3858, both as a full resolve and as a subtree resolve of each schema: no errors, andallOfis merged;allOffrom swagger-ui#10583, full and subtree: no errors, and sibling keywords (type,required) are retained;allOfstill producesElements in allOf must be objects.Without the fix, 12 of the 14 new tests fail. The two negative-case tests pass either way, as intended. With the fix, all 14 pass. The existing resolver test suite and the full unit test suite pass as well.
Screenshots (if appropriate):
N/A. The error shown in the screenshot in #3858 no longer appears.
Types of changes
package.json)Checklist: