diff --git a/src/main/java/io/swagger/codegen/v3/generators/examples/ExampleGenerator.java b/src/main/java/io/swagger/codegen/v3/generators/examples/ExampleGenerator.java index fc22104d24..5d42a8fc6e 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/examples/ExampleGenerator.java +++ b/src/main/java/io/swagger/codegen/v3/generators/examples/ExampleGenerator.java @@ -157,7 +157,10 @@ public List> generate(Map examples, List 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(); @@ -269,10 +272,10 @@ private double randomNumber(Double min, Double max) { } private Object resolveModelToExample(String name, String mediaType, Schema schema, Set processedModels) { - if (processedModels.contains(name)) { + if (processedModels.contains(schema.get$ref())) { return schema.getExample(); } - processedModels.add(name); + processedModels.add(schema.get$ref()); Map values = new HashMap<>(); logger.debug("Resolving model '{}' to example", name); diff --git a/src/test/java/io/swagger/codegen/v3/generators/examples/ExampleGeneratorTest.java b/src/test/java/io/swagger/codegen/v3/generators/examples/ExampleGeneratorTest.java index d50f4fada3..52a8093ae7 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/examples/ExampleGeneratorTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/examples/ExampleGeneratorTest.java @@ -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> exampleList = exampleGenerator.generate(null, null, categorySchema); + Assert.assertEquals(exampleList.size(), 1); + final Map example = exampleList.get(0); + Assert.assertEquals(example.get("contentType"), "application/json"); + Assert.assertTrue(example.get("example").contains("\"name\" : \"Yinotheria\"")); + } } diff --git a/src/test/resources/3_0_0/petstore.yaml b/src/test/resources/3_0_0/petstore.yaml index 765afd90a7..9837e36bce 100644 --- a/src/test/resources/3_0_0/petstore.yaml +++ b/src/test/resources/3_0_0/petstore.yaml @@ -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: