Skip to content

Issue 422 #771

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import org.openapitools.openapidiff.core.model.deferred.RecursiveSchemaSet;
import org.openapitools.openapidiff.core.utils.RefPointer;
import org.openapitools.openapidiff.core.utils.RefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SchemaDiff {
private static final Logger logger = LoggerFactory.getLogger(SchemaDiff.class);

private static final RefPointer<Schema<?>> refPointer = new RefPointer<>(RefType.SCHEMAS);
private static final Map<Class<? extends Schema>, Class<? extends SchemaDiffResult>>
Expand Down Expand Up @@ -96,7 +99,14 @@ protected static Schema<?> resolveComposedSchema(
updatedVisitedRefs.add(composed.get$ref());
composed = refPointer.resolveRef(components, composed, composed.get$ref());
composed = resolveComposedSchema(components, composed, updatedVisitedRefs);
schema = addSchema(schema, composed);
if (composed.getProperties() != null
&& composed.getProperties().containsValue(composedSchema)) {
logger.warn(
Copy link
Author

@maksim-kosolapov maksim-kosolapov Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we fail in case of recursive refs? It's an invalid specification, isn't it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maksim-kosolapov thanks for your PR.
I would say cyclic refs are pretty normal for OpenAPI spec (meanwhile it's not convenient to handle them).

So based on code above this resolveComposedSchema method should handle cyclic refs
if (composed.get$ref() == null || !visitedRefs.contains(composed.get$ref())) so while diving into the properties processing cyclic refs should be skipped. Suppose there is an issue with how this method handle visitedRefs in recursion. Could you please check it from this perspective?

"Circular reference detected in composed (allOf/oneOf/anyOf) schema: {}",
composed.get$ref());
} else {
addSchema(schema, composed);
}
}
}
composedSchema.setAllOf(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public boolean contains(CacheKey key) {

public void put(CacheKey key) {
leftKeys.add(key.getLeft());
leftKeys.add(key.getRight());
rightKeys.add(key.getRight());
}
}
11 changes: 11 additions & 0 deletions core/src/test/resources/recursive_model_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ components:
type: string
message2:
type: string
recursiveDirect:
$ref: '#/components/schemas/B'
recursiveAllOf:
allOf:
- $ref: '#/components/schemas/B'
recursiveOneOf:
oneOf:
- $ref: '#/components/schemas/B'
recursiveAnyOf:
anyOf:
- $ref: '#/components/schemas/B'
details:
type: array
items:
Expand Down