diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/configuration/properties/SpringwolfConfigConstants.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/configuration/properties/SpringwolfConfigConstants.java index e37124bdf..364994cc9 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/configuration/properties/SpringwolfConfigConstants.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/configuration/properties/SpringwolfConfigConstants.java @@ -27,7 +27,5 @@ public class SpringwolfConfigConstants { public static final String SPRINGWOLF_SCANNER_PRODUCER_DATA_ENABLED = SPRINGWOLF_SCANNER_PREFIX + ".producer-data" + ENABLED; - public static final String SPRINGWOLF_SCHEMA_EXAMPLE_GENERATOR = SPRINGWOLF_CONFIG_PREFIX + ".example-generator"; - private SpringwolfConfigConstants() {} } diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/DefaultComponentsService.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/DefaultComponentsService.java index ea1290cb7..ab2e00f95 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/DefaultComponentsService.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/DefaultComponentsService.java @@ -92,8 +92,7 @@ public String registerSchema(Class type, String contentType) { String schemaName = getSchemaName(type, schemas); preProcessSchemas(schemas, schemaName, type); - // TODO Fix Me putIfAbsent - this.schemas.putAll(schemas); + schemas.forEach(this.schemas::putIfAbsent); schemas.values().forEach(schema -> postProcessSchema(schema, actualContentType)); return schemaName; diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/DefaultSchemaWalker.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/DefaultSchemaWalker.java index 31d466907..275ba4479 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/DefaultSchemaWalker.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/DefaultSchemaWalker.java @@ -1,12 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 package io.github.stavshamir.springwolf.schemas.example; +import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageReference; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import java.util.HashSet; import java.util.List; @@ -15,11 +14,8 @@ import java.util.Optional; import java.util.Set; -import static io.github.stavshamir.springwolf.configuration.properties.SpringwolfConfigConstants.SPRINGWOLF_SCHEMA_EXAMPLE_GENERATOR; - // TODO: Einfach Yaml generieren? @Slf4j -@ConditionalOnProperty(name = SPRINGWOLF_SCHEMA_EXAMPLE_GENERATOR, havingValue = "buildin-json", matchIfMissing = true) @RequiredArgsConstructor public class DefaultSchemaWalker implements SchemaWalker { @@ -37,7 +33,7 @@ public R fromSchema(Schema schema, Map definitions) { T generatedExample = buildSchemaInternal(schema.getName(), schema, definitions, new HashSet<>()); - return exampleValueGenerator.serializeIfNeeded(schema.getName(), generatedExample); + return exampleValueGenerator.prepareForSerialization(schema.getName(), generatedExample); } catch (ExampleGeneratingException ex) { log.info("Failed to build example for schema {}", schema.getName(), ex); } @@ -57,12 +53,12 @@ private T buildSchemaInternal(String name, Schema schema, Map de String type = schema.getType(); return switch (type) { - case "array" -> handleArraySchema(schema, definitions, visited); // Handle array schema + case "array" -> handleArraySchema(schema, definitions, visited); case "boolean" -> exampleValueGenerator.createBooleanExample(); case "integer" -> exampleValueGenerator.createIntegerExample(); case "number" -> exampleValueGenerator.createDoubleExample(); - case "object" -> handleObject(name, schema, definitions, visited); // Handle object schema - case "string" -> handleStringSchema(schema); // Handle string schema + case "object" -> handleObject(name, schema, definitions, visited); + case "string" -> handleStringSchema(schema); default -> exampleValueGenerator.generateUnknownSchemaStringTypeExample(type); }; } @@ -120,7 +116,6 @@ private T getExampleValueFromSchemaAnnotation(Schema schema) { } private T handleArraySchema(Schema schema, Map definitions, Set visited) { - T arrayItem = buildSchemaInternal(schema.getName(), schema.getItems(), definitions, visited); return exampleValueGenerator.generateArrayExample(arrayItem); @@ -133,7 +128,6 @@ private T handleStringSchema(Schema schema) { } String format = schema.getFormat(); - if (format == null) { return exampleValueGenerator.generateStringExample(); } @@ -165,10 +159,12 @@ private T handleObject(String name, Schema schema, Map definitio // TODO Handle missconfiguration when object schema has no name Map properties = schema.getProperties(); if (properties != null) { - if (!visited.contains(schema)) { visited.add(schema); - T example = handleObjectProperties(name, properties, definitions, visited); + + List> propertyList = buildPropertyListFromSchema(properties, definitions, visited); + T example = exampleValueGenerator.createObjectExample(name, propertyList); + visited.remove(schema); return example; } @@ -177,14 +173,7 @@ private T handleObject(String name, Schema schema, Map definitio if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) { List schemas = schema.getAllOf(); - List mergedPropertiesOfSchemas = mergePropertiesOfSchemas(schemas, definitions); - - List> mergedProperties = mergedPropertiesOfSchemas.stream() - .map((mergedProperty) -> new PropertyExample<>( - mergedProperty.name(), - buildSchemaInternal(mergedProperty.name(), mergedProperty.schema(), definitions, visited))) - .toList(); - + List> mergedProperties = buildPropertyListFromSchemas(schemas, definitions, visited); return exampleValueGenerator.createObjectExample(name, mergedProperties); } if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) { @@ -202,10 +191,10 @@ private T handleObject(String name, Schema schema, Map definitio return exampleValueGenerator.createEmptyObjectExample(); } - private T handleObjectProperties( - String name, Map properties, Map definitions, Set visited) { - - List> propertyList = properties.entrySet().stream() + private List> buildPropertyListFromSchema( + Map properties, Map definitions, Set visited) { + return properties.entrySet().stream() + // TODO: comparing to sort? Remove or add to buildPropertyListFromSchemas .sorted(Map.Entry.comparingByKey()) .map(entry -> { String propertyKey = entry.getKey(); @@ -213,14 +202,23 @@ private T handleObjectProperties( return new PropertyExample<>(propertyKey, propertyValue); }) .toList(); + } - return exampleValueGenerator.createObjectExample(name, propertyList); + private List> buildPropertyListFromSchemas( + List schemas, Map definitions, Set visited) { + return schemas.stream() + .map(schema -> resolveSchemaFromRefIfAny(schema, definitions).orElse(schema)) + .map(Schema::getProperties) + .filter(Objects::nonNull) + .flatMap(propertiesFromSchema -> + buildPropertyListFromSchema(propertiesFromSchema, definitions, visited).stream()) + .toList(); } private Optional> resolveSchemaFromRefIfAny(Schema schema, Map definitions) { String ref = schema.get$ref(); if (ref != null) { - String schemaName = StringUtils.substringAfterLast(ref, "/"); + String schemaName = MessageReference.extractRefName(ref); Schema resolvedSchema = definitions.get(schemaName); if (resolvedSchema == null) { throw new ExampleGeneratingException("Missing schema during example json generation: " + schemaName); @@ -229,14 +227,4 @@ private Optional> resolveSchemaFromRefIfAny(Schema schema, Map mergePropertiesOfSchemas(List schemas, Map definitions) { - return schemas.stream() - .map(schema -> resolveSchemaFromRefIfAny(schema, definitions).orElse(schema)) - .map(Schema::getProperties) - .filter(Objects::nonNull) - .flatMap(propertiesFromSchema -> propertiesFromSchema.entrySet().stream()) - .map(entry -> new PropertySchema(entry.getKey(), entry.getValue())) - .toList(); - } } diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonValueGenerator.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonValueGenerator.java index a14e68c97..1a3143528 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleJsonValueGenerator.java @@ -13,40 +13,21 @@ import io.swagger.v3.core.util.Json; import jakarta.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import java.util.List; +import java.util.Set; @Slf4j public class ExampleJsonValueGenerator implements ExampleValueGenerator { - private static final BooleanNode DEFAULT_BOOLEAN_EXAMPLE = BooleanNode.TRUE; - + private static final Set SUPPORTED_CONTENT_TYPES = Set.of("application/json"); + private static final BooleanNode DEFAULT_BOOLEAN_EXAMPLE = + BooleanNode.valueOf(ExampleValueGenerator.DEFAULT_BOOLEAN_EXAMPLE); private static final ObjectMapper objectMapper = Json.mapper(); - private static final Double DEFAULT_NUMBER_EXAMPLE = 1.1; - private static final Integer DEFAULT_INTEGER_EXAMPLE = 0; - - private static final String DEFAULT_DATE_EXAMPLE = "2015-07-20"; - private static final String DEFAULT_DATE_TIME_EXAMPLE = "2015-07-20T15:49:04-07:00"; - private static final String DEFAULT_PASSWORD_EXAMPLE = "string-password"; - private static final String DEFAULT_BYTE_EXAMPLE = "YmFzZTY0LWV4YW1wbGU="; - private static final String DEFAULT_BINARY_EXAMPLE = - "0111010001100101011100110111010000101101011000100110100101101110011000010110010001111001"; - private static final String DEFAULT_STRING_EXAMPLE = "string"; - private static final String DEFAULT_EMAIL_EXAMPLE = "example@example.com"; - private static final String DEFAULT_UUID_EXAMPLE = "3fa85f64-5717-4562-b3fc-2c963f66afa6"; - - private static String DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(String type) { - return "unknown schema type: " + type; - } - - private static String DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(String format) { - return "unknown string schema format: " + format; - } @Override public boolean canHandle(String contentType) { - return (StringUtils.equals(contentType, "application/json")); + return SUPPORTED_CONTENT_TYPES.contains(contentType); } @Override @@ -138,12 +119,12 @@ public JsonNode generateUuidExample() { @Override public JsonNode generateUnknownSchemaStringTypeExample(String type) { - return JsonNodeFactory.instance.textNode(DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(type)); + return JsonNodeFactory.instance.textNode("unknown schema type: " + type); } @Override public JsonNode generateUnknownSchemaFormatExample(String schemaFormat) { - return JsonNodeFactory.instance.textNode(DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(schemaFormat)); + return JsonNodeFactory.instance.textNode("unknown string schema format: " + schemaFormat); } @Override @@ -154,7 +135,7 @@ public JsonNode generateArrayExample(JsonNode arrayItem) { } @Override - public JsonNode serializeIfNeeded(String name, JsonNode exampleObject) { + public JsonNode prepareForSerialization(String name, JsonNode exampleObject) { return exampleObject; } diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleValueGenerator.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleValueGenerator.java index 9416b956e..fbee27e0a 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleValueGenerator.java @@ -3,12 +3,42 @@ import java.util.List; +/** + * Provides the building blocks to generate an example + * + * @param The internal representation of a node like object to compose the example + * @param The serializable representation of the example + */ interface ExampleValueGenerator { + Boolean DEFAULT_BOOLEAN_EXAMPLE = true; + + String DEFAULT_STRING_EXAMPLE = "string"; + Integer DEFAULT_INTEGER_EXAMPLE = 0; + Double DEFAULT_NUMBER_EXAMPLE = 1.1; + + String DEFAULT_DATE_EXAMPLE = "2015-07-20"; + String DEFAULT_DATE_TIME_EXAMPLE = "2015-07-20T15:49:04-07:00"; + String DEFAULT_PASSWORD_EXAMPLE = "string-password"; + String DEFAULT_BYTE_EXAMPLE = "YmFzZTY0LWV4YW1wbGU="; + String DEFAULT_BINARY_EXAMPLE = + "0111010001100101011100110111010000101101011000100110100101101110011000010110010001111001"; + + String DEFAULT_EMAIL_EXAMPLE = "example@example.com"; + String DEFAULT_UUID_EXAMPLE = "3fa85f64-5717-4562-b3fc-2c963f66afa6"; + boolean canHandle(String contentType); + /** + * Some internal representation need to be initialized per Schema + */ void initialize(); + /** + * @return The serializable representation of the example (object for json & yaml, string for others) + */ + R prepareForSerialization(String name, T exampleObject); + T createIntegerExample(Integer value); T createDoubleExample(Double value); @@ -51,8 +81,6 @@ interface ExampleValueGenerator { T generateArrayExample(T arrayItem); - R serializeIfNeeded(String name, T exampleObject); - T createRaw(Object exampleValueString); T exampleOrNull(String name, Object example); diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleXmlValueGenerator.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleXmlValueGenerator.java index 7323dd08a..0a312a51f 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleXmlValueGenerator.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/ExampleXmlValueGenerator.java @@ -2,7 +2,6 @@ package io.github.stavshamir.springwolf.schemas.example; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -19,51 +18,27 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; @Slf4j public class ExampleXmlValueGenerator implements ExampleValueGenerator { - private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + private final Set SUPPORTED_CONTENT_TYPES = Set.of("text/xml", "application/xml"); private final ExampleXmlValueSerializer exampleXmlValueSerializer; private Document document; + private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); private final Map exampleCache = new HashMap<>(); - private static final Boolean DEFAULT_BOOLEAN_EXAMPLE = true; - - private static final String DEFAULT_STRING_EXAMPLE = "string"; - - private static final Integer DEFAULT_INTEGER_EXAMPLE = 0; - - private static final Double DEFAULT_NUMBER_EXAMPLE = 1.1; - - private static final String DEFAULT_DATE_EXAMPLE = "2015-07-20"; - private static final String DEFAULT_DATE_TIME_EXAMPLE = "2015-07-20T15:49:04-07:00"; - private static final String DEFAULT_PASSWORD_EXAMPLE = "string-password"; - private static final String DEFAULT_BYTE_EXAMPLE = "YmFzZTY0LWV4YW1wbGU="; - private static final String DEFAULT_BINARY_EXAMPLE = - "0111010001100101011100110111010000101101011000100110100101101110011000010110010001111001"; - - private static final String DEFAULT_EMAIL_EXAMPLE = "example@example.com"; - private static final String DEFAULT_UUID_EXAMPLE = "3fa85f64-5717-4562-b3fc-2c963f66afa6"; - public ExampleXmlValueGenerator(ExampleXmlValueSerializer exampleXmlValueSerializer) { this.exampleXmlValueSerializer = exampleXmlValueSerializer; } - private static String DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(String type) { - return "unknown schema type: " + type; - } - - private static String DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(String format) { - return "unknown string schema format: " + format; - } - @Override public boolean canHandle(String contentType) { - return (StringUtils.equals(contentType, "text/xml") || StringUtils.equals(contentType, "application/xml")); + return SUPPORTED_CONTENT_TYPES.contains(contentType); } @Override @@ -103,7 +78,7 @@ public Node createIntegerExample() { @Override public Node createObjectExample(String name, List> properties) { if (name == null) { - throw new IllegalArgumentException("Object Name must not be empty"); + throw new IllegalArgumentException("Object name must not be empty"); } try { Element rootElement = document.createElement(name); @@ -193,12 +168,12 @@ public Node generateEnumExample(String anEnumValue) { @Override public Node generateUnknownSchemaStringTypeExample(String schemaType) { - return document.createTextNode(DEFAULT_UNKNOWN_SCHEMA_EXAMPLE(schemaType)); + return document.createTextNode("unknown schema type: " + schemaType); } @Override public Node generateUnknownSchemaFormatExample(String schemaFormat) { - return document.createTextNode(DEFAULT_UNKNOWN_SCHEMA_STRING_EXAMPLE(schemaFormat)); + return document.createTextNode("unknown string schema format: " + schemaFormat); } @Override @@ -207,7 +182,7 @@ public Node generateArrayExample(Node arrayItem) { } @Override - public String serializeIfNeeded(String name, Node exampleObject) { + public String prepareForSerialization(String name, Node exampleObject) { final Node objectToWrite; if (exampleObject instanceof Element) { objectToWrite = exampleObject; @@ -217,7 +192,7 @@ public String serializeIfNeeded(String name, Node exampleObject) { try { document.appendChild(objectToWrite); String xml = exampleXmlValueSerializer.writeDocumentAsXmlString(document); - log.info("name {} -> xml: {}", name, xml); + log.debug("name {} -> xml: {}", name, xml); exampleCache.putIfAbsent(name, exampleObject); return xml; diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertyExample.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertyExample.java index 7b893e415..c3f5f5e6c 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertyExample.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertyExample.java @@ -1,4 +1,4 @@ // SPDX-License-Identifier: Apache-2.0 package io.github.stavshamir.springwolf.schemas.example; -public record PropertyExample(String name, T example) {} +record PropertyExample(String name, T example) {} diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertySchema.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertySchema.java deleted file mode 100644 index f707b768a..000000000 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/PropertySchema.java +++ /dev/null @@ -1,6 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -package io.github.stavshamir.springwolf.schemas.example; - -import io.swagger.v3.oas.models.media.Schema; - -public record PropertySchema(String name, Schema schema) {} diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalker.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalker.java index 4fdeffedc..f07877dd2 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalker.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalker.java @@ -7,9 +7,7 @@ import java.util.Map; /** - * Builds an example that is embedded into {@link Schema#setExample(Object)} - * - * Handles types defined in https://www.asyncapi.com/docs/reference/specification/v3.0.0#dataTypeFormat + * Builds an example for a schema */ public interface SchemaWalker { @@ -22,6 +20,11 @@ public interface SchemaWalker { @Nullable R fromSchema(Schema schema, Map definitions); + /** + * Check if the walker can handle the given content type + * + * @param contentType The content type to check + */ boolean canHandle(String contentType); class ExampleGeneratingException extends RuntimeException { diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalkerProvider.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalkerProvider.java index 5d73c3d58..c851d4009 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalkerProvider.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/example/SchemaWalkerProvider.java @@ -6,6 +6,9 @@ import java.util.List; import java.util.Optional; +/** + * Selects the appropriate SchemaWalker for a given content type + */ @RequiredArgsConstructor public class SchemaWalkerProvider { diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/ExampleGeneratorPostProcessor.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/ExampleGeneratorPostProcessor.java index 3cb25c394..edc4d7dc9 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/ExampleGeneratorPostProcessor.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/ExampleGeneratorPostProcessor.java @@ -20,13 +20,13 @@ public class ExampleGeneratorPostProcessor implements SchemasPostProcessor { public void process(Schema schema, Map definitions, String contentType) { if (schema.getExample() == null) { log.debug("Generate example for {}", schema.getName()); - Optional schemaWalkerOptional = schemaWalkerProvider.generatorFor(contentType); + Optional schemaWalkerOptional = schemaWalkerProvider.generatorFor(contentType); if (schemaWalkerOptional.isPresent()) { Object example = schemaWalkerOptional.get().fromSchema(schema, definitions); schema.setExample(example); } else { - log.debug("No Schema Walker for ContentType: {}", contentType); + log.debug("No matching SchemaWalker for contentType {} for example {}", contentType, schema.getName()); } } } diff --git a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/stavshamir/springwolf/example/kafka/consumers/XmlConsumer.java b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/stavshamir/springwolf/example/kafka/consumers/XmlConsumer.java index bd89518cc..6bb6b9080 100644 --- a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/stavshamir/springwolf/example/kafka/consumers/XmlConsumer.java +++ b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/stavshamir/springwolf/example/kafka/consumers/XmlConsumer.java @@ -4,6 +4,7 @@ import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncListener; import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncMessage; import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncOperation; +import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.KafkaAsyncOperationBinding; import io.github.stavshamir.springwolf.example.kafka.dtos.XmlPayloadDto; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -16,7 +17,12 @@ public class XmlConsumer { @AsyncListener( - operation = @AsyncOperation(channelName = "xml-topic", message = @AsyncMessage(contentType = "text/xml"))) + operation = + @AsyncOperation( + channelName = "xml-topic", + description = "Showcases a xml based message", + message = @AsyncMessage(contentType = "text/xml"))) + @KafkaAsyncOperationBinding @KafkaListener(topics = "xml-topic") public void receiveExamplePayload(XmlPayloadDto payload) { log.info("Received new message in example-queue: {}", payload.toString()); diff --git a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json index 56d0446cf..5fc741d25 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json @@ -100,11 +100,6 @@ "io.github.stavshamir.springwolf.example.kafka.dtos.XmlPayloadDto": { "$ref": "#/components/messages/io.github.stavshamir.springwolf.example.kafka.dtos.XmlPayloadDto" } - }, - "bindings": { - "kafka": { - "bindingVersion": "0.4.0" - } } } }, @@ -819,11 +814,7 @@ } }, "examples": [ - { - "someEnum": "FOO1", - "someLong": 0, - "someString": "string" - } + "FOO10string" ], "x-json-schema": { "$schema": "https://json-schema.org/draft-04/schema#", @@ -905,7 +896,7 @@ }, "io.github.stavshamir.springwolf.example.kafka.dtos.AnotherPayloadDto": { "headers": { - "$ref": "#/components/schemas/HeadersNotDocumented" + "$ref": "#/components/schemas/SpringKafkaDefaultHeaders-AnotherPayloadDto" }, "payload": { "schemaFormat": "application/vnd.aai.asyncapi+json;version=3.0.0", @@ -923,7 +914,7 @@ }, "io.github.stavshamir.springwolf.example.kafka.dtos.ExamplePayloadDto": { "headers": { - "$ref": "#/components/schemas/HeadersNotDocumented" + "$ref": "#/components/schemas/SpringKafkaDefaultHeaders-ExamplePayloadDto" }, "payload": { "schemaFormat": "application/vnd.aai.asyncapi+json;version=3.0.0", @@ -965,6 +956,25 @@ } } }, + "io.github.stavshamir.springwolf.example.kafka.dtos.XmlPayloadDto": { + "headers": { + "$ref": "#/components/schemas/HeadersNotDocumented" + }, + "payload": { + "schemaFormat": "application/vnd.aai.asyncapi+json;version=3.0.0", + "schema": { + "$ref": "#/components/schemas/io.github.stavshamir.springwolf.example.kafka.dtos.XmlPayloadDto" + } + }, + "contentType": "text/xml", + "name": "io.github.stavshamir.springwolf.example.kafka.dtos.XmlPayloadDto", + "title": "XmlPayloadDto", + "bindings": { + "kafka": { + "bindingVersion": "0.4.0" + } + } + }, "java.lang.String": { "headers": { "$ref": "#/components/schemas/HeadersNotDocumented" @@ -997,6 +1007,13 @@ "title": "MonetaryAmount", "bindings": { "kafka": { + "key": { + "type": "string", + "description": "Kafka Consumer Message Key", + "examples": [ + "example-key" + ] + }, "bindingVersion": "0.4.0" } } @@ -1178,6 +1195,8 @@ "channel": { "$ref": "#/channels/xml-topic" }, + "title": "xml-topic_receive", + "description": "Showcases a xml based message", "bindings": { "kafka": { "bindingVersion": "0.4.0" @@ -1190,4 +1209,4 @@ ] } } -} +} \ No newline at end of file