Skip to content
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

Fix recursion failure when generating examples #431

Merged
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 @@ -157,7 +157,10 @@ public List<Map<String, String>> generate(Map<String, Object> examples, List<Str
}

private Object resolveSchemaToExample(String propertyName, String mediaType, Schema schema, Set<String> processedModels) {
// logger.debug("Resolving example for property {}...", schema);
if (processedModels.contains(schema.get$ref())) {
return schema.getExample();
}
processedModels.add(schema.get$ref());
if (schema.getExample() != null) {
logger.debug("Example set in swagger spec, returning example: '{}'", schema.getExample().toString());
return schema.getExample();
Expand Down Expand Up @@ -269,10 +272,10 @@ private double randomNumber(Double min, Double max) {
}

private Object resolveModelToExample(String name, String mediaType, Schema schema, Set<String> processedModels) {
if (processedModels.contains(name)) {
if (processedModels.contains(schema.get$ref())) {
return schema.getExample();
}
processedModels.add(name);
processedModels.add(schema.get$ref());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These processedModels are passed to resolveSchemaToExample() at line 290.

I changed this from using the name because names are not guaranteed to be unique within an object tree. For example, an id property on the top-level schema could be redundant with an id in a nested object. In the case of a self-referencing or circular structure, names are certain not to be unique.

Map<String, Object> values = new HashMap<>();

logger.debug("Resolving model '{}' to example", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ public void testExampleFromSchema() throws Exception {
Assert.assertNotNull(example);
Assert.assertTrue(example.contains("\"name\" : \"doggie\""));
}

@Test
public void testExampleWithRecursiveNodes() throws Exception {
final Schema categorySchema = openAPI.getComponents().getSchemas().get("Category");
final ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI);

final List<Map<String, String>> exampleList = exampleGenerator.generate(null, null, categorySchema);
Assert.assertEquals(exampleList.size(), 1);
final Map<String, String> example = exampleList.get(0);
Assert.assertEquals(example.get("contentType"), "application/json");
Assert.assertTrue(example.get("example").contains("\"name\" : \"Yinotheria\""));
}
}
12 changes: 12 additions & 0 deletions src/test/resources/3_0_0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ components:
format: int64
name:
type: string
subcategories:
type: array
items:
$ref: '#/components/schemas/Category'
example:
- id: 100
name: Mammal
subcategories:
- id: 110
name: Yinotheria
- id: 120
name: Theriiformes
xml:
name: Category
User:
Expand Down