From 019fdb33399251553847f32d097605e02385c01e Mon Sep 17 00:00:00 2001 From: Timon Back Date: Sun, 28 Jul 2024 22:34:28 +0200 Subject: [PATCH] feat(core): handle Schema annotation defaultValue field --- .../examples/walkers/DefaultSchemaWalker.java | 9 ++-- ...efaultSchemaWalkerJsonIntegrationTest.java | 48 +++++++++++++++++ ...DefaultSchemaWalkerXmlIntegrationTest.java | 44 ++++++++++++++++ ...efaultSchemaWalkerYamlIntegrationTest.java | 52 +++++++++++++++++++ 4 files changed, 150 insertions(+), 3 deletions(-) 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 b36b68f6a..b157e8b82 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 @@ -74,7 +74,7 @@ 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); + Optional exampleValue = getExampleFromSchemaAnnotation(schema); if (exampleValue.isPresent()) { return exampleValue; } @@ -88,9 +88,12 @@ private Optional buildExample(String name, Schema schema, Map return example; } - private Optional getExampleValueFromSchemaAnnotation(Schema schema) { - Object exampleValue = schema.getExample(); + private Optional getExampleFromSchemaAnnotation(Schema schema) { + return getExampleValueFromSchemaAnnotation(schema, schema.getExample()) + .or(() -> getExampleValueFromSchemaAnnotation(schema, schema.getDefault())); + } + private Optional getExampleValueFromSchemaAnnotation(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(); 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 8e81e2100..0565902cd 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 @@ -101,6 +101,18 @@ void type_boolean(TestInfo testInfo) throws JsonProcessingException { assertThat(actualString).isEqualTo("true"); } + @Test + void type_boolean_default_set(TestInfo testInfo) throws JsonProcessingException { + BooleanSchema schema = new BooleanSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault(Boolean.FALSE); + + JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap()); + String actualString = jsonMapper.writeValueAsString(actual); + + assertThat(actualString).isEqualTo("false"); + } + @Test void type_boolean_example_set(TestInfo testInfo) throws JsonProcessingException { BooleanSchema schema = new BooleanSchema(); @@ -124,6 +136,18 @@ void type_integer(TestInfo testInfo) throws JsonProcessingException { assertThat(actualString).isEqualTo("0"); } + @Test + void type_integer_default_set(TestInfo testInfo) throws JsonProcessingException { + IntegerSchema schema = new IntegerSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault(Integer.parseInt("123")); + + JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap()); + String actualString = jsonMapper.writeValueAsString(actual); + + assertThat(actualString).isEqualTo("123"); + } + @Test void type_integer_example_set(TestInfo testInfo) throws JsonProcessingException { IntegerSchema schema = new IntegerSchema(); @@ -172,6 +196,18 @@ void type_number_format_double(TestInfo testInfo) throws JsonProcessingException assertThat(actualString).isEqualTo("1.1"); } + @Test + void type_number_default_set(TestInfo testInfo) throws JsonProcessingException { + Schema schema = new NumberSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault(new BigDecimal("123.45")); + + JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap()); + String actualString = jsonMapper.writeValueAsString(actual); + + assertThat(actualString).isEqualTo("123.45"); + } + @Test void type_number_example_set(TestInfo testInfo) throws JsonProcessingException { Schema schema = new NumberSchema(); @@ -195,6 +231,18 @@ void type_string(TestInfo testInfo) throws JsonProcessingException { assertThat(actualString).isEqualTo("\"string\""); } + @Test + void type_string_default_set(TestInfo testInfo) throws JsonProcessingException { + StringSchema schema = new StringSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault("custom-example-value"); + + JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap()); + String actualString = jsonMapper.writeValueAsString(actual); + + assertThat(actualString).isEqualTo("\"custom-example-value\""); + } + @Test void type_string_example_set(TestInfo testInfo) throws JsonProcessingException { StringSchema schema = new StringSchema(); 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 3ae46d6cc..5d878ac24 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 @@ -107,6 +107,17 @@ void type_boolean() { assertThat(actual).isEqualTo("true"); } + @Test + void type_boolean_default_set() { + BooleanSchema schema = new BooleanSchema(); + schema.setDefault(Boolean.FALSE); + schema.setName("type_boolean_default_set"); + + String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim(); + + assertThat(actual).isEqualTo("false"); + } + @Test void type_boolean_example_set() { BooleanSchema schema = new BooleanSchema(); @@ -128,6 +139,17 @@ void type_integer() { assertThat(actual).isEqualTo("0"); } + @Test + void type_integer_default_set() { + IntegerSchema schema = new IntegerSchema(); + schema.setDefault(Integer.parseInt("123")); + schema.setName("type_integer_default_set"); + + String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim(); + + assertThat(actual).isEqualTo("123"); + } + @Test void type_integer_example_set() { IntegerSchema schema = new IntegerSchema(); @@ -172,6 +194,17 @@ void type_number_format_double() { assertThat(actual).isEqualTo("1.1"); } + @Test + void type_number_default_set() { + Schema schema = new NumberSchema(); + schema.setDefault(new BigDecimal("123.45")); + schema.setName("type_number_default_set"); + + String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim(); + + assertThat(actual).isEqualTo("123.45"); + } + @Test void type_number_example_set() { Schema schema = new NumberSchema(); @@ -193,6 +226,17 @@ void type_string() { assertThat(actual).isEqualTo("string"); } + @Test + void type_string_default_set() { + StringSchema schema = new StringSchema(); + schema.setDefault("custom-example-value"); + schema.setName("type_string_default_set"); + + String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim(); + + assertThat(actual).isEqualTo("custom-example-value"); + } + @Test void type_string_example_set() { StringSchema schema = new StringSchema(); 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 628f025b8..dd1ed2e7b 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 @@ -105,6 +105,19 @@ void type_boolean(TestInfo testInfo) { """); } + @Test + void type_boolean_default_set(TestInfo testInfo) { + BooleanSchema schema = new BooleanSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault(Boolean.FALSE); + + String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap()); + + assertThat(actualString).isEqualTo(""" + false + """); + } + @Test void type_boolean_example_set(TestInfo testInfo) { BooleanSchema schema = new BooleanSchema(); @@ -130,6 +143,19 @@ void type_integer(TestInfo testInfo) { """); } + @Test + void type_integer_default_set(TestInfo testInfo) { + IntegerSchema schema = new IntegerSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault(Integer.parseInt("123")); + + String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap()); + + assertThat(actualString).isEqualTo(""" + 123 + """); + } + @Test void type_integer_example_set(TestInfo testInfo) { IntegerSchema schema = new IntegerSchema(); @@ -182,6 +208,19 @@ void type_number_format_double(TestInfo testInfo) { """); } + @Test + void type_number_default_set(TestInfo testInfo) { + Schema schema = new NumberSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault(new BigDecimal("123.45")); + + String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap()); + + assertThat(actualString).isEqualTo(""" + 123.45 + """); + } + @Test void type_number_example_set(TestInfo testInfo) { Schema schema = new NumberSchema(); @@ -207,6 +246,19 @@ void type_string(TestInfo testInfo) { """); } + @Test + void type_string_default_set(TestInfo testInfo) { + StringSchema schema = new StringSchema(); + schema.setName(testInfo.getDisplayName()); + schema.setDefault("custom-example-value"); + + String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap()); + + assertThat(actualString).isEqualTo(""" + "custom-example-value" + """); + } + @Test void type_string_example_set(TestInfo testInfo) { StringSchema schema = new StringSchema();