Skip to content

Commit

Permalink
fix(core): extract payload in generics from correct index
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed May 3, 2024
1 parent 9e7a4be commit 3c50156
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private Class<?> extractActualGenericClass(ParameterizedType parameterType, Stri
String typeName = rawParameterTypeName;

while (type instanceof ParameterizedType && extractableClassToArgumentIndex.containsKey(typeName)) {
Integer index = extractableClassToArgumentIndex.get(rawParameterTypeName);
Integer index = extractableClassToArgumentIndex.get(typeName);

type = ((ParameterizedType) type).getActualTypeArguments()[index];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -23,6 +24,10 @@ class TypeToClassConverterTest {
{
SpringwolfConfigProperties properties = new SpringwolfConfigProperties();
properties.setPayload(new SpringwolfConfigProperties.Payload());
properties
.getPayload()
.setExtractableClasses(new HashMap<>(properties.getPayload().getExtractableClasses()));
properties.getPayload().getExtractableClasses().put(TestClass.CustomPair.class.getName(), 1);
typeToClassConverter = new TypeToClassConverter(properties);
}

Expand Down Expand Up @@ -72,6 +77,16 @@ void getPayloadTypeWithMessageOfListOfString() throws NoSuchMethodException {
assertThat(result).isEqualTo(String.class);
}

@Test
void getPayloadTypeWithMessageOfListOfCustomPair() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithMessageOfCustomPair", Message.class);

Class<?> result = typeToClassConverter.extractClass(extractFrom(m));

// payload is extracted from the second generic argument
assertThat(result).isEqualTo(Double.class);
}

@Test
void getPayloadTypeWithMessageOfString() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithMessageOfString", Message.class);
Expand Down Expand Up @@ -112,6 +127,8 @@ public void consumeWithMessageOfStringExtends(Message<? extends String> value) {

public void consumeWithMessageOfListOfString(Message<List<String>> value) {}

public void consumeWithMessageOfCustomPair(Message<CustomPair<Integer, Double>> value) {}

public void consumeWithMessageOfString(Message<String> value) {}

public void consumeWithCustomType(MyType value) {}
Expand All @@ -121,6 +138,8 @@ public MyType(String payload) {
super(payload);
}
}

public interface CustomPair<K, V> {}
}

private Type extractFrom(Method method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ springwolf.docket.info.contact.name=springwolf
springwolf.docket.info.contact.email=[email protected]
springwolf.docket.info.contact.url=https://github.com/springwolf/springwolf-core
springwolf.docket.info.license.name=Apache License 2.0
springwolf.payload.extractable-classes.java.util.List=0
springwolf.payload.extractable-classes.org.apache.kafka.clients.consumer.ConsumerRecord=1

# Springwolf kafka configuration
springwolf.docket.servers.kafka-server.protocol=kafka
Expand All @@ -53,3 +53,5 @@ springwolf.plugin.kafka.publishing.producer.properties.sasl.mechanism=PLAIN

# For debugging purposes
logging.level.io.github.springwolf=DEBUG


0 comments on commit 3c50156

Please sign in to comment.