Skip to content

Commit d2d08d8

Browse files
committed
fix(spec): better filters type
1 parent 6e3d1e2 commit d2d08d8

File tree

4 files changed

+149
-48
lines changed

4 files changed

+149
-48
lines changed

generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.swagger.util.Json;
77
import java.util.*;
88
import java.util.Map.Entry;
9+
import org.openapitools.codegen.CodegenComposedSchemas;
910
import org.openapitools.codegen.CodegenModel;
1011
import org.openapitools.codegen.CodegenOperation;
1112
import org.openapitools.codegen.CodegenParameter;
@@ -205,7 +206,24 @@ private void handleModel(
205206
int suffix
206207
) throws CTSException {
207208
if (!spec.getHasVars()) {
208-
throw new CTSException("Spec has no vars.");
209+
// In this case we might have a complex `allOf`, we will first check
210+
// if it exists
211+
CodegenComposedSchemas composedSchemas = spec.getComposedSchemas();
212+
213+
if (composedSchemas != null) {
214+
List<CodegenProperty> allOf = composedSchemas.getAllOf();
215+
216+
if (allOf != null && !allOf.isEmpty()) {
217+
traverseParams(paramName, param, allOf.get(0), parent, suffix);
218+
219+
return;
220+
}
221+
}
222+
// We only throw if there is no `composedSchemas`, because `oneOf` can also
223+
// be handled below
224+
else {
225+
throw new CTSException("Spec has no vars.");
226+
}
209227
}
210228

211229
if (spec.getItems() != null) {
@@ -466,7 +484,18 @@ private IJsonSchemaValidationProperties findMatchingOneOf(
466484
return bestOneOf;
467485
}
468486
if (param instanceof List) {
469-
// no idea for list
487+
// NICE ---> no idea for list <--- NICE
488+
CodegenComposedSchemas composedSchemas = model.getComposedSchemas();
489+
490+
if (composedSchemas != null) {
491+
List<CodegenProperty> oneOf = composedSchemas.getOneOf();
492+
493+
// Somehow this is not yet enough
494+
if (oneOf != null && !oneOf.isEmpty()) {
495+
return oneOf.get(0);
496+
}
497+
}
498+
470499
return null;
471500
}
472501

specs/common/schemas/SearchParams.yml

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,14 @@ baseSearchParams:
3030
type: string
3131
description: Filter the query with numeric, facet and/or tag filters.
3232
default: ''
33-
# There could be a pattern for this one (complicated one)
3433
facetFilters:
35-
type: array
36-
items:
37-
type: string
38-
description: Filter hits by facet value.
39-
default: []
34+
$ref: '#/facetFilters'
4035
optionalFilters:
41-
type: array
42-
items:
43-
type: string
44-
description: Create filters for ranking purposes, where records that match the filter are ranked higher, or lower in the case of a negative optional filter.
45-
default: []
36+
$ref: '#/optionalFilters'
4637
numericFilters:
47-
type: array
48-
items:
49-
type: string
50-
description: Filter on numeric attributes.
51-
default: []
38+
$ref: '#/numericFilters'
5239
tagFilters:
53-
type: array
54-
items:
55-
type: string
56-
description: Filter hits by tags.
57-
default: []
40+
$ref: '#/tagFilters'
5841
sumOrFiltersScores:
5942
type: boolean
6043
description: Determines how to calculate the total score for filtering.
@@ -164,6 +147,8 @@ baseSearchParams:
164147
type: boolean
165148
description: Whether this search should use AI Re-Ranking.
166149
default: true
150+
reRankingApplyFilter:
151+
$ref: '#/reRankingApplyFilter'
167152

168153
searchParamsString:
169154
type: object
@@ -202,3 +187,47 @@ aroundRadius:
202187
aroundRadiusAll:
203188
type: string
204189
enum: [all]
190+
191+
# There is duplicated logic here because we want to keep a correct description
192+
# and using `$ref` override everything.
193+
searchFiltersArrayString:
194+
type: array
195+
items:
196+
type: string
197+
198+
searchFiltersNestedArrayString:
199+
type: array
200+
items:
201+
type: array
202+
items:
203+
type: string
204+
205+
facetFilters:
206+
description: Filter hits by facet value.
207+
oneOf:
208+
- $ref: '#/searchFiltersArrayString'
209+
- $ref: '#/searchFiltersNestedArrayString'
210+
211+
reRankingApplyFilter:
212+
description: When Dynamic Re-Ranking is enabled, only records that match these filters will be impacted by Dynamic Re-Ranking.
213+
oneOf:
214+
- $ref: '#/searchFiltersArrayString'
215+
- $ref: '#/searchFiltersNestedArrayString'
216+
217+
tagFilters:
218+
description: Filter hits by tags.
219+
oneOf:
220+
- $ref: '#/searchFiltersArrayString'
221+
- $ref: '#/searchFiltersNestedArrayString'
222+
223+
numericFilters:
224+
description: Filter on numeric attributes.
225+
oneOf:
226+
- $ref: '#/searchFiltersArrayString'
227+
- $ref: '#/searchFiltersNestedArrayString'
228+
229+
optionalFilters:
230+
description: Create filters for ranking purposes, where records that match the filter are ranked higher, or lower in the case of a negative optional filter.
231+
oneOf:
232+
- $ref: '#/searchFiltersArrayString'
233+
- $ref: '#/searchFiltersNestedArrayString'

specs/search/common/schemas/Hit.yml

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,9 @@ rankingInfo:
6464
type: integer
6565
description: Precision used when computing the geo distance, in meters.
6666
matchedGeoLocation:
67-
type: object
68-
additionalProperties:
69-
type: object
70-
additionalProperties: false
71-
properties:
72-
lat:
73-
type: number
74-
format: double
75-
description: Latitude of the matched location.
76-
lng:
77-
type: number
78-
format: double
79-
description: Longitude of the matched location.
80-
distance:
81-
type: integer
82-
description: Distance between the matched location and the search location (in meters).
67+
$ref: '#/matchedGeoLocation'
68+
personalization:
69+
$ref: '#/personalization'
8370
nbExactWords:
8471
type: integer
8572
description: Number of exactly matched words.
@@ -95,9 +82,21 @@ rankingInfo:
9582
userScore:
9683
type: integer
9784
description: Custom ranking for the object, expressed as a single integer value.
98-
word:
85+
words:
9986
type: integer
10087
description: Number of matched words, including prefixes and typos.
88+
promotedByReRanking:
89+
type: boolean
90+
description: Wether the record are promoted by the re-ranking strategy.
91+
required:
92+
- promoted
93+
- nbTypos
94+
- firstMatchedWord
95+
- geoDistance
96+
- nbExactWords
97+
- words
98+
- filters
99+
- userScore
101100

102101
highlightedValue:
103102
type: string
@@ -108,3 +107,31 @@ matchLevel:
108107
type: string
109108
description: Indicates how well the attribute matched the search query.
110109
enum: [none, partial, full]
110+
111+
matchedGeoLocation:
112+
type: object
113+
properties:
114+
lat:
115+
type: number
116+
format: double
117+
description: Latitude of the matched location.
118+
lng:
119+
type: number
120+
format: double
121+
description: Longitude of the matched location.
122+
distance:
123+
type: integer
124+
description: Distance between the matched location and the search location (in meters).
125+
126+
personalization:
127+
type: object
128+
properties:
129+
filtersScore:
130+
type: integer
131+
description: The score of the filters.
132+
rankingScore:
133+
type: integer
134+
description: The score of the ranking.
135+
score:
136+
type: integer
137+
description: The score of the event.

tests/output/java/src/test/java/com/algolia/methods/requests/recommend.test.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ void getRecommendationsTest1() {
234234

235235
facetFilters4.add(facetFilters_05);
236236
}
237-
queryParameters3.setFacetFilters(facetFilters4);
237+
queryParameters3.setFacetFilters(
238+
FacetFilters.ofList(facetFilters4)
239+
);
238240
}
239241
requests_02.setQueryParameters(queryParameters3);
240242

@@ -250,7 +252,9 @@ void getRecommendationsTest1() {
250252

251253
facetFilters4.add(facetFilters_05);
252254
}
253-
fallbackParameters3.setFacetFilters(facetFilters4);
255+
fallbackParameters3.setFacetFilters(
256+
FacetFilters.ofList(facetFilters4)
257+
);
254258
}
255259
requests_02.setFallbackParameters(fallbackParameters3);
256260
}
@@ -364,7 +368,9 @@ void getRecommendationsTest3() {
364368

365369
facetFilters4.add(facetFilters_05);
366370
}
367-
queryParameters3.setFacetFilters(facetFilters4);
371+
queryParameters3.setFacetFilters(
372+
FacetFilters.ofList(facetFilters4)
373+
);
368374
}
369375
requests_02.setQueryParameters(queryParameters3);
370376

@@ -380,7 +386,9 @@ void getRecommendationsTest3() {
380386

381387
facetFilters4.add(facetFilters_05);
382388
}
383-
fallbackParameters3.setFacetFilters(facetFilters4);
389+
fallbackParameters3.setFacetFilters(
390+
FacetFilters.ofList(facetFilters4)
391+
);
384392
}
385393
requests_02.setFallbackParameters(fallbackParameters3);
386394
}
@@ -521,7 +529,9 @@ void getRecommendationsTest5() {
521529

522530
facetFilters4.add(facetFilters_05);
523531
}
524-
queryParameters3.setFacetFilters(facetFilters4);
532+
queryParameters3.setFacetFilters(
533+
FacetFilters.ofList(facetFilters4)
534+
);
525535
}
526536
requests_02.setQueryParameters(queryParameters3);
527537

@@ -537,7 +547,9 @@ void getRecommendationsTest5() {
537547

538548
facetFilters4.add(facetFilters_05);
539549
}
540-
fallbackParameters3.setFacetFilters(facetFilters4);
550+
fallbackParameters3.setFacetFilters(
551+
FacetFilters.ofList(facetFilters4)
552+
);
541553
}
542554
requests_02.setFallbackParameters(fallbackParameters3);
543555
}
@@ -580,7 +592,9 @@ void getRecommendationsTest5() {
580592

581593
facetFilters4.add(facetFilters_05);
582594
}
583-
queryParameters3.setFacetFilters(facetFilters4);
595+
queryParameters3.setFacetFilters(
596+
FacetFilters.ofList(facetFilters4)
597+
);
584598
}
585599
requests_12.setQueryParameters(queryParameters3);
586600

@@ -596,7 +610,9 @@ void getRecommendationsTest5() {
596610

597611
facetFilters4.add(facetFilters_05);
598612
}
599-
fallbackParameters3.setFacetFilters(facetFilters4);
613+
fallbackParameters3.setFacetFilters(
614+
FacetFilters.ofList(facetFilters4)
615+
);
600616
}
601617
requests_12.setFallbackParameters(fallbackParameters3);
602618
}

0 commit comments

Comments
 (0)