Skip to content

Commit 56e4772

Browse files
algolia-botmillotp
andcommitted
fix(clients): correctly deserialize SearchResult (generated)
algolia/api-clients-automation#4756 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 682c95f commit 56e4772

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

algoliasearch/src/main/java/com/algolia/api/SearchClient.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -5025,6 +5025,8 @@ public CompletableFuture<UpdatedAtResponse> saveSynonymsAsync(@Nonnull String in
50255025
* Sends multiple search requests to one or more indices. This can be useful in these cases: -
50265026
* Different indices for different purposes, such as, one index for products, another one for
50275027
* marketing content. - Multiple searches to the same index—for example, with different filters.
5028+
* Use the helper `searchForHits` or `searchForFacets` to get the results in a more convenient
5029+
* format, if you already know the return type you want.
50285030
*
50295031
* @param searchMethodParams Muli-search request body. Results are returned in the same order as
50305032
* the requests. (required)
@@ -5045,6 +5047,8 @@ public <T> SearchResponses<T> search(
50455047
* Sends multiple search requests to one or more indices. This can be useful in these cases: -
50465048
* Different indices for different purposes, such as, one index for products, another one for
50475049
* marketing content. - Multiple searches to the same index—for example, with different filters.
5050+
* Use the helper `searchForHits` or `searchForFacets` to get the results in a more convenient
5051+
* format, if you already know the return type you want.
50485052
*
50495053
* @param searchMethodParams Muli-search request body. Results are returned in the same order as
50505054
* the requests. (required)
@@ -5059,7 +5063,8 @@ public <T> SearchResponses<T> search(@Nonnull SearchMethodParams searchMethodPar
50595063
* (asynchronously) Sends multiple search requests to one or more indices. This can be useful in
50605064
* these cases: - Different indices for different purposes, such as, one index for products,
50615065
* another one for marketing content. - Multiple searches to the same index—for example, with
5062-
* different filters.
5066+
* different filters. Use the helper `searchForHits` or `searchForFacets` to get the results in a
5067+
* more convenient format, if you already know the return type you want.
50635068
*
50645069
* @param searchMethodParams Muli-search request body. Results are returned in the same order as
50655070
* the requests. (required)
@@ -5088,7 +5093,8 @@ public <T> CompletableFuture<SearchResponses<T>> searchAsync(
50885093
* (asynchronously) Sends multiple search requests to one or more indices. This can be useful in
50895094
* these cases: - Different indices for different purposes, such as, one index for products,
50905095
* another one for marketing content. - Multiple searches to the same index—for example, with
5091-
* different filters.
5096+
* different filters. Use the helper `searchForHits` or `searchForFacets` to get the results in a
5097+
* more convenient format, if you already know the return type you want.
50925098
*
50935099
* @param searchMethodParams Muli-search request body. Results are returned in the same order as
50945100
* the requests. (required)

algoliasearch/src/main/java/com/algolia/model/search/SearchResult.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,34 @@
66
import com.algolia.exceptions.AlgoliaRuntimeException;
77
import com.fasterxml.jackson.annotation.*;
88
import com.fasterxml.jackson.core.*;
9-
import com.fasterxml.jackson.core.type.TypeReference;
109
import com.fasterxml.jackson.databind.*;
10+
import com.fasterxml.jackson.databind.BeanProperty;
1111
import com.fasterxml.jackson.databind.annotation.*;
12+
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
1213
import java.io.IOException;
1314
import java.util.logging.Logger;
1415

1516
/** SearchResult */
1617
@JsonDeserialize(using = SearchResult.Deserializer.class)
1718
public interface SearchResult<T> {
18-
class Deserializer<T> extends JsonDeserializer<SearchResult<T>> {
19+
class Deserializer<T> extends JsonDeserializer<SearchResult<T>> implements ContextualDeserializer {
1920

2021
private static final Logger LOGGER = Logger.getLogger(Deserializer.class.getName());
2122

23+
private JavaType returnType;
24+
25+
public Deserializer() {}
26+
27+
private Deserializer(JavaType returnType) {
28+
this.returnType = returnType;
29+
}
30+
31+
@Override
32+
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
33+
JavaType contextualType = ctxt.getContextualType().containedType(0);
34+
return new Deserializer(contextualType);
35+
}
36+
2237
@Override
2338
public SearchResult<T> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
2439
JsonNode tree = jp.readValueAsTree();
@@ -36,7 +51,12 @@ public SearchResult<T> deserialize(JsonParser jp, DeserializationContext ctxt) t
3651
// deserialize SearchResponse
3752
if (tree.isObject()) {
3853
try (JsonParser parser = tree.traverse(jp.getCodec())) {
39-
return parser.readValueAs(new TypeReference<SearchResponse<T>>() {});
54+
// For generic types, the innerType is erased by Java, we need to use the contextual type.
55+
JavaType innerType = ctxt.getTypeFactory().constructParametricType(SearchResponse.class, returnType);
56+
if (parser.getCurrentToken() == null) {
57+
parser.nextToken();
58+
}
59+
return ctxt.readValue(parser, innerType);
4060
} catch (Exception e) {
4161
// deserialization failed, continue
4262
LOGGER.finest("Failed to deserialize oneOf SearchResponse (error: " + e.getMessage() + ") (type: SearchResponse)");

0 commit comments

Comments
 (0)