Browse Source

Refine BindingReflectionHintsRegistrar

This commit refines BindingReflectionHintsRegistrar to handle
correctly a use case with multiple levels of nested generics.

Closes gh-28683
pull/28715/head
Sébastien Deleuze 4 years ago
parent
commit
1aaa44bbfe
  1. 1
      spring-core/src/main/java/org/springframework/aot/hint/support/BindingReflectionHintsRegistrar.java
  2. 30
      spring-core/src/test/java/org/springframework/aot/hint/support/BindingReflectionHintsRegistrarTests.java

1
spring-core/src/main/java/org/springframework/aot/hint/support/BindingReflectionHintsRegistrar.java

@ -142,7 +142,6 @@ public class BindingReflectionHintsRegistrar { @@ -142,7 +142,6 @@ public class BindingReflectionHintsRegistrar {
if (type == null || seen.contains(type)) {
return;
}
seen.add(type);
ResolvableType resolvableType = ResolvableType.forType(type);
Class<?> clazz = resolvableType.resolve();
if (clazz != null) {

30
spring-core/src/test/java/org/springframework/aot/hint/support/BindingReflectionHintsRegistrarTests.java

@ -18,6 +18,7 @@ package org.springframework.aot.hint.support; @@ -18,6 +18,7 @@ package org.springframework.aot.hint.support;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;
@ -184,6 +185,17 @@ public class BindingReflectionHintsRegistrarTests { @@ -184,6 +185,17 @@ public class BindingReflectionHintsRegistrarTests {
});
}
@Test
void registerTypeForSerializationWithMultipleLevelsAndCollection() {
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassA.class);
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassA.class)),
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassB.class)),
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassC.class)),
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)),
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Set.class)));
}
static class SampleEmptyClass {
}
@ -244,4 +256,22 @@ public class BindingReflectionHintsRegistrarTests { @@ -244,4 +256,22 @@ public class BindingReflectionHintsRegistrarTests {
}
}
static class SampleClassA {
public Set<SampleClassB> getB() {
return null;
}
}
static class SampleClassB {
public SampleClassC getC() {
return null;
}
}
class SampleClassC {
public String getString() {
return "";
}
}
}

Loading…
Cancel
Save