Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): push xml-specific handling of array names to ExampleXmlValueGenerator #857

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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