Skip to content

Commit

Permalink
fix for recurssion #2
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina committed Nov 24, 2018
1 parent bc13155 commit 2db4713
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ public Parameter resolveParameter(Parameter parameter){

public Schema resolveSchema(Schema schema) {
if(schema.get$ref() != null) {

String ref= schema.get$ref();
ref = ref.substring(ref.lastIndexOf("/") + 1);
Schema resolved = schemas.get(ref);
Expand Down Expand Up @@ -306,8 +305,14 @@ public Schema resolveSchema(Schema schema) {
Schema innerProperty = obj.getProperties().get(propertyName);
// reference check
if(schema != innerProperty) {
Schema resolved = resolveSchema(innerProperty);
updated.put(propertyName, resolved);
if(resolvedProperties.get(propertyName) == null && resolvedProperties.get(propertyName) != innerProperty) {
LOGGER.debug("avoiding infinite loop");
Schema resolved = resolveSchema(innerProperty);
updated.put(propertyName, resolved);
resolvedProperties.put(propertyName, resolved);
}else {
updated.put(propertyName, resolvedProperties.get(propertyName));
}
}
}
obj.setProperties(updated);
Expand Down Expand Up @@ -500,7 +505,6 @@ public Schema resolveSchema(Schema schema) {
resolvedProperties.put(propertyName, resolved);
}else {
updated.put(propertyName, resolvedProperties.get(propertyName));

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -1151,6 +1152,20 @@ public void recursiveResolving() {

}

@Test
public void recursiveResolving2() {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("recursive2.yaml", null, parseOptions);
try {
Json.mapper().writeValueAsString(openAPI);
}
catch (Exception e) {
fail("Recursive loop found");
}
}

public String replacePort(String url){
String pathFile = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
return pathFile;
Expand Down
44 changes: 44 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/recursive2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: 3.0.0
info:
version: 'minimal'
title: 'recursion2'
description: 'problem in ResolverFully'

paths:
/foo:
post:
responses:
'200':
description: Ok
requestBody:
$ref: '#/components/requestBodies/MyRequestBody'

/bar:
put:
responses:
'200':
description: Ok
requestBody:
$ref: '#/components/requestBodies/MyRequestBody'

components:
schemas:
Schema1:
type: object
properties:
prop:
$ref: '#/components/schemas/Schema2'
Schema2:
type: object
properties:
prop1:
$ref: '#/components/schemas/Schema1'
prop2:
$ref: '#/components/schemas/Schema1'

requestBodies:
MyRequestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Schema2'

0 comments on commit 2db4713

Please sign in to comment.