From e4b7f62c54a3bad87b2c223bf41a2f7b8d4673cc Mon Sep 17 00:00:00 2001 From: Ralph Ursprung Date: Thu, 26 Mar 2026 16:33:36 +0100 Subject: [PATCH] make AOT hints for ELC optional `spring-data-elasticsearch` is also being pulled in as a dependency of `spring-data-opensearch`. the latter excludes the transient ELC dependency since it doesn't need it. if `spring-data-opensearch` (or any downstream project) wants to use AOT then spring will automatically also pick up the AOT hints from `spring-data-elasticsearch` - but so far these reference the ELC classes which have been excluded by `spring-data-opensearch`, thus resulting in a `ClassNotFoundException`. this commit makes the registration of these hints dependent on the presence of the ELC on the classpath. this has no impact on `spring-data-elasticsearch` consumers which already make use of AOT but unblocks the usage of AOT for `spring-data-opensearch` and its consumers. see also opensearch-project/spring-data-opensearch#441 Signed-off-by: Ralph Ursprung --- .../aot/ElasticsearchClientRuntimeHints.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/aot/ElasticsearchClientRuntimeHints.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/aot/ElasticsearchClientRuntimeHints.java index 17bf3ec83..0030eedbe 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/aot/ElasticsearchClientRuntimeHints.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/aot/ElasticsearchClientRuntimeHints.java @@ -15,15 +15,9 @@ */ package org.springframework.data.elasticsearch.client.elc.aot; -import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType; -import co.elastic.clients.elasticsearch._types.mapping.TypeMapping; -import co.elastic.clients.elasticsearch.indices.IndexSettings; -import co.elastic.clients.elasticsearch.indices.PutMappingRequest; - import org.jspecify.annotations.Nullable; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; -import org.springframework.aot.hint.TypeReference; import org.springframework.util.ClassUtils; /** @@ -38,10 +32,14 @@ public class ElasticsearchClientRuntimeHints implements RuntimeHintsRegistrar { public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { hints.reflection() - .registerType(TypeReference.of(IndexSettings.class), builder -> builder.withField("_DESERIALIZER")) - .registerType(TypeReference.of(PutMappingRequest.class), builder -> builder.withField("_DESERIALIZER")) - .registerType(TypeReference.of(RuntimeFieldType.class), builder -> builder.withField("_DESERIALIZER")) - .registerType(TypeReference.of(TypeMapping.class), builder -> builder.withField("_DESERIALIZER")); + .registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch.indices.IndexSettings", + builder -> builder.withField("_DESERIALIZER")) + .registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch.indices.PutMappingRequest", + builder -> builder.withField("_DESERIALIZER")) + .registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType", + builder -> builder.withField("_DESERIALIZER")) + .registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch._types.mapping.TypeMapping", + builder -> builder.withField("_DESERIALIZER")); if (ClassUtils.isPresent("org.apache.http.impl.auth.BasicScheme", ElasticsearchClientRuntimeHints.class.getClassLoader())) {