Skip to content
Merged
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 @@ -21,6 +21,7 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -66,7 +67,8 @@ public ResolverFully(ParseOptions options) {
private Map<String, RequestBody> requestBodies;
private Map<String, Header> headers;
private Map<String, Link> links;
private Map<String, Schema> resolvedProperties = new IdentityHashMap<>();
private Map<Schema, Schema> resolvedSchemas = new IdentityHashMap<>();
private Set<Schema> schemasInProgress = Collections.newSetFromMap(new IdentityHashMap<>());
private Map<String, Callback> callbacks;

public void resolveFully(OpenAPI openAPI) {
Expand Down Expand Up @@ -336,6 +338,28 @@ public Schema resolveSchema(Schema schema) {
if (schema == null) {
return null;
}
Schema cached = resolvedSchemas.get(schema);
if (cached != null) {
return cached;
}
if (schemasInProgress.contains(schema)) {
return schema;
}
return resolveAndCache(schema);
}

private Schema resolveAndCache(Schema schema) {
schemasInProgress.add(schema);
try {
Schema resolved = resolveSchemaImpl(schema);
resolvedSchemas.put(schema, resolved);
return resolved;
} finally {
schemasInProgress.remove(schema);
}
}

private Schema resolveSchemaImpl(Schema schema) {

if(schema.get$ref() != null) {
String ref= schema.get$ref();
Expand Down Expand Up @@ -394,7 +418,7 @@ public Schema resolveSchema(Schema schema) {
Schema innerProperty = obj.getProperties().get(propertyName);
// reference check
if(schema != innerProperty) {
updated.put(propertyName, resolveSchemaProperty(propertyName, innerProperty));
updated.put(propertyName, resolveSchema(innerProperty));
}
}
obj.setProperties(updated);
Expand Down Expand Up @@ -499,7 +523,7 @@ public Schema resolveSchema(Schema schema) {
Map<String, Schema> properties = model.getProperties();
for (String propertyName : properties.keySet()) {
Schema property = (Schema) model.getProperties().get(propertyName);
updated.put(propertyName, resolveSchemaProperty(propertyName, property));
updated.put(propertyName, resolveSchema(property));
}

for (String key : updated.keySet()) {
Expand Down Expand Up @@ -573,7 +597,7 @@ private void aggregateSchemaCombinators(ComposedSchema sourceSchema, Schema targ
if (resolved.getProperties() != null) {
for (String key : properties.keySet()) {
Schema prop = (Schema) resolved.getProperties().get(key);
targetSchema.addProperties(key, resolveSchemaProperty(key, prop));
targetSchema.addProperties(key, resolveSchema(prop));
}

if (resolved.getRequired() != null) {
Expand Down Expand Up @@ -698,15 +722,4 @@ private void aggregateSchemaCombinators(ComposedSchema sourceSchema, Schema targ
targetSchema.setRequired(required);
}
}

private Schema resolveSchemaProperty(String propertyName, Schema innerProperty) {
if (resolvedProperties.get(propertyName) == null || resolvedProperties.get(propertyName) != innerProperty) {
LOGGER.debug("avoiding infinite loop");
Schema resolved = resolveSchema(innerProperty);
resolvedProperties.put(propertyName, resolved);
return resolved;
} else {
return resolvedProperties.get(propertyName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,66 @@ public void recursiveResolving2() {
}
}

@Test
public void recursiveResolvingIssue1751() {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("issue_1751.yaml", null, parseOptions);
assertNotNull(openAPI, "OpenAPI should be parsed successfully");
assertNotNull(openAPI.getComponents().getSchemas().get("NestedObject"), "NestedObject schema should be present");
try {
String serialized = Json.mapper().writeValueAsString(openAPI);
assertNotNull(serialized, "Serialized output should not be null");
}
catch (Exception e) {
fail("Recursive loop found: " + e.getMessage());
}
}

@Test
public void recursiveResolvingIssue1751MutualRecursion() {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("issue_1751_mutual_recursion.yaml", null, parseOptions);
assertNotNull(openAPI, "OpenAPI should be parsed successfully");
assertNotNull(openAPI.getComponents(), "Components should not be null");
assertNotNull(openAPI.getComponents().getSchemas().get("SchemaA"), "SchemaA should be present");
assertNotNull(openAPI.getComponents().getSchemas().get("SchemaB"), "SchemaB should be present");
assertEquals(openAPI.getComponents().getSchemas().get("SchemaA").getType(), "object", "SchemaA should be object type");
assertEquals(openAPI.getComponents().getSchemas().get("SchemaB").getType(), "object", "SchemaB should be object type");
try {
String serialized = Json.mapper().writeValueAsString(openAPI);
assertNotNull(serialized, "Serialized output should not be null");
}
catch (Exception e) {
fail("Recursive loop found: " + e.getMessage());
}
}

@Test
public void recursiveResolvingIssue1751RecursiveArrayItems() {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("issue_1751_recursive_array.yaml", null, parseOptions);
assertNotNull(openAPI, "OpenAPI should be parsed successfully");
Schema treeNode = openAPI.getComponents().getSchemas().get("TreeNode");
assertNotNull(treeNode, "TreeNode schema should be present");
assertEquals(treeNode.getType(), "object", "TreeNode should be object type");
Schema valueProperty = (Schema) treeNode.getProperties().get("value");
assertNotNull(valueProperty, "TreeNode should have a value property");
assertEquals(valueProperty.getType(), "integer", "value property should be integer type");
try {
String serialized = Json.mapper().writeValueAsString(openAPI);
assertNotNull(serialized, "Serialized output should not be null");
}
catch (Exception e) {
fail("Recursive loop found: " + e.getMessage());
}
}

@Test
public void recursiveIssue984() {
ParseOptions parseOptions = new ParseOptions();
Expand Down
26 changes: 26 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue_1751.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.0
paths:
/first:
get:
parameters:
- "$ref": "#/components/parameters/p_one"
- "$ref": "#/components/parameters/p_one"
- "$ref": "#/components/parameters/p_one"
responses:
200:
description: ok
components:
parameters:
p_one:
in: query
schema:
"$ref": "#/components/schemas/NestedObject"
style: deepObject
schemas:
NestedObject:
additionalProperties:
oneOf:
- "$ref": "#/components/schemas/NestedObject"
- not:
type: object
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.0
info:
title: Mutual Recursion Test
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: ok
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaA'
components:
schemas:
SchemaA:
type: object
properties:
name:
type: string
b:
$ref: '#/components/schemas/SchemaB'
SchemaB:
type: object
properties:
value:
type: integer
a:
$ref: '#/components/schemas/SchemaA'
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.0
info:
title: Recursive Array Items Test
version: 1.0.0
paths:
/tree:
get:
responses:
'200':
description: ok
content:
application/json:
schema:
$ref: '#/components/schemas/TreeNode'
components:
schemas:
TreeNode:
type: object
properties:
value:
type: integer
left:
$ref: '#/components/schemas/TreeNode'
right:
$ref: '#/components/schemas/TreeNode'
children:
type: array
items:
$ref: '#/components/schemas/TreeNode'
Loading