Fix reflection hint registration for @JsonMixins

The hints need to be registered for the mixin not the target of the
mixin.

Closes gh-32929
This commit is contained in:
Andy Wilkinson
2022-11-03 17:44:26 +00:00
parent bb684cc0ad
commit 3fce1e9715
3 changed files with 9 additions and 7 deletions

View File

@@ -98,7 +98,7 @@ class JsonMixinModuleEntriesBeanRegistrationAotProcessor implements BeanRegistra
private void contributeHints(RuntimeHints runtimeHints, JsonMixinModuleEntries entries) {
Set<Class<?>> mixins = new LinkedHashSet<>();
entries.doWithEntry(this.classLoader, (type, mixin) -> mixins.add(type));
entries.doWithEntry(this.classLoader, (type, mixin) -> mixins.add(mixin));
new BindingReflectionHintsRegistrar().registerReflectionHints(runtimeHints.reflection(),
mixins.toArray(Class<?>[]::new));
}

View File

@@ -24,6 +24,7 @@ import java.util.function.BiConsumer;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.test.generate.TestGenerationContext;
@@ -62,8 +63,11 @@ class JsonMixinModuleEntriesBeanRegistrationAotProcessorTests {
registerEntries(RenameMixInClass.class);
processAheadOfTime();
RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
assertThat(RuntimeHintsPredicates.reflection().onMethod(Name.class, "getName")).accepts(runtimeHints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(NameAndAge.class, "getAge")).accepts(runtimeHints);
assertThat(RuntimeHintsPredicates.reflection().onType(RenameMixInClass.class)
.withMemberCategories(MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
.accepts(runtimeHints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(RenameMixInClass.class, "getName").introspect())
.accepts(runtimeHints);
}
@Test

View File

@@ -23,11 +23,9 @@ import org.springframework.boot.jackson.Name;
import org.springframework.boot.jackson.NameAndAge;
@JsonMixin(type = { Name.class, NameAndAge.class })
public class RenameMixInClass {
public abstract class RenameMixInClass {
@JsonProperty("username")
String getName() {
return null;
}
public abstract String getName();
}