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

fix: for oneOfMessage parsing, kafka binding type error, yaml parsing tests #56

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 0 additions & 14 deletions asyncapi-core/asyncapi-core.iml

This file was deleted.

5 changes: 5 additions & 0 deletions asyncapi-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +63,7 @@ private <T extends MessageBinding> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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)

}
}



Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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