Skip to content

Commit

Permalink
feat(core): use AsyncApi 3.0 OperationAction
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Jan 14, 2024
1 parent 6165d4b commit 07ab2f8
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import io.github.stavshamir.springwolf.asyncapi.scanners.classes.ComponentClassScanner;
import io.github.stavshamir.springwolf.asyncapi.scanners.classes.ConfigurationClassScanner;
import io.github.stavshamir.springwolf.asyncapi.scanners.classes.SpringwolfClassScanner;
import io.github.stavshamir.springwolf.asyncapi.types.OperationData;
import io.github.stavshamir.springwolf.asyncapi.v3.model.operation.OperationAction;
import io.github.stavshamir.springwolf.configuration.AsyncApiDocketService;
import io.github.stavshamir.springwolf.schemas.SchemasService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -121,8 +121,8 @@ public AsyncOperation getAsyncOperation(AsyncListener annotation) {
}

@Override
public OperationData.OperationType getOperationType() {
return OperationData.OperationType.PUBLISH;
public OperationAction getOperationType() {
return OperationAction.SEND;
}
};
}
Expand All @@ -141,8 +141,8 @@ public AsyncOperation getAsyncOperation(AsyncPublisher annotation) {
}

@Override
public OperationData.OperationType getOperationType() {
return OperationData.OperationType.SUBSCRIBE;
public OperationAction getOperationType() {
return OperationAction.RECEIVE;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncOperation;
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.payload.PayloadClassExtractor;
import io.github.stavshamir.springwolf.asyncapi.scanners.classes.ClassScanner;
import io.github.stavshamir.springwolf.asyncapi.types.OperationData;
import io.github.stavshamir.springwolf.asyncapi.types.channel.operation.message.header.AsyncHeaders;
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.MessageBinding;
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.OperationBinding;
Expand Down Expand Up @@ -164,10 +163,7 @@ private Map.Entry<String, Operation> buildOperation(MethodAndAnnotation<A> metho
String operationName = resolver.resolveStringValue(operationAnnotation.channelName());

Operation operation = buildOperation(operationAnnotation, methodAndAnnotation.method(), operationName);
switch (this.asyncAnnotationProvider.getOperationType()) {
case PUBLISH -> operation.setAction(OperationAction.RECEIVE);
case SUBSCRIBE -> operation.setAction(OperationAction.SEND);
}
operation.setAction(this.asyncAnnotationProvider.getOperationType());

MessageObject message = buildMessage(operationAnnotation, methodAndAnnotation.method());

Expand All @@ -183,7 +179,7 @@ private Operation buildOperation(AsyncOperation asyncOperation, Method method, S
description = "Auto-generated description";
}

String operationTitle = channelName + "_" + this.asyncAnnotationProvider.getOperationType().operationName;
String operationTitle = channelName + "_" + this.asyncAnnotationProvider.getOperationType().type;

Map<String, OperationBinding> operationBinding =
AsyncAnnotationScannerUtil.processOperationBindingFromAnnotation(method, operationBindingProcessors);
Expand Down Expand Up @@ -269,7 +265,7 @@ public interface AsyncAnnotationProvider<A> {

AsyncOperation getAsyncOperation(A annotation);

OperationData.OperationType getOperationType();
OperationAction getOperationType();
}

private record MethodAndAnnotation<A>(Method method, A annotation) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private Stream<Map.Entry<String, Operation>> mapClassToOperation(Class<?> compon

// FIXME
String channelName = bindingFactory.getChannelName(classAnnotation);
String operationId = channelName + "_publish_" + component.getSimpleName();
String operationId = channelName + "_receive_" + component.getSimpleName();

Operation operation = buildOperation(classAnnotation, annotatedMethods);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private Map.Entry<String, Operation> mapMethodToOperation(Method method) {
MethodAnnotation annotation = AnnotationUtil.findAnnotationOrThrow(methodAnnotationClass, method);

String channelName = bindingFactory.getChannelName(annotation);
String operationId = channelName + "_publish_" + method.getName();
String operationId = channelName + "_receive_" + method.getName();
Class<?> payload = payloadClassExtractor.extractFrom(method);

Operation operation = buildOperation(annotation, payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,4 @@ public interface OperationData {
Map<String, MessageBinding> getMessageBinding();

MessageObject getMessage();

// FIXME: With AsyncAPI v3 this should be Action: SEND / RECEIVER
enum OperationType {
PUBLISH("publish"),
SUBSCRIBE("subscribe");

public final String operationName;

OperationType(String operationName) {
this.operationName = operationName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncOperation;
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.payload.PayloadClassExtractor;
import io.github.stavshamir.springwolf.asyncapi.scanners.classes.ClassScanner;
import io.github.stavshamir.springwolf.asyncapi.types.OperationData;
import io.github.stavshamir.springwolf.asyncapi.types.channel.operation.message.header.AsyncHeaders;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.ChannelObject;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.ServerReference;
Expand All @@ -19,6 +18,7 @@
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageReference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.info.Info;
import io.github.stavshamir.springwolf.asyncapi.v3.model.operation.Operation;
import io.github.stavshamir.springwolf.asyncapi.v3.model.operation.OperationAction;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.SchemaReference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.server.Server;
Expand Down Expand Up @@ -70,8 +70,8 @@ public AsyncOperation getAsyncOperation(AsyncListener annotation) {
}

@Override
public OperationData.OperationType getOperationType() {
return OperationData.OperationType.PUBLISH;
public OperationAction getOperationType() {
return OperationAction.SEND;
}
};
private final SpringwolfConfigProperties properties = new SpringwolfConfigProperties();
Expand Down Expand Up @@ -177,7 +177,7 @@ void scan_componentHasListenerMethodWithUnknownServer() {
assertThatThrownBy(channelScanner::scanChannels)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Operation 'test-channel_publish' defines unknown server ref 'server3'. This AsyncApi defines these server(s): [server1, server2]");
"Operation 'test-channel_send' defines unknown server ref 'server3'. This AsyncApi defines these server(s): [server1, server2]");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
}
},
"operations": {
"another-queue_publish_receiveAnotherPayload": {
"another-queue_receive_receiveAnotherPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/another-queue"
Expand All @@ -274,11 +274,11 @@
]
},
"example-producer-channel-publisher": {
"action": "send",
"action": "receive",
"channel": {
"$ref": "#/channels/example-producer-channel-publisher"
},
"title": "example-producer-channel-publisher_subscribe",
"title": "example-producer-channel-publisher_receive",
"description": "Custom, optional description defined in the AsyncPublisher annotation",
"bindings": {
"amqp": {
Expand All @@ -298,7 +298,7 @@
}
]
},
"example-queue_publish_receiveExamplePayload": {
"example-queue_receive_receiveExamplePayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/example-queue"
Expand All @@ -318,7 +318,7 @@
}
]
},
"example-topic-routing-key_publish_bindingsExample": {
"example-topic-routing-key_receive_bindingsExample": {
"action": "receive",
"channel": {
"$ref": "#/channels/example-topic-routing-key"
Expand All @@ -338,7 +338,7 @@
}
]
},
"multi-payload-queue_publish_bindingsBeanExample": {
"multi-payload-queue_receive_bindingsBeanExample": {
"action": "receive",
"channel": {
"$ref": "#/channels/multi-payload-queue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@
},
"operations": {
"another-queue": {
"action": "send",
"action": "receive",
"channel": {
"$ref": "#/channels/another-queue"
},
"title": "another-queue_subscribe",
"title": "another-queue_receive",
"description": "Custom, optional description defined in the AsyncPublisher annotation",
"bindings": {
"jms": {
Expand All @@ -172,7 +172,7 @@
}
]
},
"another-queue_publish_receiveAnotherPayload": {
"another-queue_receive_receiveAnotherPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/another-queue"
Expand All @@ -186,7 +186,7 @@
}
]
},
"example-queue_publish_receiveExamplePayload": {
"example-queue_receive_receiveExamplePayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/example-queue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@
}
},
"operations": {
"another-topic_publish_receiveAnotherPayloadBatched": {
"another-topic_receive_receiveAnotherPayloadBatched": {
"action": "receive",
"channel": {
"$ref": "#/channels/another-topic"
Expand All @@ -867,11 +867,11 @@
]
},
"avro-topic": {
"action": "receive",
"action": "send",
"channel": {
"$ref": "#/channels/avro-topic"
},
"title": "avro-topic_publish",
"title": "avro-topic_send",
"description": "Requires a running kafka-schema-registry. See docker-compose.yml to start it",
"bindings": {
"kafka": {
Expand All @@ -884,7 +884,7 @@
}
]
},
"avro-topic_publish_receiveExampleAvroPayload": {
"avro-topic_receive_receiveExampleAvroPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/avro-topic"
Expand All @@ -900,7 +900,7 @@
}
]
},
"example-topic_publish_receiveExamplePayload": {
"example-topic_receive_receiveExamplePayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/example-topic"
Expand All @@ -917,11 +917,11 @@
]
},
"multi-payload-topic": {
"action": "receive",
"action": "send",
"channel": {
"$ref": "#/channels/multi-payload-topic"
},
"title": "multi-payload-topic_publish",
"title": "multi-payload-topic_send",
"description": "Override description in the AsyncListener annotation with servers at kafka:29092",
"bindings": {
"kafka": {
Expand All @@ -946,7 +946,7 @@
}
]
},
"multi-payload-topic_publish_ExampleClassLevelKafkaListener": {
"multi-payload-topic_receive_ExampleClassLevelKafkaListener": {
"action": "receive",
"channel": {
"$ref": "#/channels/multi-payload-topic"
Expand All @@ -957,7 +957,7 @@
}
}
},
"protobuf-topic_publish_receiveExampleProtobufPayload": {
"protobuf-topic_receive_receiveExampleProtobufPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/protobuf-topic"
Expand All @@ -974,11 +974,11 @@
]
},
"string-topic": {
"action": "receive",
"action": "send",
"channel": {
"$ref": "#/channels/string-topic"
},
"title": "string-topic_publish",
"title": "string-topic_send",
"description": "Final classes (like String) can be documented using an envelope class and the @AsyncApiPayload annotation.",
"bindings": {
"kafka": {
Expand All @@ -991,7 +991,7 @@
}
]
},
"string-topic_publish_receiveStringPayload": {
"string-topic_receive_receiveStringPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/string-topic"
Expand All @@ -1008,11 +1008,11 @@
]
},
"topic-defined-via-asyncPublisher-annotation": {
"action": "send",
"action": "receive",
"channel": {
"$ref": "#/channels/topic-defined-via-asyncPublisher-annotation"
},
"title": "topic-defined-via-asyncPublisher-annotation_subscribe",
"title": "topic-defined-via-asyncPublisher-annotation_receive",
"description": "Custom, optional description defined in the AsyncPublisher annotation",
"bindings": {
"kafka": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@
},
"operations": {
"another-topic": {
"action": "receive",
"action": "send",
"channel": {
"$ref": "#/channels/another-topic"
},
"title": "another-topic_publish",
"title": "another-topic_send",
"description": "Auto-generated description",
"bindings": {
"sns": {
Expand All @@ -249,11 +249,11 @@
]
},
"example-topic": {
"action": "receive",
"action": "send",
"channel": {
"$ref": "#/channels/example-topic"
},
"title": "example-topic_publish",
"title": "example-topic_send",
"description": "Auto-generated description",
"bindings": {
"sns": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@
},
"operations": {
"another-queue": {
"action": "send",
"action": "receive",
"channel": {
"$ref": "#/channels/another-queue"
},
"title": "another-queue_subscribe",
"title": "another-queue_receive",
"description": "Custom, optional description defined in the AsyncPublisher annotation",
"bindings": {
"sqs": {
Expand All @@ -168,7 +168,7 @@
}
]
},
"another-queue_publish_receiveAnotherPayload": {
"another-queue_receive_receiveAnotherPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/another-queue"
Expand All @@ -184,7 +184,7 @@
}
]
},
"example-queue_publish_receiveExamplePayload": {
"example-queue_receive_receiveExamplePayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/example-queue"
Expand Down

0 comments on commit 07ab2f8

Please sign in to comment.