Skip to content

Commit c0dc484

Browse files
committed
feat: support for beanRef in KafkaListener annotation
1 parent fea0a2e commit c0dc484

File tree

26 files changed

+160
-54
lines changed

26 files changed

+160
-54
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/bindings/BindingFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import java.util.Map;
1111

1212
public interface BindingFactory<T> {
13-
default String getChannelId(T annotation) {
14-
return ReferenceUtil.toValidId(getChannelName(annotation));
13+
default String getChannelId(T annotation, Class<?> component) {
14+
return ReferenceUtil.toValidId(getChannelName(annotation, component));
1515
}
1616

17-
String getChannelName(T annotation);
17+
String getChannelName(T annotation, Class<?> component);
1818

1919
Map<String, ChannelBinding> buildChannelBinding(T annotation);
2020

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationClassLevelChannelsScanner.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ private Stream<ChannelObject> mapClassToChannel(
4545
Set<Method> methods =
4646
annotatedMethods.stream().map(MethodAndAnnotation::method).collect(Collectors.toSet());
4747
Map<String, Message> messages = new HashMap<>(springAnnotationMessagesService.buildMessages(
48-
classAnnotation, methods, SpringAnnotationMessagesService.MessageType.CHANNEL));
48+
classAnnotation, component, methods, SpringAnnotationMessagesService.MessageType.CHANNEL));
4949

50-
return mapClassToChannel(classAnnotation, messages);
50+
return mapClassToChannel(classAnnotation, component, messages);
5151
}
5252

53-
private Stream<ChannelObject> mapClassToChannel(ClassAnnotation classAnnotation, Map<String, Message> messages) {
54-
return Stream.of(springAnnotationChannelService.buildChannel(classAnnotation, messages));
53+
private Stream<ChannelObject> mapClassToChannel(
54+
ClassAnnotation classAnnotation, Class<?> component, Map<String, Message> messages) {
55+
return Stream.of(springAnnotationChannelService.buildChannel(classAnnotation, component, messages));
5556
}
5657
}

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationMethodLevelChannelsScanner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private ChannelObject mapMethodToChannel(MethodAndAnnotation<MethodAnnotation> m
4848
MessageObject message = springAnnotationMessageService.buildMessage(annotation, payloadSchema, headerSchema);
4949
Map<String, Message> messages = Map.of(message.getMessageId(), MessageReference.toComponentMessage(message));
5050

51-
return springAnnotationChannelService.buildChannel(annotation, messages);
51+
return springAnnotationChannelService.buildChannel(
52+
annotation, method.method().getDeclaringClass(), messages);
5253
}
5354
}

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/channel/SpringAnnotationChannelService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class SpringAnnotationChannelService<Annotation extends java.lang.annotat
1818

1919
private final BindingFactory<Annotation> bindingFactory;
2020

21-
public ChannelObject buildChannel(Annotation annotation, Map<String, Message> messages) {
21+
public ChannelObject buildChannel(Annotation annotation, Class<?> component, Map<String, Message> messages) {
2222
Map<String, ChannelBinding> channelBinding = bindingFactory.buildChannelBinding(annotation);
2323
Map<String, ChannelBinding> chBinding = channelBinding != null ? new HashMap<>(channelBinding) : null;
24-
String channelName = bindingFactory.getChannelName(annotation);
24+
String channelName = bindingFactory.getChannelName(annotation, component);
2525
return ChannelObject.builder()
2626
.channelId(ReferenceUtil.toValidId(channelName))
2727
.address(channelName)

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/message/SpringAnnotationMessagesService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public enum MessageType {
4343
}
4444

4545
public Map<String, MessageReference> buildMessages(
46-
ClassAnnotation classAnnotation, Set<Method> methods, MessageType messageType) {
46+
ClassAnnotation classAnnotation, Class<?> component, Set<Method> methods, MessageType messageType) {
4747
Set<MessageObject> messages = methods.stream()
4848
.map(method -> buildMessage(classAnnotation, method))
4949
.collect(toSet());
5050

5151
if (messageType == MessageType.OPERATION) {
52-
String channelId = bindingFactory.getChannelName(classAnnotation);
52+
String channelId = bindingFactory.getChannelName(classAnnotation, component);
5353
return toOperationsMessagesMap(channelId, messages);
5454
}
5555
return toMessagesMap(messages);

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/operation/SpringAnnotationOperationService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ public class SpringAnnotationOperationService<MethodAnnotation extends Annotatio
2525
private final SpringAnnotationMessageService<MethodAnnotation> springAnnotationMessageService;
2626

2727
public Operation buildOperation(
28-
MethodAnnotation annotation, PayloadSchemaObject payloadType, SchemaObject headerSchema) {
28+
MethodAnnotation annotation,
29+
Class<?> component,
30+
PayloadSchemaObject payloadType,
31+
SchemaObject headerSchema) {
2932
MessageObject message = springAnnotationMessageService.buildMessage(annotation, payloadType, headerSchema);
3033
Map<String, OperationBinding> operationBinding = bindingFactory.buildOperationBinding(annotation);
3134
Map<String, OperationBinding> opBinding = operationBinding != null ? new HashMap<>(operationBinding) : null;
32-
String channelId = bindingFactory.getChannelId(annotation);
35+
String channelId = bindingFactory.getChannelId(annotation, component);
3336

3437
return Operation.builder()
3538
.action(OperationAction.RECEIVE)

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/operation/SpringAnnotationOperationsService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ public class SpringAnnotationOperationsService<ClassAnnotation extends Annotatio
2323
private final BindingFactory<ClassAnnotation> bindingFactory;
2424
private final SpringAnnotationMessagesService<ClassAnnotation> springAnnotationMessagesService;
2525

26-
public Operation buildOperation(ClassAnnotation classAnnotation, Set<Method> methods) {
26+
public Operation buildOperation(ClassAnnotation classAnnotation, Class<?> component, Set<Method> methods) {
2727
var messages = springAnnotationMessagesService.buildMessages(
28-
classAnnotation, methods, SpringAnnotationMessagesService.MessageType.OPERATION);
29-
return buildOperation(classAnnotation, messages);
28+
classAnnotation, component, methods, SpringAnnotationMessagesService.MessageType.OPERATION);
29+
return buildOperation(classAnnotation, component, messages);
3030
}
3131

32-
private Operation buildOperation(ClassAnnotation classAnnotation, Map<String, MessageReference> messages) {
32+
private Operation buildOperation(
33+
ClassAnnotation classAnnotation, Class<?> component, Map<String, MessageReference> messages) {
3334
Map<String, OperationBinding> operationBinding = bindingFactory.buildOperationBinding(classAnnotation);
3435
Map<String, OperationBinding> opBinding = operationBinding != null ? new HashMap<>(operationBinding) : null;
35-
String channelName = bindingFactory.getChannelName(classAnnotation);
36+
String channelName = bindingFactory.getChannelName(classAnnotation, component);
3637
String channelId = ReferenceUtil.toValidId(channelName);
3738

3839
return Operation.builder()

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/SpringAnnotationClassLevelOperationsScanner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ private Stream<Map.Entry<String, Operation>> mapClassToOperation(
4343
Class<?> component, Set<MethodAndAnnotation<MethodAnnotation>> annotatedMethods) {
4444
ClassAnnotation classAnnotation = AnnotationUtil.findFirstAnnotationOrThrow(classAnnotationClass, component);
4545

46-
String channelId = bindingFactory.getChannelId(classAnnotation);
46+
String channelId = bindingFactory.getChannelId(classAnnotation, component);
4747
String operationId =
4848
StringUtils.joinWith("_", channelId, OperationAction.RECEIVE.type, component.getSimpleName());
4949

5050
Set<Method> methods =
5151
annotatedMethods.stream().map(MethodAndAnnotation::method).collect(Collectors.toSet());
52-
Operation operation = springAnnotationOperationsService.buildOperation(classAnnotation, methods);
52+
Operation operation = springAnnotationOperationsService.buildOperation(classAnnotation, component, methods);
5353
annotatedMethods.forEach(
5454
method -> customizers.forEach(customizer -> customizer.customize(operation, method.method())));
5555
return Stream.of(Map.entry(operationId, operation));

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/operations/annotations/SpringAnnotationMethodLevelOperationsScanner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public Stream<Map.Entry<String, Operation>> scan(Class<?> clazz) {
4343
private Map.Entry<String, Operation> mapMethodToOperation(MethodAndAnnotation<MethodAnnotation> method) {
4444
MethodAnnotation annotation = AnnotationUtil.findFirstAnnotationOrThrow(methodAnnotationClass, method.method());
4545

46-
String channelId = bindingFactory.getChannelId(annotation);
46+
String channelId =
47+
bindingFactory.getChannelId(annotation, method.method().getDeclaringClass());
4748
String operationId = StringUtils.joinWith(
4849
"_", channelId, OperationAction.RECEIVE.type, method.method().getName());
4950

5051
PayloadSchemaObject payloadSchema = payloadMethodParameterService.extractSchema(method.method());
5152
SchemaObject headerSchema = headerClassExtractor.extractHeader(method.method(), payloadSchema);
5253

53-
Operation operation = springAnnotationOperationService.buildOperation(annotation, payloadSchema, headerSchema);
54+
Operation operation = springAnnotationOperationService.buildOperation(
55+
annotation, method.method().getDeclaringClass(), payloadSchema, headerSchema);
5456
customizers.forEach(customizer -> customizer.customize(operation, method.method()));
5557
return Map.entry(operationId, operation);
5658
}

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/channels/annotations/SpringAnnotationClassLevelChannelsScannerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static class TestBindingFactory implements BindingFactory<TestClassListener> {
291291
Map.of(CHANNEL_ID, new TestBindingFactory.TestOperationBinding());
292292

293293
@Override
294-
public String getChannelName(TestClassListener annotation) {
294+
public String getChannelName(TestClassListener annotation, Class<?> component) {
295295
return CHANNEL;
296296
}
297297

0 commit comments

Comments
 (0)