From 5a4fb3817c81a0ce983f556504551bf205b1273a Mon Sep 17 00:00:00 2001 From: Bernhard Huber Date: Thu, 23 Sep 2021 10:18:29 +0200 Subject: [PATCH 1/2] fixing oneOf message parsing and kafa binding type error --- asyncapi-core/asyncapi-core.iml | 14 ------ asyncapi-core/pom.xml | 9 +++- .../binding/MessageBindingsDeserializer.java | 3 +- .../jackson/OperationMessageDeserializer.java | 28 +++++++++--- .../asyncapi/v2/yaml/ParseAsyncApiYamlTest.kt | 43 +++++++++++++++++++ .../yaml/model/testKafkaBindingsKey.yaml | 26 +++++++++++ .../yaml/model/testOperationOneOf.yaml | 26 +++++++++++ 7 files changed, 127 insertions(+), 22 deletions(-) delete mode 100644 asyncapi-core/asyncapi-core.iml create mode 100644 asyncapi-core/src/test/kotlin/com/asyncapi/v2/yaml/ParseAsyncApiYamlTest.kt create mode 100644 asyncapi-core/src/test/resources/yaml/model/testKafkaBindingsKey.yaml create mode 100644 asyncapi-core/src/test/resources/yaml/model/testOperationOneOf.yaml diff --git a/asyncapi-core/asyncapi-core.iml b/asyncapi-core/asyncapi-core.iml deleted file mode 100644 index 390a3d87..00000000 --- a/asyncapi-core/asyncapi-core.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/asyncapi-core/pom.xml b/asyncapi-core/pom.xml index 5d66c42d..92d8b317 100644 --- a/asyncapi-core/pom.xml +++ b/asyncapi-core/pom.xml @@ -5,8 +5,8 @@ asyncapi - com.asyncapi - 1.0.0-EAP-1 + at.wrwks.com.asyncapi + 1.0.0-EAP-WRWKS-1 4.0.0 @@ -45,6 +45,11 @@ com.fasterxml.jackson.core jackson-databind + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + test + diff --git a/asyncapi-core/src/main/java/com/asyncapi/v2/binding/MessageBindingsDeserializer.java b/asyncapi-core/src/main/java/com/asyncapi/v2/binding/MessageBindingsDeserializer.java index 9c873372..88dfac8c 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v2/binding/MessageBindingsDeserializer.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v2/binding/MessageBindingsDeserializer.java @@ -4,6 +4,7 @@ import com.asyncapi.v2.binding.amqp1.AMQP1MessageBinding; import com.asyncapi.v2.binding.http.HTTPMessageBinding; import com.asyncapi.v2.binding.jms.JMSMessageBinding; +import com.asyncapi.v2.binding.kafka.KafkaMessageBinding; import com.asyncapi.v2.binding.mqtt.MQTTMessageBinding; import com.asyncapi.v2.binding.mqtt5.MQTT5MessageBinding; import com.asyncapi.v2.binding.nats.NATSMessageBinding; @@ -62,7 +63,7 @@ private T chooseKnownPojo(String bindingKey, JsonNode case "amqp1": return (T) objectMapper.readValue(binding.toString(), AMQP1MessageBinding.class); case "http": return (T) objectMapper.readValue(binding.toString(), HTTPMessageBinding.class); case "jms": return (T) objectMapper.readValue(binding.toString(), JMSMessageBinding.class); - case "kafka": return (T) objectMapper.readValue(binding.toString(), JMSMessageBinding.class); + case "kafka": return (T) objectMapper.readValue(binding.toString(), KafkaMessageBinding.class); case "mqtt": return (T) objectMapper.readValue(binding.toString(), MQTTMessageBinding.class); case "mqtt5": return (T) objectMapper.readValue(binding.toString(), MQTT5MessageBinding.class); case "nats": return (T) objectMapper.readValue(binding.toString(), NATSMessageBinding.class); diff --git a/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/OperationMessageDeserializer.java b/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/OperationMessageDeserializer.java index bcaffbe2..4a86d12a 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/OperationMessageDeserializer.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/OperationMessageDeserializer.java @@ -11,9 +11,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** - * Serializes operation traits list. + * Serializes operation message list. * * @author Pavel Bodiachevskii */ @@ -29,12 +31,28 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx return chooseKnownPojo(node); } - private Object chooseKnownPojo(JsonNode traitsValue) throws IOException { - if (traitsValue.get("$ref") != null) { - return objectMapper.readValue(traitsValue.toString(), Reference.class); + private Object chooseKnownPojo(JsonNode messageValue) throws IOException { + if (messageValue.get("$ref") != null) { + return objectMapper.readValue(messageValue.toString(), Reference.class); + } else if (messageValue.get("oneOf") != null) { + return extractOneOf(messageValue); } else { - return objectMapper.readValue(traitsValue.toString(), Message.class); + return objectMapper.readValue(messageValue.toString(), Message.class); } } + private List extractOneOf(JsonNode messageValue) throws IOException { + List oneOf = new ArrayList(); + for (JsonNode array : messageValue) { + if (array.isArray()) { + for (JsonNode item : array) { + Object parsedChildOrNull = chooseKnownPojo(item); + if (parsedChildOrNull != null) + oneOf.add(parsedChildOrNull); + } + } + } + return oneOf; + } + } diff --git a/asyncapi-core/src/test/kotlin/com/asyncapi/v2/yaml/ParseAsyncApiYamlTest.kt b/asyncapi-core/src/test/kotlin/com/asyncapi/v2/yaml/ParseAsyncApiYamlTest.kt new file mode 100644 index 00000000..a732630e --- /dev/null +++ b/asyncapi-core/src/test/kotlin/com/asyncapi/v2/yaml/ParseAsyncApiYamlTest.kt @@ -0,0 +1,43 @@ +package com.asyncapi.v2.yaml + +import com.asyncapi.v2.ClasspathUtils +import com.asyncapi.v2.binding.kafka.KafkaMessageBinding +import com.asyncapi.v2.model.AsyncAPI +import com.asyncapi.v2.model.channel.message.Message +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test + + +class ParseAsyncApiYamlTest { + + + private val objectMapper = ObjectMapper(YAMLFactory()) + + @Test + @DisplayName("parse yaml with oneOf Message") + fun testParseOneOfOperationMessage() { + val result = objectMapper.readValue( + ClasspathUtils.readAsString("/yaml/model/testOperationOneOf.yaml"), + AsyncAPI::class.java + ) + Assertions.assertEquals(2, (result.channels.get("test-topic-1")?.subscribe?.message as Collection<*>).size) + + } + + @Test + @DisplayName("parse yaml with kafka binding") + fun testParseKeyOfKafkaMessageBinding() { + val result = objectMapper.readValue( + ClasspathUtils.readAsString("/yaml/model/testKafkaBindingsKey.yaml"), + AsyncAPI::class.java + ) + Assertions.assertNotNull(((result.components?.messages?.get("test-event-1") as Message)?.bindings?.get("kafka") as KafkaMessageBinding).key) + + } +} + + + diff --git a/asyncapi-core/src/test/resources/yaml/model/testKafkaBindingsKey.yaml b/asyncapi-core/src/test/resources/yaml/model/testKafkaBindingsKey.yaml new file mode 100644 index 00000000..47d9bcfd --- /dev/null +++ b/asyncapi-core/src/test/resources/yaml/model/testKafkaBindingsKey.yaml @@ -0,0 +1,26 @@ +asyncapi: 2.1.0 +info: + title: test api + version: 1.0.0 +channels: + test-topic-1: + subscribe: + message: + $ref: '#/components/messages/test-event-1' + + +components: + messages: + test-event-1: + contentType: 'application/json' + payload: + type: object + additionalProperties: true + bindings: + kafka: + key: + type: string + title: businesskey + description: loginidentifier + format: email + diff --git a/asyncapi-core/src/test/resources/yaml/model/testOperationOneOf.yaml b/asyncapi-core/src/test/resources/yaml/model/testOperationOneOf.yaml new file mode 100644 index 00000000..5c32541b --- /dev/null +++ b/asyncapi-core/src/test/resources/yaml/model/testOperationOneOf.yaml @@ -0,0 +1,26 @@ +asyncapi: 2.1.0 +info: + title: test api + version: 1.0.0 +channels: + test-topic-1: + subscribe: + message: + oneOf: + - $ref: '#/components/messages/test-event-1' + - $ref: '#/components/messages/test-event-2' + + +components: + messages: + test-event-1: + contentType: 'application/json' + payload: + type: object + additionalProperties: true + + test-event-2: + contentType: 'application/json' + payload: + type: object + additionalProperties: true From 677802b062a3d095c217cad260ee103d63d76d80 Mon Sep 17 00:00:00 2001 From: Bernhard Huber Date: Thu, 23 Sep 2021 10:25:08 +0200 Subject: [PATCH 2/2] KUN-4408 reverting parent pom mods --- asyncapi-core/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asyncapi-core/pom.xml b/asyncapi-core/pom.xml index 92d8b317..ed5f4499 100644 --- a/asyncapi-core/pom.xml +++ b/asyncapi-core/pom.xml @@ -5,8 +5,8 @@ asyncapi - at.wrwks.com.asyncapi - 1.0.0-EAP-WRWKS-1 + com.asyncapi + 1.0.0-EAP-1 4.0.0