Skip to content

Commit e9ae6c7

Browse files
Fix native image hints for web support.
This commit adds a required native image reflection hint to allow Jackson object mapping to resolve and invoke the constructor of PageMetadata. It also fixes an issue that surfaced after changing the bean constructor argument for SpringDataWebSettings leading to failures during the native compilation. Closes: #3171
1 parent 960b539 commit e9ae6c7

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/main/java/org/springframework/data/web/aot/WebRuntimeHints.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2121
import org.springframework.aot.hint.TypeReference;
2222
import org.springframework.data.web.PagedModel;
23+
import org.springframework.data.web.config.EnableSpringDataWebSupport;
2324
import org.springframework.data.web.config.SpringDataJacksonConfiguration.PageModule;
2425
import org.springframework.lang.Nullable;
2526
import org.springframework.util.ClassUtils;
@@ -35,12 +36,17 @@ class WebRuntimeHints implements RuntimeHintsRegistrar {
3536
@Override
3637
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
3738

39+
hints.reflection().registerType(org.springframework.data.web.config.SpringDataWebSettings.class, hint -> hint
40+
.withMembers(MemberCategory.INVOKE_DECLARED_METHODS).onReachableType(EnableSpringDataWebSupport.class));
41+
3842
if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader)) {
3943

4044
// Page Model for Jackson Rendering
4145
hints.reflection().registerType(org.springframework.data.web.PagedModel.class,
4246
MemberCategory.INVOKE_PUBLIC_METHODS);
43-
hints.reflection().registerType(PagedModel.PageMetadata.class, MemberCategory.INVOKE_PUBLIC_METHODS);
47+
48+
hints.reflection().registerType(PagedModel.PageMetadata.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
49+
MemberCategory.INVOKE_PUBLIC_METHODS);
4450

4551
// Type that might not be seen otherwise
4652
hints.reflection().registerType(TypeReference.of("org.springframework.data.domain.Unpaged"),

src/test/java/org/springframework/data/web/aot/WebRuntimeHintsUnitTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package org.springframework.data.web.aot;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.mockito.Mockito.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
2021

2122
import org.junit.jupiter.api.Test;
22-
2323
import org.springframework.aot.hint.ReflectionHints;
2424
import org.springframework.aot.hint.RuntimeHints;
2525
import org.springframework.aot.hint.TypeReference;
@@ -46,7 +46,7 @@ void shouldRegisterRuntimeHintWhenJacksonPresent() {
4646
RuntimeHintsPredicates.reflection().onType(TypeReference.of("org.springframework.data.domain.Unpaged")));
4747
}
4848

49-
@Test // GH-3033
49+
@Test // GH-3033, GH-3171
5050
@ClassPathExclusions(packages = { "com.fasterxml.jackson.databind" })
5151
void shouldRegisterRuntimeHintWithTypeNameWhenJacksonNotPresent() {
5252

@@ -56,6 +56,7 @@ void shouldRegisterRuntimeHintWithTypeNameWhenJacksonNotPresent() {
5656

5757
new WebRuntimeHints().registerHints(runtimeHints, this.getClass().getClassLoader());
5858

59-
assertThat(runtimeHints.reflection().typeHints()).isEmpty();
59+
assertThat(runtimeHints).matches(RuntimeHintsPredicates.reflection()
60+
.onType(TypeReference.of("org.springframework.data.web.config.SpringDataWebSettings")));
6061
}
6162
}

0 commit comments

Comments
 (0)