Skip to content

Commit 2838123

Browse files
authored
fix(spec): better filters type (#413)
1 parent 61cd455 commit 2838123

File tree

13 files changed

+291
-84
lines changed

13 files changed

+291
-84
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.0.9.0.8
1+
8.0.10.0.8

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ jobs:
222222
with:
223223
path: |
224224
${{ format('{0}/algoliasearch-core/src/com/algolia/api/{1}.java', matrix.client.path, matrix.client.api) }}
225-
${{ format('{0}/{1}/**', matrix.client.path, matrix.client.capitalizedName) }}
225+
${{ format('{0}/algoliasearch-core/src/com/algolia/model/{1}/**', matrix.client.path, matrix.client.camelizedName) }}
226226
key: |
227227
${{ env.CACHE_VERSION }}-${{
228228
hashFiles(

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
152152
.get(0)
153153
.get("model");
154154
if (!model.oneOf.isEmpty()) {
155-
List<HashMap<String, String>> listOneOf = new ArrayList();
155+
List<HashMap<String, String>> oneOfList = new ArrayList();
156156

157157
for (String iterateModel : model.oneOf) {
158158
HashMap<String, String> oneOfModel = new HashMap();
@@ -163,11 +163,11 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
163163
iterateModel.replace("<", "").replace(">", "")
164164
);
165165

166-
listOneOf.add(oneOfModel);
166+
oneOfList.add(oneOfModel);
167167
}
168168

169169
model.vendorExtensions.put("x-is-one-of-interface", true);
170-
model.vendorExtensions.put("x-is-one-of-list", listOneOf);
170+
model.vendorExtensions.put("x-is-one-of-list", oneOfList);
171171
}
172172
}
173173

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

Lines changed: 44 additions & 6 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) {
@@ -224,12 +242,21 @@ private void handleModel(
224242
);
225243

226244
HashMap<String, String> oneOfModel = new HashMap<>();
245+
String typeName = getTypeName(match).replace("<", "").replace(">", "");
227246

228247
oneOfModel.put("classname", Utils.capitalize(baseType));
229-
oneOfModel.put(
230-
"name",
231-
getTypeName(match).replace("<", "").replace(">", "")
232-
);
248+
249+
if (typeName.equals("List")) {
250+
CodegenProperty items = match.getItems();
251+
252+
if (items == null) {
253+
throw new CTSException("Unhandled case for empty oneOf List items.");
254+
}
255+
256+
typeName += getTypeName(items);
257+
}
258+
259+
oneOfModel.put("name", typeName);
233260
testOutput.put("oneOfModel", oneOfModel);
234261

235262
return;
@@ -466,7 +493,18 @@ private IJsonSchemaValidationProperties findMatchingOneOf(
466493
return bestOneOf;
467494
}
468495
if (param instanceof List) {
469-
// no idea for list
496+
// NICE ---> no idea for list <--- NICE
497+
CodegenComposedSchemas composedSchemas = model.getComposedSchemas();
498+
499+
if (composedSchemas != null) {
500+
List<CodegenProperty> oneOf = composedSchemas.getOneOf();
501+
502+
// Somehow this is not yet enough
503+
if (oneOf != null && !oneOf.isEmpty()) {
504+
return oneOf.get(0);
505+
}
506+
}
507+
470508
return null;
471509
}
472510

scripts/ci/createMatrix.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CLIENTS, GENERATORS } from '../common';
2-
import { createClientName } from '../cts/utils';
2+
import { camelize, createClientName } from '../cts/utils';
33
import type { Language } from '../types';
44

55
import { getNbGitDiff } from './utils';
@@ -16,9 +16,10 @@ type BaseMatrix = {
1616
};
1717

1818
type ClientMatrix = BaseMatrix & {
19-
config?: string;
20-
api?: string;
21-
capitalizedName?: string;
19+
config: string;
20+
api: string;
21+
capitalizedName: string;
22+
camelizedName: string;
2223
};
2324

2425
type SpecMatrix = BaseMatrix;
@@ -56,18 +57,16 @@ async function getClientMatrix({
5657
continue;
5758
}
5859

59-
const matchedGenerator: ClientMatrix = {
60-
name: client,
61-
path: output,
62-
};
63-
6460
const clientName = createClientName(client, language);
6561

66-
matchedGenerator.config = `${clientName}Config`;
67-
matchedGenerator.api = `${clientName}Client`;
68-
matchedGenerator.capitalizedName = clientName;
69-
70-
matrix.client.push(matchedGenerator);
62+
matrix.client.push({
63+
name: client,
64+
path: output,
65+
config: `${clientName}Config`,
66+
api: `${clientName}Client`,
67+
capitalizedName: clientName,
68+
camelizedName: camelize(client),
69+
});
7170
}
7271

7372
return matrix;

scripts/cts/utils.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,47 @@ export async function* walk(
1414
}
1515
}
1616

17+
/**
18+
* Sets the first letter of the given string in capital.
19+
*
20+
* `searchClient` -> `SearchClient`.
21+
*/
1722
export function capitalize(str: string): string {
1823
return str.charAt(0).toUpperCase() + str.slice(1);
1924
}
2025

21-
export function createClientName(client: string, language: string): string {
22-
const clientName = client
23-
.split('-')
26+
/**
27+
* Splits a string for a given `delimiter` (defaults to `-`) and capitalize each
28+
* parts except the first letter.
29+
*
30+
* `search-client` -> `searchClient`.
31+
*/
32+
export function camelize(str: string, delimiter: string = '-'): string {
33+
return str
34+
.split(delimiter)
2435
.map((part, i) => {
25-
if (language === 'javascript' && i === 0) {
36+
if (i === 0) {
2637
return part;
2738
}
2839

2940
return capitalize(part);
3041
})
3142
.join('');
43+
}
44+
45+
/**
46+
* Returns the client name with the correct casing for its language.
47+
*
48+
* `search-client`, `java` -> `SearchClient`.
49+
*
50+
* `search-client`, `javascript` -> `searchClient`.
51+
*/
52+
export function createClientName(client: string, language: string): string {
53+
if (language === 'javascript') {
54+
return camelize(client);
55+
}
3256

33-
return clientName;
57+
return capitalize(camelize(client));
3458
}
3559

3660
export async function createOutputDir({

scripts/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,7 @@ buildCommand
194194
clientsTodo = CLIENTS;
195195
}
196196
// ignore cache when building from cli
197-
await buildSpecs(
198-
clientsTodo,
199-
outputFormat!,
200-
Boolean(verbose),
201-
!skipCache
202-
);
197+
await buildSpecs(clientsTodo, outputFormat, Boolean(verbose), !skipCache);
203198
}
204199
);
205200

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'

0 commit comments

Comments
 (0)