Skip to content

Commit 2db4713

Browse files
committed
fix for recurssion #2
1 parent bc13155 commit 2db4713

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ public Parameter resolveParameter(Parameter parameter){
264264

265265
public Schema resolveSchema(Schema schema) {
266266
if(schema.get$ref() != null) {
267-
268267
String ref= schema.get$ref();
269268
ref = ref.substring(ref.lastIndexOf("/") + 1);
270269
Schema resolved = schemas.get(ref);
@@ -306,8 +305,14 @@ public Schema resolveSchema(Schema schema) {
306305
Schema innerProperty = obj.getProperties().get(propertyName);
307306
// reference check
308307
if(schema != innerProperty) {
309-
Schema resolved = resolveSchema(innerProperty);
310-
updated.put(propertyName, resolved);
308+
if(resolvedProperties.get(propertyName) == null && resolvedProperties.get(propertyName) != innerProperty) {
309+
LOGGER.debug("avoiding infinite loop");
310+
Schema resolved = resolveSchema(innerProperty);
311+
updated.put(propertyName, resolved);
312+
resolvedProperties.put(propertyName, resolved);
313+
}else {
314+
updated.put(propertyName, resolvedProperties.get(propertyName));
315+
}
311316
}
312317
}
313318
obj.setProperties(updated);
@@ -500,7 +505,6 @@ public Schema resolveSchema(Schema schema) {
500505
resolvedProperties.put(propertyName, resolved);
501506
}else {
502507
updated.put(propertyName, resolvedProperties.get(propertyName));
503-
504508
}
505509
}
506510

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.tomakehurst.wiremock.client.WireMock;
99
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
1010
import io.swagger.v3.core.util.Json;
11+
import io.swagger.v3.core.util.Yaml;
1112
import io.swagger.v3.oas.models.Components;
1213
import io.swagger.v3.oas.models.OpenAPI;
1314
import io.swagger.v3.oas.models.Operation;
@@ -1151,6 +1152,20 @@ public void recursiveResolving() {
11511152

11521153
}
11531154

1155+
@Test
1156+
public void recursiveResolving2() {
1157+
ParseOptions parseOptions = new ParseOptions();
1158+
parseOptions.setResolve(true);
1159+
parseOptions.setResolveFully(true);
1160+
OpenAPI openAPI = new OpenAPIV3Parser().read("recursive2.yaml", null, parseOptions);
1161+
try {
1162+
Json.mapper().writeValueAsString(openAPI);
1163+
}
1164+
catch (Exception e) {
1165+
fail("Recursive loop found");
1166+
}
1167+
}
1168+
11541169
public String replacePort(String url){
11551170
String pathFile = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
11561171
return pathFile;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 'minimal'
4+
title: 'recursion2'
5+
description: 'problem in ResolverFully'
6+
7+
paths:
8+
/foo:
9+
post:
10+
responses:
11+
'200':
12+
description: Ok
13+
requestBody:
14+
$ref: '#/components/requestBodies/MyRequestBody'
15+
16+
/bar:
17+
put:
18+
responses:
19+
'200':
20+
description: Ok
21+
requestBody:
22+
$ref: '#/components/requestBodies/MyRequestBody'
23+
24+
components:
25+
schemas:
26+
Schema1:
27+
type: object
28+
properties:
29+
prop:
30+
$ref: '#/components/schemas/Schema2'
31+
Schema2:
32+
type: object
33+
properties:
34+
prop1:
35+
$ref: '#/components/schemas/Schema1'
36+
prop2:
37+
$ref: '#/components/schemas/Schema1'
38+
39+
requestBodies:
40+
MyRequestBody:
41+
content:
42+
application/json:
43+
schema:
44+
$ref: '#/components/schemas/Schema2'

0 commit comments

Comments
 (0)