Skip to content

Commit cd2dca2

Browse files
committed
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
1 parent 78bef31 commit cd2dca2

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

src/main/java/org/springframework/data/elasticsearch/client/elc/aot/ElasticsearchClientRuntimeHints.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client.elc.aot;
1717

18-
import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
19-
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
20-
import co.elastic.clients.elasticsearch.indices.IndexSettings;
21-
import co.elastic.clients.elasticsearch.indices.PutMappingRequest;
22-
2318
import org.jspecify.annotations.Nullable;
2419
import org.springframework.aot.hint.RuntimeHints;
2520
import org.springframework.aot.hint.RuntimeHintsRegistrar;
26-
import org.springframework.aot.hint.TypeReference;
2721
import org.springframework.util.ClassUtils;
2822

2923
/**
@@ -38,10 +32,14 @@ public class ElasticsearchClientRuntimeHints implements RuntimeHintsRegistrar {
3832
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
3933

4034
hints.reflection()
41-
.registerType(TypeReference.of(IndexSettings.class), builder -> builder.withField("_DESERIALIZER"))
42-
.registerType(TypeReference.of(PutMappingRequest.class), builder -> builder.withField("_DESERIALIZER"))
43-
.registerType(TypeReference.of(RuntimeFieldType.class), builder -> builder.withField("_DESERIALIZER"))
44-
.registerType(TypeReference.of(TypeMapping.class), builder -> builder.withField("_DESERIALIZER"));
35+
.registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch.indices.IndexSettings",
36+
builder -> builder.withField("_DESERIALIZER"))
37+
.registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch.indices.PutMappingRequest",
38+
builder -> builder.withField("_DESERIALIZER"))
39+
.registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType",
40+
builder -> builder.withField("_DESERIALIZER"))
41+
.registerTypeIfPresent(classLoader, "co.elastic.clients.elasticsearch._types.mapping.TypeMapping",
42+
builder -> builder.withField("_DESERIALIZER"));
4543

4644
if (ClassUtils.isPresent("org.apache.http.impl.auth.BasicScheme",
4745
ElasticsearchClientRuntimeHints.class.getClassLoader())) {

0 commit comments

Comments
 (0)