From c6c917d6ae7bb1784422dfa10bcd12efe72d85e5 Mon Sep 17 00:00:00 2001 From: Timon Back Date: Tue, 5 Mar 2024 19:42:13 +0100 Subject: [PATCH] fix(core): create correct servers reference --- .../v3/model/channel/ServerReference.java | 4 ++ .../AsyncAnnotationChannelsScanner.java | 5 +- .../AsyncAnnotationChannelsScannerTest.java | 49 +------------------ .../AsyncAnnotationOperationsScannerTest.java | 24 --------- .../kafka/producers/NestedProducer.java | 1 + .../src/test/resources/asyncapi.json | 9 +++- 6 files changed, 15 insertions(+), 77 deletions(-) diff --git a/springwolf-asyncapi/src/main/java/io/github/springwolf/asyncapi/v3/model/channel/ServerReference.java b/springwolf-asyncapi/src/main/java/io/github/springwolf/asyncapi/v3/model/channel/ServerReference.java index 2cb92919e..8ccd21464 100644 --- a/springwolf-asyncapi/src/main/java/io/github/springwolf/asyncapi/v3/model/channel/ServerReference.java +++ b/springwolf-asyncapi/src/main/java/io/github/springwolf/asyncapi/v3/model/channel/ServerReference.java @@ -20,4 +20,8 @@ public class ServerReference implements Reference { public String getRef() { return ref; } + + public static ServerReference fromServer(String server) { + return new ServerReference("#/servers/" + server); + } } diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/AsyncAnnotationChannelsScanner.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/AsyncAnnotationChannelsScanner.java index 7ceed4f05..253e1c7cd 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/AsyncAnnotationChannelsScanner.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/AsyncAnnotationChannelsScanner.java @@ -70,9 +70,8 @@ private Map.Entry buildChannel(MethodAndAnnotation met List servers = AsyncAnnotationUtil.getServers(operationAnnotation, resolver); if (servers != null && !servers.isEmpty()) { validateServers(servers, operation.getTitle()); - channelBuilder.servers(servers.stream() - .map(it -> ServerReference.builder().ref(it).build()) - .toList()); + channelBuilder.servers( + servers.stream().map(ServerReference::fromServer).toList()); } MessageObject message = buildMessage(operationAnnotation, methodAndAnnotation.method()); diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/AsyncAnnotationChannelsScannerTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/AsyncAnnotationChannelsScannerTest.java index 4694bd037..d2adfdc88 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/AsyncAnnotationChannelsScannerTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/AsyncAnnotationChannelsScannerTest.java @@ -204,18 +204,7 @@ void scan_componentHasListenerMethodWithAllAttributes() { ChannelObject expectedChannel = ChannelObject.builder() .bindings(null) .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) - .servers(List.of( - ServerReference.builder().ref("server1").build(), - ServerReference.builder().ref("server2").build())) - .build(); - - Operation expectedOperation = Operation.builder() - .action(OperationAction.SEND) - .title("test-channel_send") - .channel(ChannelReference.fromChannel("test-channel")) - .description("description") - .bindings(Map.of(TestOperationBindingProcessor.TYPE, TestOperationBindingProcessor.BINDING)) - .messages(List.of(MessageReference.toChannelMessage("test-channel", message))) + .servers(List.of(ServerReference.fromServer("server1"), ServerReference.fromServer("server2"))) .build(); assertThat(actualChannels).containsExactly(Map.entry("test-channel", expectedChannel)); @@ -244,29 +233,11 @@ void scan_componentHasMultipleListenerAnnotations() { .description("SimpleFoo Message Description") .build(); - Operation expectedOperation1 = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel("test-channel-1")) - .description("test-channel-1-description") - .title("test-channel-1_send") - .bindings(EMPTY_MAP) - .messages(List.of(MessageReference.toChannelMessage("test-channel-1", message))) - .build(); - ChannelObject expectedChannel1 = ChannelObject.builder() .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) .bindings(null) .build(); - Operation expectedOperation2 = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel("test-channel-2")) - .description("test-channel-2-description") - .title("test-channel-2_send") - .bindings(EMPTY_MAP) - .messages(List.of(MessageReference.toChannelMessage("test-channel-2", message))) - .build(); - ChannelObject expectedChannel2 = ChannelObject.builder() .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) .bindings(null) @@ -301,15 +272,6 @@ void scan_componentHasAsyncMethodAnnotation() { .bindings(EMPTY_MAP) .build(); - Operation expectedOperation = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel("test-channel")) - .description("test channel operation description") - .title("test-channel_send") - .bindings(EMPTY_MAP) - .messages(List.of(MessageReference.toChannelMessage("test-channel", message))) - .build(); - ChannelObject expectedChannel = ChannelObject.builder() .bindings(null) .messages(Map.of(message.getName(), MessageReference.toComponentMessage(message))) @@ -416,15 +378,6 @@ void scan_componentHasOnlyDeclaredMethods(Class clazz) { .bindings(EMPTY_MAP) .build(); - Operation expectedOperation = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel("test-channel")) - .description("test channel operation description") - .title("test-channel_send") - .bindings(EMPTY_MAP) - .messages(List.of(MessageReference.toChannelMessage("test-channel", message))) - .build(); - ChannelObject expectedChannel = ChannelObject.builder() .bindings(null) .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/AsyncAnnotationOperationsScannerTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/AsyncAnnotationOperationsScannerTest.java index b2745b9ab..d64d0b85a 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/AsyncAnnotationOperationsScannerTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/AsyncAnnotationOperationsScannerTest.java @@ -3,7 +3,6 @@ import io.github.springwolf.asyncapi.v3.model.channel.ChannelObject; import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; -import io.github.springwolf.asyncapi.v3.model.channel.ServerReference; import io.github.springwolf.asyncapi.v3.model.channel.message.MessageHeaders; import io.github.springwolf.asyncapi.v3.model.channel.message.MessageObject; import io.github.springwolf.asyncapi.v3.model.channel.message.MessagePayload; @@ -192,14 +191,6 @@ void scan_componentHasListenerMethodWithAllAttributes() { .bindings(EMPTY_MAP) .build(); - ChannelObject expectedChannel = ChannelObject.builder() - .bindings(null) - .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) - .servers(List.of( - ServerReference.builder().ref("server1").build(), - ServerReference.builder().ref("server2").build())) - .build(); - Operation expectedOperation = Operation.builder() .action(OperationAction.SEND) .title("test-channel_send") @@ -245,11 +236,6 @@ void scan_componentHasMultipleListenerAnnotations() { .messages(List.of(MessageReference.toChannelMessage("test-channel-1", message))) .build(); - ChannelObject expectedChannel1 = ChannelObject.builder() - .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) - .bindings(null) - .build(); - Operation expectedOperation2 = Operation.builder() .action(OperationAction.SEND) .channel(ChannelReference.fromChannel("test-channel-2")) @@ -259,11 +245,6 @@ void scan_componentHasMultipleListenerAnnotations() { .messages(List.of(MessageReference.toChannelMessage("test-channel-2", message))) .build(); - ChannelObject expectedChannel2 = ChannelObject.builder() - .messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message))) - .bindings(null) - .build(); - assertThat(actualOperations) .containsExactlyInAnyOrderEntriesOf(Map.of( "test-channel-1_send_methodWithMultipleAnnotation", expectedOperation1, @@ -302,11 +283,6 @@ void scan_componentHasAsyncMethodAnnotation() { .messages(List.of(MessageReference.toChannelMessage("test-channel", message))) .build(); - ChannelObject expectedChannel = ChannelObject.builder() - .bindings(null) - .messages(Map.of(message.getName(), MessageReference.toComponentMessage(message))) - .build(); - assertThat(actualOperations) .containsExactly(Map.entry("test-channel_send_methodWithAnnotation", expectedOperation)); } diff --git a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/producers/NestedProducer.java b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/producers/NestedProducer.java index ad81f82e5..e958a2c3b 100644 --- a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/producers/NestedProducer.java +++ b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/producers/NestedProducer.java @@ -23,6 +23,7 @@ public class NestedProducer { @AsyncOperation( channelName = "topic-defined-via-asyncPublisher-annotation", description = "Custom, optional description defined in the AsyncPublisher annotation", + servers = {"kafka"}, headers = @AsyncOperation.Headers( schemaName = "SpringDefaultHeaderAndCloudEvent", 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 6022c2145..cbb73e640 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json @@ -93,7 +93,12 @@ "io.github.springwolf.examples.kafka.dtos.NestedPayloadDto": { "$ref": "#/components/messages/io.github.springwolf.examples.kafka.dtos.NestedPayloadDto" } - } + }, + "servers": [ + { + "$ref": "#/servers/kafka" + } + ] }, "xml-topic": { "messages": { @@ -1297,4 +1302,4 @@ ] } } -} \ No newline at end of file +}