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 9ca26fd61..09106a012 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,6 +20,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; @Slf4j @RequiredArgsConstructor @@ -163,15 +164,14 @@ 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( - arrayName.orElseThrow( - () -> new ExampleGeneratingException("Array schema does not have a name: " + schema)), - arrayItem)); + .map(arrayItem -> exampleValueGenerator.createArrayExample(arrayNameSupplier, 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 370c8335b..7748d4a16 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,6 +5,7 @@ import java.util.List; import java.util.Optional; +import java.util.function.Supplier; /** * Provides the building blocks to generate an example @@ -50,7 +51,7 @@ default void endObject() {} void addPropertyExamples(T object, List> properties); - T createArrayExample(String name, T arrayItem); + T createArrayExample(Supplier nameSupplier, 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 aef73c92a..18ab08b57 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,6 +20,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; @Slf4j public class ExampleJsonValueGenerator implements ExampleValueGenerator { @@ -75,7 +76,7 @@ public Optional createUnknownSchemaStringFormatExample(String schemaFo } @Override - public JsonNode createArrayExample(String name, JsonNode arrayItem) { + public JsonNode createArrayExample(Supplier nameSupplier, 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 41a46b4d7..e851700b4 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 @@ -27,6 +27,7 @@ import java.util.Optional; import java.util.Set; import java.util.Stack; +import java.util.function.Supplier; @Slf4j public class ExampleXmlValueGenerator implements ExampleValueGenerator { @@ -154,8 +155,8 @@ public Optional createUnknownSchemaStringFormatExample(String schemaFormat } @Override - public Node createArrayExample(String name, Node arrayItem) { - return wrapNode(name, arrayItem); + public Node createArrayExample(Supplier nameSupplier, Node arrayItem) { + return wrapNode(nameSupplier.get(), arrayItem); } @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 daf3c2ff4..e5b63deef 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,6 +15,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; @Slf4j @RequiredArgsConstructor @@ -106,8 +107,8 @@ public Optional createUnknownSchemaStringFormatExample(String schemaFo } @Override - public JsonNode createArrayExample(String name, JsonNode arrayItem) { - return this.exampleJsonValueGenerator.createArrayExample(name, arrayItem); + public JsonNode createArrayExample(Supplier nameSupplier, JsonNode arrayItem) { + return this.exampleJsonValueGenerator.createArrayExample(nameSupplier, arrayItem); } @Override diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java index 0263d6441..8e81e2100 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java @@ -451,12 +451,32 @@ void object_with_map(TestInfo testInfo) throws JsonProcessingException { Schema propertySchema = new StringSchema(); mapSchema.setAdditionalProperties(propertySchema); - JsonNode actual = jsonSchemaWalker.fromSchema(mapSchema, Map.of("Nested", propertySchema)); + JsonNode actual = jsonSchemaWalker.fromSchema(mapSchema, Map.of()); String actualString = jsonMapper.writeValueAsString(actual); assertThat(actualString).isEqualTo("{\"key\":\"string\"}"); } + @Test + void object_with_map_and_set(TestInfo testInfo) throws JsonProcessingException { + // Example: Map> + Schema objectSchema = new ObjectSchema(); + objectSchema.setName("objectName"); + objectSchema.setProperties(Map.of("field1", new StringSchema())); + + Schema arraySchema = new ArraySchema(); + arraySchema.setItems(objectSchema); + + MapSchema mapSchema = new MapSchema(); + mapSchema.setName(testInfo.getDisplayName()); + mapSchema.setAdditionalProperties(arraySchema); + + JsonNode actual = jsonSchemaWalker.fromSchema(mapSchema, Map.of()); + String actualString = jsonMapper.writeValueAsString(actual); + + assertThat(actualString).isEqualTo("{\"key\":[{\"field1\":\"string\"}]}"); + } + @Test void schema_with_problematic_object_toString_example(TestInfo testInfo) throws JsonProcessingException { ObjectSchema schema = new ObjectSchema(); diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java index 8027fee1d..3ae46d6cc 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java @@ -516,10 +516,28 @@ void object_with_map() { Schema propertySchema = new StringSchema(); mapSchema.setAdditionalProperties(propertySchema); - String actual = xmlSchemaWalker.fromSchema(mapSchema, Map.of("Nested", propertySchema)); + String actual = xmlSchemaWalker.fromSchema(mapSchema, Map.of()); assertThat(actual).isEqualTo("string"); } + @Test + void object_with_map_and_set() { + // Example: Map> + Schema objectSchema = new ObjectSchema(); + objectSchema.setName("objectName"); + objectSchema.setProperties(Map.of("field1", new StringSchema())); + + Schema arraySchema = new ArraySchema(); + arraySchema.setItems(objectSchema); + + MapSchema mapSchema = new MapSchema(); + mapSchema.setName("object_with_map_and_set"); + mapSchema.setAdditionalProperties(arraySchema); + + String actual = xmlSchemaWalker.fromSchema(mapSchema, Map.of()); + assertThat(actual).isNull(); + } + @Test void schema_with_problematic_object_toString_example() { ObjectSchema schema = new ObjectSchema(); diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java index 477b91ea0..628f025b8 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java @@ -499,11 +499,30 @@ void object_with_map(TestInfo testInfo) { Schema propertySchema = new StringSchema(); mapSchema.setAdditionalProperties(propertySchema); - String actualString = jsonSchemaWalker.fromSchema(mapSchema, Map.of("Nested", propertySchema)); + String actualString = jsonSchemaWalker.fromSchema(mapSchema, Map.of()); assertThat(actualString).isEqualTo("key: \"string\"\n"); } + @Test + void object_with_map_and_set(TestInfo testInfo) { + // Example: Map> + Schema objectSchema = new ObjectSchema(); + objectSchema.setName("objectName"); + objectSchema.setProperties(Map.of("field1", new StringSchema())); + + Schema arraySchema = new ArraySchema(); + arraySchema.setItems(objectSchema); + + MapSchema mapSchema = new MapSchema(); + mapSchema.setName(testInfo.getDisplayName()); + mapSchema.setAdditionalProperties(arraySchema); + + String actualString = jsonSchemaWalker.fromSchema(mapSchema, Map.of()); + + assertThat(actualString).isEqualTo("key:\n- field1: \"string\"\n"); + } + @Test void schema_with_problematic_object_toString_example(TestInfo testInfo) { ObjectSchema schema = new ObjectSchema();