Skip to content

Commit

Permalink
feat(core): build allOf example with all available fields
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Feb 14, 2024
1 parent 6d90cb3 commit bbcb52a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,16 @@ private static JsonNode handleObject(Schema schema, Map<String, Schema> definiti

if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) {
List<Schema> 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<Schema> schemas = schema.getAnyOf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"type" : "object",
"examples" : [ {
"firstOne" : "string",
"secondOne" : "string"
"secondOne" : "string",
"firstTwo" : 0,
"secondTwo" : true
} ],
"allOf" : [ {
"$ref" : "#/components/schemas/ImplementationOne"
Expand Down Expand Up @@ -85,7 +87,9 @@
"examples" : [ {
"allOf" : {
"firstOne" : "string",
"secondOne" : "string"
"secondOne" : "string",
"firstTwo" : 0,
"secondTwo" : true
},
"anyOf" : {
"firstOne" : "string",
Expand Down

0 comments on commit bbcb52a

Please sign in to comment.