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 316c4e055..939515bab 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 @@ -56,10 +56,7 @@ public R fromSchema(Schema schema, Map definitions) { exampleValueGenerator.initialize(); try { - String schemaName = exampleValueGenerator - .lookupSchemaName(schema) - .orElseThrow(() -> - new ExampleGeneratingException("There is no name set for Schema: " + schema.toString())); + Optional schemaName = exampleValueGenerator.lookupSchemaName(schema); T generatedExample = buildExample(schemaName, schema, definitions, new HashSet<>()) .orElseThrow(() -> new ExampleGeneratingException("Something went wrong")); @@ -71,7 +68,8 @@ public R fromSchema(Schema schema, Map definitions) { return null; } - private Optional buildExample(String name, Schema schema, Map definitions, Set visited) { + private Optional buildExample( + Optional name, Schema schema, Map definitions, Set visited) { log.debug("Building example for schema {}", schema); Optional exampleValue = getExampleFromSchemaAnnotation(name, schema); @@ -88,12 +86,13 @@ private Optional buildExample(String name, Schema schema, Map return example; } - private Optional getExampleFromSchemaAnnotation(String fieldName, Schema schema) { + private Optional getExampleFromSchemaAnnotation(Optional fieldName, Schema schema) { return getExampleValueFromSchemaAnnotation(fieldName, schema, schema.getExample()) .or(() -> getExampleValueFromSchemaAnnotation(fieldName, schema, schema.getDefault())); } - private Optional getExampleValueFromSchemaAnnotation(String fieldName, Schema schema, Object exampleValue) { + private Optional getExampleValueFromSchemaAnnotation( + Optional fieldName, Schema schema, Object exampleValue) { // schema is a map of properties from a nested object, whose example cannot be inferred if (exampleValue == null) { return Optional.empty(); @@ -159,7 +158,7 @@ private Optional getExampleValueFromSchemaAnnotation(String fieldName, Schema * The caller must ensure that the schema has not been visited before to avoid infinite recursion */ private Optional buildExampleFromUnvisitedSchema( - String name, Schema schema, Map definitions, Set visited) { + Optional name, Schema schema, Map definitions, Set visited) { Optional> resolvedSchema = resolveSchemaFromRef(schema, definitions); if (resolvedSchema.isPresent()) { return buildExample(name, resolvedSchema.get(), definitions, visited); @@ -192,7 +191,8 @@ private Optional buildArrayExample(Schema schema, Map definit return exampleValueGenerator .lookupSchemaName(arrayItemSchema) .or(() -> arrayName) - .flatMap(arrayItemName -> buildExample(arrayItemName, arrayItemSchema, definitions, visited)) + .flatMap(arrayItemName -> + buildExample(Optional.of(arrayItemName), arrayItemSchema, definitions, visited)) .map(arrayItem -> exampleValueGenerator.createArrayExample(arrayName, arrayItem)); } @@ -231,7 +231,7 @@ private String getFirstEnumValue(Schema schema) { } private Optional buildFromComposedSchema( - String name, Schema schema, Map definitions, Set visited) { + Optional name, Schema schema, Map definitions, Set visited) { final List schemasAllOf = schema.getAllOf(); final List schemasAnyOf = schema.getAnyOf(); final List schemasOneOf = schema.getOneOf(); @@ -247,7 +247,7 @@ private Optional buildFromComposedSchema( } private Optional buildFromObjectSchema( - String name, Schema schema, Map definitions, Set visited) { + Optional name, Schema schema, Map definitions, Set visited) { final Optional exampleValue; final Map properties = schema.getProperties(); @@ -264,7 +264,7 @@ private Optional buildFromObjectSchema( } private Optional buildMapExample( - String name, Schema additionalProperties, Map definitions, Set visited) { + Optional name, Schema additionalProperties, Map definitions, Set visited) { T object = exampleValueGenerator.startObject(name); Map mapProperties = Map.of(DEFAULT_MAP_KEY_EXAMPLE, additionalProperties); exampleValueGenerator.addPropertyExamples( @@ -275,7 +275,10 @@ private Optional buildMapExample( } private Optional buildFromObjectSchemaWithProperties( - String name, Map properties, Map definitions, Set visited) { + Optional name, + Map properties, + Map definitions, + Set visited) { T object = exampleValueGenerator.startObject(name); exampleValueGenerator.addPropertyExamples( object, buildPropertyExampleListFromSchema(properties, definitions, visited)); @@ -285,7 +288,7 @@ private Optional buildFromObjectSchemaWithProperties( } private Optional buildFromObjectSchemaWithAllOf( - String name, List schemasAllOf, Map definitions, Set visited) { + Optional name, List schemasAllOf, Map definitions, Set visited) { T object = exampleValueGenerator.startObject(name); exampleValueGenerator.addPropertyExamples( object, buildPropertyExampleListFromSchemas(schemasAllOf, definitions, visited)); @@ -305,7 +308,7 @@ private List> buildPropertyExampleListFromSchema( .orElse(propertySchema.getKey()); Optional propertyValue = - buildExample(propertyKey, propertySchema.getValue(), definitions, visited); + buildExample(Optional.of(propertyKey), propertySchema.getValue(), definitions, visited); return propertyValue .map(optionalElem -> new PropertyExample<>(propertyKey, optionalElem)) 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 33b948a88..df4fe3adf 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 @@ -44,7 +44,7 @@ default void initialize() {} Optional createUnknownSchemaStringFormatExample(String schemaFormat); - T startObject(String name); + T startObject(Optional name); default void endObject() {} @@ -54,5 +54,5 @@ default void endObject() {} T createRaw(Object exampleValueString); - T getExampleOrNull(String fieldName, Schema schema, Object example); + T getExampleOrNull(Optional fieldName, Schema schema, Object example); } 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 fef8ca94f..d24d17cc8 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 @@ -97,7 +97,7 @@ public JsonNode createRaw(Object exampleValue) { } @Override - public JsonNode getExampleOrNull(String fieldName, Schema schema, Object example) { + public JsonNode getExampleOrNull(Optional fieldName, Schema schema, Object example) { if (example instanceof JsonNode) { return (JsonNode) example; } @@ -106,7 +106,7 @@ public JsonNode getExampleOrNull(String fieldName, Schema schema, Object example } @Override - public JsonNode startObject(String name) { + public JsonNode startObject(Optional name) { return objectMapper.createObjectNode(); } 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 a6a74813b..7f24c595d 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 @@ -87,12 +87,9 @@ public Optional createBooleanExample(Boolean value, Schema schema) { } @Override - public Element startObject(String name) { - if (name == null) { - throw new IllegalArgumentException("Object name must not be empty"); - } - - return nodeStack.push(document.createElement(name)); + public Element startObject(Optional name) { + return nodeStack.push(document.createElement(name.orElseThrow( + () -> new SchemaWalker.ExampleGeneratingException("There is no name set for Schema")))); } @Override @@ -198,7 +195,7 @@ public Node createRaw(Object exampleValue) { } @Override - public Node getExampleOrNull(String fieldName, Schema schema, Object example) { + public Node getExampleOrNull(Optional fieldName, Schema schema, Object example) { String name = getCacheKey(schema); if (example instanceof Node) { @@ -207,7 +204,10 @@ public Node getExampleOrNull(String fieldName, Schema schema, Object example) { if (exampleCache.containsKey(name)) { Node oldElement = exampleCache.get(name); - Node newElement = modifyElementFromCacheIfNeeded(oldElement, fieldName); + Node newElement = modifyElementFromCacheIfNeeded( + oldElement, + fieldName.orElseThrow( + () -> new SchemaWalker.ExampleGeneratingException("There is no name set for Schema"))); return this.document.importNode(newElement, true); } 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 135e16610..b19069e52 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 @@ -71,7 +71,7 @@ public Optional createBooleanExample(Boolean value, Schema schema) { } @Override - public JsonNode startObject(String name) { + public JsonNode startObject(Optional name) { return this.exampleJsonValueGenerator.startObject(name); } @@ -116,7 +116,7 @@ public JsonNode createRaw(Object exampleValueString) { } @Override - public JsonNode getExampleOrNull(String fieldName, Schema schema, Object example) { + public JsonNode getExampleOrNull(Optional fieldName, Schema schema, Object example) { return this.exampleJsonValueGenerator.getExampleOrNull(fieldName, schema, example); } } diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGeneratorTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGeneratorTest.java index eec84a847..7c0259f81 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGeneratorTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGeneratorTest.java @@ -7,6 +7,8 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -33,11 +35,13 @@ void cacheShouldResolveBySchemaName() { .get(); generator.prepareForSerialization(schema1, example1); - Node cachedExample1 = generator.getExampleOrNull("fieldName1", schema1, "does-not-matter-for-test-1"); + Node cachedExample1 = + generator.getExampleOrNull(Optional.of("fieldName1"), schema1, "does-not-matter-for-test-1"); // when generator.initialize(); - Node exampleFromCache = generator.getExampleOrNull("fieldName2", schema2, "does-not-matter-for-test-2"); + Node exampleFromCache = + generator.getExampleOrNull(Optional.of("fieldName2"), schema2, "does-not-matter-for-test-2"); // then assertThat(exampleFromCache).isNotEqualTo(cachedExample1); @@ -62,11 +66,13 @@ void cacheShouldResolveBySchemaNameAndRenameToWrappingField() { Node example1 = generator.createRaw("aValue"); generator.prepareForSerialization(schema1, example1); - Node cachedExample1 = generator.getExampleOrNull("fieldName1", schema1, "does-not-matter-for-test-1"); + Node cachedExample1 = + generator.getExampleOrNull(Optional.of("fieldName1"), schema1, "does-not-matter-for-test-1"); // when generator.initialize(); - Node exampleFromCache = generator.getExampleOrNull("fieldName2", schema2, "does-not-matter-for-test-2"); + Node exampleFromCache = + generator.getExampleOrNull(Optional.of("fieldName2"), schema2, "does-not-matter-for-test-2"); // then assertThat(((Element) cachedExample1).getTagName()).isEqualTo("fieldName1"); @@ -90,7 +96,7 @@ void cacheShouldStoreExampleBySchemaName() { generator.prepareForSerialization(schema1, example1); generator.initialize(); - Node exampleFromCache = generator.getExampleOrNull("fieldName", schema2, "example-string"); + Node exampleFromCache = generator.getExampleOrNull(Optional.of("fieldName"), schema2, "example-string"); assertThat(exampleFromCache).isNull(); }