Skip to content

Commit

Permalink
fix(core): create example for Map<String, Set<String>> schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Jul 15, 2024
1 parent ef4c28c commit 404b0cd
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -163,15 +164,14 @@ private Optional<T> buildArrayExample(Schema schema, Map<String, Schema> definit
resolveSchemaFromRef(schema.getItems(), definitions).orElse(schema.getItems());

Optional<String> arrayName = exampleValueGenerator.lookupSchemaName(schema);
Supplier<String> 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<T> buildFromStringSchema(Schema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,7 +51,7 @@ default void endObject() {}

void addPropertyExamples(T object, List<PropertyExample<T>> properties);

T createArrayExample(String name, T arrayItem);
T createArrayExample(Supplier<String> nameSupplier, T arrayItem);

T createRaw(Object exampleValueString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonNode, JsonNode> {
Expand Down Expand Up @@ -75,7 +76,7 @@ public Optional<JsonNode> createUnknownSchemaStringFormatExample(String schemaFo
}

@Override
public JsonNode createArrayExample(String name, JsonNode arrayItem) {
public JsonNode createArrayExample(Supplier<String> nameSupplier, JsonNode arrayItem) {
ArrayNode array = objectMapper.createArrayNode();
array.add(arrayItem);
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node, String> {
Expand Down Expand Up @@ -154,8 +155,8 @@ public Optional<Node> createUnknownSchemaStringFormatExample(String schemaFormat
}

@Override
public Node createArrayExample(String name, Node arrayItem) {
return wrapNode(name, arrayItem);
public Node createArrayExample(Supplier<String> nameSupplier, Node arrayItem) {
return wrapNode(nameSupplier.get(), arrayItem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -106,8 +107,8 @@ public Optional<JsonNode> createUnknownSchemaStringFormatExample(String schemaFo
}

@Override
public JsonNode createArrayExample(String name, JsonNode arrayItem) {
return this.exampleJsonValueGenerator.createArrayExample(name, arrayItem);
public JsonNode createArrayExample(Supplier<String> nameSupplier, JsonNode arrayItem) {
return this.exampleJsonValueGenerator.createArrayExample(nameSupplier, arrayItem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Set<String>>
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<object_with_map><key>string</key></object_with_map>");
}

@Test
void object_with_map_and_set() {
// Example: Map<String, Set<String>>
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Set<String>>
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();
Expand Down

0 comments on commit 404b0cd

Please sign in to comment.