From bbcb52a00a5a888c6a23dc40b6b7cfe8f3b5e642 Mon Sep 17 00:00:00 2001 From: Timon Back Date: Wed, 14 Feb 2024 20:13:15 +0100 Subject: [PATCH] feat(core): build allOf example with all available fields --- .../schemas/example/ExampleJsonGenerator.java | 12 +++++-- .../example/ExampleJsonGeneratorTest.java | 32 +++++++++++++++++++ .../schemas/annotation-definitions.json | 8 +++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGenerator.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGenerator.java index bec66c230..74b8faf0a 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGenerator.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGenerator.java @@ -216,8 +216,16 @@ private static JsonNode handleObject(Schema schema, Map definiti if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) { List schemas = schema.getAllOf(); - // Open: Handle properties of all schemas, not only the first one - return buildSchemaInternal(schemas.get(0), definitions, visited); + + ObjectNode combinedNode = objectMapper.createObjectNode(); + schemas.stream() + .map(s -> buildSchemaInternal(s, definitions, visited)) + .filter(JsonNode::isObject) + .map(JsonNode::fields) + .forEach(fields -> + fields.forEachRemaining(entry -> combinedNode.set(entry.getKey(), entry.getValue()))); + + return combinedNode; } if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) { List schemas = schema.getAnyOf(); diff --git a/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGeneratorTest.java b/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGeneratorTest.java index 13854e02f..9985c8686 100644 --- a/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGeneratorTest.java +++ b/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonGeneratorTest.java @@ -318,6 +318,38 @@ void object_with_anyOf() { assertThat(actual).isEqualTo("{\"anyOfField\":\"string\"}"); } + @Test + void object_with_oneOf() { + ObjectSchema compositeSchema = new ObjectSchema(); + + Schema propertySchema = new ObjectSchema(); + propertySchema.setOneOf(List.of(new StringSchema(), new NumberSchema())); + compositeSchema.addProperty("oneOfField", propertySchema); + + String actual = ExampleJsonGenerator.buildSchema(compositeSchema, Map.of("Nested", propertySchema)); + + assertThat(actual).isEqualTo("{\"oneOfField\":\"string\"}"); + } + + @Test + void object_with_allOf() { + ObjectSchema compositeSchema = new ObjectSchema(); + + ObjectSchema schema1 = new ObjectSchema(); + schema1.setProperties(Map.of("field1", new StringSchema())); + ObjectSchema schema2 = new ObjectSchema(); + schema2.setProperties(Map.of("field2", new NumberSchema())); + StringSchema skippedSchemaSinceObjectIsRequired = new StringSchema(); + + Schema propertySchema = new ObjectSchema(); + propertySchema.setAllOf(List.of(schema1, schema2, skippedSchemaSinceObjectIsRequired)); + compositeSchema.addProperty("allOfField", propertySchema); + + String actual = ExampleJsonGenerator.buildSchema(compositeSchema, Map.of("Nested", propertySchema)); + + assertThat(actual).isEqualTo("{\"allOfField\":{\"field1\":\"string\",\"field2\":1.1}}"); + } + @Test void schema_with_problematic_object_toString_example() { ObjectSchema schema = new ObjectSchema(); diff --git a/springwolf-core/src/test/resources/schemas/annotation-definitions.json b/springwolf-core/src/test/resources/schemas/annotation-definitions.json index b78172903..2b988f719 100644 --- a/springwolf-core/src/test/resources/schemas/annotation-definitions.json +++ b/springwolf-core/src/test/resources/schemas/annotation-definitions.json @@ -3,7 +3,9 @@ "type" : "object", "examples" : [ { "firstOne" : "string", - "secondOne" : "string" + "secondOne" : "string", + "firstTwo" : 0, + "secondTwo" : true } ], "allOf" : [ { "$ref" : "#/components/schemas/ImplementationOne" @@ -85,7 +87,9 @@ "examples" : [ { "allOf" : { "firstOne" : "string", - "secondOne" : "string" + "secondOne" : "string", + "firstTwo" : 0, + "secondTwo" : true }, "anyOf" : { "firstOne" : "string",