Skip to content

Commit

Permalink
Merge pull request #256 from vishwas0906/master
Browse files Browse the repository at this point in the history
Circular reference cause stackoverflow error when generating example from schema
  • Loading branch information
gracekarina committed May 25, 2018
2 parents 881e7ec + b387af2 commit d3778a3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.swagger.models.properties.StringProperty;
import io.swagger.models.properties.UUIDProperty;
import io.swagger.util.Json;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -302,6 +303,10 @@ public static Example fromProperty(Property property, Map<String, Model> definit
output = new DecimalExample(new BigDecimal(SAMPLE_DECIMAL_PROPERTY_VALUE));
}
} else if (property instanceof ObjectProperty) {
if(processedModels.contains(property.getName())) {
// return some sort of example
return alreadyProcessedRefExample(property.getName(), definitions);
}
if (example != null) {
try {
output = Json.mapper().readValue(Json.mapper().writeValueAsString(example), ObjectExample.class);
Expand Down Expand Up @@ -508,6 +513,8 @@ else if(model instanceof ModelImpl) {
if(impl.getProperties() != null) {
for(String key : impl.getProperties().keySet()) {
Property property = impl.getProperties().get(key);
if(property instanceof ObjectProperty)
property.setName(StringUtils.capitalize(key));
Example propExample = fromProperty(property, definitions, processedModels);
ex.put(key, propExample);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/swagger/inflector/utils/ResolverUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class ResolverUtil {
private Map<String, Model> resolvedModels = new HashMap<>();
private Map<String, Property> resolvedProperties = new HashMap<>();

public Map<String, Model> getResolvedModels() {
return resolvedModels;
}

public void resolveFully(Swagger swagger) {
if (swagger.getDefinitions() != null) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/io/swagger/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.swagger.inflector.examples.models.StringExample;
import io.swagger.inflector.processors.JsonExampleDeserializer;
import io.swagger.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.inflector.utils.ResolverUtil;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Response;
Expand Down Expand Up @@ -633,6 +634,24 @@ public void testAllOfAndRef() throws Exception {
"}");
}

@Test
public void testCircularRefSchema() throws Exception {
Swagger swagger = new SwaggerParser().read("./src/test/swagger/circuler-refs-SPLAT-56.yaml");
ResolverUtil resolverUtil = new ResolverUtil();
resolverUtil.resolveFully(swagger);
Example example = ExampleBuilder.fromProperty(new RefProperty("Source"), resolverUtil.getResolvedModels());
assertEqualsIgnoreLineEnding(Json.pretty(example), "{\n" +
" \"id\" : 0,\n" +
" \"name\" : \"CDR\",\n" +
" \"candidates\" : {\n" +
" \"id\" : 0,\n" +
" \"firstName\" : \"Jean\",\n" +
" \"lastName\" : \"Dupont\",\n" +
" \"source\" : { }\n" +
" }\n" +
"}");
}

private String getExampleForPath(Swagger swagger, String s) {
Response response = swagger.getPath(s).getGet().getResponses().get("200");
Example example = ExampleBuilder.fromProperty(response.getSchema(), swagger.getDefinitions());
Expand Down
95 changes: 95 additions & 0 deletions src/test/swagger/circuler-refs-SPLAT-56.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
swagger: '2.0'
tags:
- name: candidate
# - http
paths:
/candidates:
post:
tags:
- candidate
summary: Add a new candidate to the database
operationId: addCandidate
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description: Candiadate object that needs to be added to the database
required: true
schema:
$ref: '#/definitions/Candidate'
responses:
405:
description: Invalid input
security:
- petstore_auth:
- write:pets
- read:pet
/sources:
post:
tags:
- source
summary: Add a new source to the database
operationId: addSource
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description: Source object that needs to be added to the database
required: true
schema:
$ref: '#/definitions/Source'
responses:
405:
description: Invalid input
security:
- petstore_auth:
- write:pets
- read:pets
definitions:
Candidate:
type: object
required:
- firstName
- lastName
properties:
id:
type: integer
format: int64
firstName:
type: string
example: Jean
lastName:
type: string
example: Dupont
source:
$ref: '#/definitions/Source'
Source:
type: object
required:
- name
properties:
id:
type: integer
format: int64
name:
type: string
example: CDR
candidates:
$ref: '#/definitions/Candidate'
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
# Added by API Auto Mocking Plugin
# host: miami.swaggerhub.com
basePath: /mia87/MiamiAPI/1.0.1
schemes:
- https
- http
# Added by API Auto Mocking Plugin
host: virtserver.swaggerhub.com

0 comments on commit d3778a3

Please sign in to comment.