From 0ab57181ec9fe78b0b82a42f2b69161128141409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Fri, 8 Mar 2024 15:23:32 +0100 Subject: [PATCH] fix(core): generate correct xml examples for array schemas GH-642 Co-authored-by: Timon Back --- .../components/examples/walkers/DefaultSchemaWalker.java | 9 ++++++--- .../examples/walkers/ExampleValueGenerator.java | 2 +- .../examples/walkers/json/ExampleJsonValueGenerator.java | 2 +- .../examples/walkers/xml/ExampleXmlValueGenerator.java | 4 ++-- .../examples/walkers/yaml/ExampleYamlValueGenerator.java | 4 ++-- .../walkers/DefaultSchemaWalkerJsonIntegrationTest.java | 0 .../walkers/DefaultSchemaWalkerXmlIntegrationTest.java | 6 +++--- .../walkers/DefaultSchemaWalkerYamlIntegrationTest.java | 0 .../DefaultOperationsServiceIntegrationTest.java | 3 +-- .../resources/schemas/xml/array-definitions-xml.json | 2 +- .../resources/schemas/xml/complex-definitions-xml.json | 4 ++-- 11 files changed, 19 insertions(+), 17 deletions(-) rename springwolf-core/src/test/java/io/github/springwolf/core/{ => asyncapi}/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java (100%) rename springwolf-core/src/test/java/io/github/springwolf/core/{ => asyncapi}/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java (98%) rename springwolf-core/src/test/java/io/github/springwolf/core/{ => asyncapi}/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java (100%) rename springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/{oprtations => operations}/DefaultOperationsServiceIntegrationTest.java (96%) 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 17cd8d8d7..e0ffb5a1d 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 @@ -118,10 +118,13 @@ private T getExampleValueFromSchemaAnnotation(Schema schema) { } private T buildArrayExample(Schema schema, Map definitions, Set visited) { - String name = exampleValueGenerator.lookupSchemaName(schema); - T arrayItem = buildExample(name, schema.getItems(), definitions, visited); + Schema arrayItemSchema = + resolveSchemaFromRef(schema.getItems(), definitions).orElse(schema.getItems()); + String arrayItemName = exampleValueGenerator.lookupSchemaName(arrayItemSchema); + T arrayItem = buildExample(arrayItemName, arrayItemSchema, definitions, visited); - return exampleValueGenerator.createArrayExample(arrayItem); + String arrayName = exampleValueGenerator.lookupSchemaName(schema); + return exampleValueGenerator.createArrayExample(arrayName, arrayItem); } private T 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 d7cc363c4..7cd6bc8e2 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 @@ -83,7 +83,7 @@ public interface ExampleValueGenerator { T createUnknownSchemaStringFormatExample(String schemaFormat); - T createArrayExample(T arrayItem); + T createArrayExample(String 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 2b9d06841..4cae510aa 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 @@ -136,7 +136,7 @@ public JsonNode createUnknownSchemaStringFormatExample(String schemaFormat) { } @Override - public JsonNode createArrayExample(JsonNode arrayItem) { + public JsonNode createArrayExample(String 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 905943f66..d343b59cd 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 @@ -190,8 +190,8 @@ public Node createUnknownSchemaStringFormatExample(String schemaFormat) { } @Override - public Node createArrayExample(Node arrayItem) { - return arrayItem; + public Node createArrayExample(String name, Node arrayItem) { + return wrapNode(name, 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 124842269..dc4310ccf 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 @@ -153,8 +153,8 @@ public JsonNode createUnknownSchemaStringFormatExample(String schemaFormat) { } @Override - public JsonNode createArrayExample(JsonNode arrayItem) { - return this.exampleJsonValueGenerator.createArrayExample(arrayItem); + public JsonNode createArrayExample(String name, JsonNode arrayItem) { + return this.exampleJsonValueGenerator.createArrayExample(name, arrayItem); } @Override diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java similarity index 100% rename from springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java rename to springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerJsonIntegrationTest.java diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java similarity index 98% rename from springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java rename to springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java index 35bfedb54..1170421d2 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerXmlIntegrationTest.java @@ -346,9 +346,9 @@ void type_object_array() { String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim(); - // TODO FixeMe Expected - // :"truestring" - assertThat(actual).isEqualTo("truestring"); + assertThat(actual) + .isEqualTo( + "truestring"); } @Test diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java similarity index 100% rename from springwolf-core/src/test/java/io/github/springwolf/core/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java rename to springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/examples/walkers/DefaultSchemaWalkerYamlIntegrationTest.java diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/oprtations/DefaultOperationsServiceIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/operations/DefaultOperationsServiceIntegrationTest.java similarity index 96% rename from springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/oprtations/DefaultOperationsServiceIntegrationTest.java rename to springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/operations/DefaultOperationsServiceIntegrationTest.java index a376152d0..3ee428455 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/oprtations/DefaultOperationsServiceIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/operations/DefaultOperationsServiceIntegrationTest.java @@ -1,10 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 -package io.github.springwolf.core.asyncapi.oprtations; +package io.github.springwolf.core.asyncapi.operations; import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; import io.github.springwolf.asyncapi.v3.model.operation.Operation; import io.github.springwolf.asyncapi.v3.model.operation.OperationAction; -import io.github.springwolf.core.asyncapi.operations.DefaultOperationsService; import io.github.springwolf.core.asyncapi.scanners.OperationsScanner; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; diff --git a/springwolf-core/src/test/resources/schemas/xml/array-definitions-xml.json b/springwolf-core/src/test/resources/schemas/xml/array-definitions-xml.json index ac4d40722..0a66d6df1 100644 --- a/springwolf-core/src/test/resources/schemas/xml/array-definitions-xml.json +++ b/springwolf-core/src/test/resources/schemas/xml/array-definitions-xml.json @@ -9,7 +9,7 @@ } } }, - "examples" : [ "truestring" ] + "examples" : [ "truestring" ] }, "io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$SimpleFoo" : { "type" : "string", diff --git a/springwolf-core/src/test/resources/schemas/xml/complex-definitions-xml.json b/springwolf-core/src/test/resources/schemas/xml/complex-definitions-xml.json index d3912446a..9346f79b5 100644 --- a/springwolf-core/src/test/resources/schemas/xml/complex-definitions-xml.json +++ b/springwolf-core/src/test/resources/schemas/xml/complex-definitions-xml.json @@ -28,7 +28,7 @@ "type" : "string" } }, - "examples" : [ "true1.1
2015-07-20T15:49:04-07:00
1.10YmFzZTY0LWV4YW1wbGU=0stringstring3fa85f64-5717-4562-b3fc-2c963f66afa6string
" ] + "examples" : [ "true1.1
2015-07-20T15:49:04-07:00
1.10YmFzZTY0LWV4YW1wbGU=0stringstring3fa85f64-5717-4562-b3fc-2c963f66afa6string
" ] }, "io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$ComplexFoo$Nested" : { "type" : "string", @@ -71,7 +71,7 @@ "format" : "uuid" } }, - "examples" : [ "YmFzZTY0LWV4YW1wbGU=0stringstring3fa85f64-5717-4562-b3fc-2c963f66afa6" ] + "examples" : [ "YmFzZTY0LWV4YW1wbGU=0stringstring3fa85f64-5717-4562-b3fc-2c963f66afa6" ] }, "io.github.springwolf.core.asyncapi.components.DefaultXmlComponentsServiceTest$ComplexFoo$Nested$Cyclic" : { "type" : "string",