Skip to content

Commit

Permalink
refactor(core): push xml-specific handling of array names to ExampleX…
Browse files Browse the repository at this point in the history
…mlValueGenerator

Co-authored-by: Timon Back <timonback@users.noreply.github.com>
  • Loading branch information
sam0r040 and timonback committed Jul 19, 2024
1 parent 30fbff2 commit c5ad5da
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -73,6 +72,8 @@ public R fromSchema(Schema schema, Map<String, Schema> definitions) {
}

private Optional<T> buildExample(String name, Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
log.debug("Building example for schema {}", schema);

Optional<T> exampleValue = getExampleValueFromSchemaAnnotation(schema);
if (exampleValue.isPresent()) {
return exampleValue;
Expand Down Expand Up @@ -164,14 +165,12 @@ 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(arrayNameSupplier, arrayItem));
.map(arrayItem -> exampleValueGenerator.createArrayExample(arrayName, arrayItem));
}

private Optional<T> buildFromStringSchema(Schema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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 @@ -51,7 +50,7 @@ default void endObject() {}

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

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

T createRaw(Object exampleValueString);

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

@Override
public JsonNode createArrayExample(Supplier<String> nameSupplier, JsonNode arrayItem) {
public JsonNode createArrayExample(Optional<String> name, 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 @@ -3,6 +3,7 @@

import io.github.springwolf.core.asyncapi.components.examples.walkers.ExampleValueGenerator;
import io.github.springwolf.core.asyncapi.components.examples.walkers.PropertyExample;
import io.github.springwolf.core.asyncapi.components.examples.walkers.SchemaWalker;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,7 +28,6 @@
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 @@ -155,8 +155,11 @@ public Optional<Node> createUnknownSchemaStringFormatExample(String schemaFormat
}

@Override
public Node createArrayExample(Supplier<String> nameSupplier, Node arrayItem) {
return wrapNode(nameSupplier.get(), arrayItem);
public Node createArrayExample(Optional<String> arrayNameOptional, Node arrayItem) {
return arrayNameOptional
.map(arrayName -> wrapNode(arrayName, arrayItem))
.orElseThrow(() -> new SchemaWalker.ExampleGeneratingException(
"Unable to add array item to array, because the array schema does not have a name"));
}

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

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

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

@Override
Expand Down

0 comments on commit c5ad5da

Please sign in to comment.