diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalker.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalker.java index 09106a012..8e451aec9 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalker.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalker.java @@ -20,7 +20,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.function.Supplier; @Slf4j @RequiredArgsConstructor @@ -73,6 +72,8 @@ public R fromSchema(Schema schema, Map definitions) { } private Optional buildExample(String name, Schema schema, Map definitions, Set visited) { + log.debug("Building example for schema {}", schema); + Optional exampleValue = getExampleValueFromSchemaAnnotation(schema); if (exampleValue.isPresent()) { return exampleValue; @@ -164,14 +165,12 @@ private Optional buildArrayExample(Schema schema, Map definit resolveSchemaFromRef(schema.getItems(), definitions).orElse(schema.getItems()); Optional arrayName = exampleValueGenerator.lookupSchemaName(schema); - Supplier arrayNameSupplier = () -> arrayName.orElseThrow( - () -> new ExampleGeneratingException("Array schema does not have a name: " + schema)); return exampleValueGenerator .lookupSchemaName(arrayItemSchema) .or(() -> arrayName) .flatMap(arrayItemName -> buildExample(arrayItemName, arrayItemSchema, definitions, visited)) - .map(arrayItem -> exampleValueGenerator.createArrayExample(arrayNameSupplier, arrayItem)); + .map(arrayItem -> exampleValueGenerator.createArrayExample(arrayName, arrayItem)); } private Optional buildFromStringSchema(Schema schema) { diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/ExampleValueGenerator.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/ExampleValueGenerator.java index 7748d4a16..73ad14d59 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/ExampleValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/ExampleValueGenerator.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Optional; -import java.util.function.Supplier; /** * Provides the building blocks to generate an example @@ -51,7 +50,7 @@ default void endObject() {} void addPropertyExamples(T object, List> properties); - T createArrayExample(Supplier nameSupplier, T arrayItem); + T createArrayExample(Optional name, T arrayItem); T createRaw(Object exampleValueString); diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/json/ExampleJsonValueGenerator.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/json/ExampleJsonValueGenerator.java index 18ab08b57..fe80be6a7 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/json/ExampleJsonValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/json/ExampleJsonValueGenerator.java @@ -20,7 +20,6 @@ import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.function.Supplier; @Slf4j public class ExampleJsonValueGenerator implements ExampleValueGenerator { @@ -76,7 +75,7 @@ public Optional createUnknownSchemaStringFormatExample(String schemaFo } @Override - public JsonNode createArrayExample(Supplier nameSupplier, JsonNode arrayItem) { + public JsonNode createArrayExample(Optional name, JsonNode arrayItem) { ArrayNode array = objectMapper.createArrayNode(); array.add(arrayItem); return array; diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGenerator.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGenerator.java index e851700b4..c0047ac3f 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGenerator.java @@ -3,6 +3,7 @@ import io.github.springwolf.core.asyncapi.components.examples.walkers.ExampleValueGenerator; import io.github.springwolf.core.asyncapi.components.examples.walkers.PropertyExample; +import io.github.springwolf.core.asyncapi.components.examples.walkers.SchemaWalker; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import lombok.extern.slf4j.Slf4j; @@ -27,7 +28,6 @@ import java.util.Optional; import java.util.Set; import java.util.Stack; -import java.util.function.Supplier; @Slf4j public class ExampleXmlValueGenerator implements ExampleValueGenerator { @@ -155,8 +155,11 @@ public Optional createUnknownSchemaStringFormatExample(String schemaFormat } @Override - public Node createArrayExample(Supplier nameSupplier, Node arrayItem) { - return wrapNode(nameSupplier.get(), arrayItem); + public Node createArrayExample(Optional arrayNameOptional, Node arrayItem) { + return arrayNameOptional + .map(arrayName -> wrapNode(arrayName, arrayItem)) + .orElseThrow(() -> new SchemaWalker.ExampleGeneratingException( + "Unable to add array item to array, because the array schema does not have a name")); } @Override diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/yaml/ExampleYamlValueGenerator.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/yaml/ExampleYamlValueGenerator.java index e5b63deef..bd7266b22 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/yaml/ExampleYamlValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/yaml/ExampleYamlValueGenerator.java @@ -15,7 +15,6 @@ import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.function.Supplier; @Slf4j @RequiredArgsConstructor @@ -107,8 +106,8 @@ public Optional createUnknownSchemaStringFormatExample(String schemaFo } @Override - public JsonNode createArrayExample(Supplier nameSupplier, JsonNode arrayItem) { - return this.exampleJsonValueGenerator.createArrayExample(nameSupplier, arrayItem); + public JsonNode createArrayExample(Optional name, JsonNode arrayItem) { + return this.exampleJsonValueGenerator.createArrayExample(name, arrayItem); } @Override