Skip to content

Commit f51984f

Browse files
authored
fix(java): escape oneOf naming (#268)
1 parent 3d5c440 commit f51984f

File tree

8 files changed

+43
-14
lines changed

8 files changed

+43
-14
lines changed

.github/actions/cache/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ runs:
326326
hashFiles(
327327
'clients/algoliasearch-client-java-2/**',
328328
'!clients/algoliasearch-client-java-2/target',
329+
'templates/java/**',
329330
'specs/bundled/search.yml'
330331
)}}
331332

.github/workflows/check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ jobs:
202202
hashFiles(
203203
format('{0}/**', matrix.client.folder),
204204
format('!{0}/target', matrix.client.folder),
205+
'templates/java/**',
205206
format('specs/bundled/{0}.yml', matrix.client.name)
206207
)}}
207208

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ dist
2323
.openapi-generator
2424

2525
tests/output/*/.openapi-generator-ignore
26+
27+
generators/bin

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,19 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
115115
CodegenModel model = ((Map<String, List<Map<String, CodegenModel>>>) modelContainer).get("models").get(0)
116116
.get("model");
117117
if (!model.oneOf.isEmpty()) {
118+
List<HashMap<String, String>> listOneOf = new ArrayList();
119+
120+
for (String iterateModel : model.oneOf) {
121+
HashMap<String, String> hashMapOneOf = new HashMap();
122+
123+
hashMapOneOf.put("type", iterateModel);
124+
hashMapOneOf.put("name", iterateModel.replace("<", "").replace(">", ""));
125+
126+
listOneOf.add(hashMapOneOf);
127+
}
128+
118129
model.vendorExtensions.put("x-is-one-of-interface", true);
130+
model.vendorExtensions.put("x-is-one-of-list", listOneOf);
119131
}
120132
}
121133

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ private void handleModel(String paramName, Object param, Map<String, Object> tes
165165
IJsonSchemaValidationProperties match = findMatchingOneOf(param, model);
166166
testOutput.clear();
167167
testOutput.putAll(traverseParams(paramName, param, match, parent, suffix));
168-
testOutput.put("oneOfModel", baseType);
168+
169+
HashMap<String, String> hashMapOneOfModel = new HashMap();
170+
171+
hashMapOneOfModel.put("classname", baseType);
172+
hashMapOneOfModel.put("name", getTypeName(match).replace("<", "").replace(">", ""));
173+
174+
testOutput.put("oneOfModel", hashMapOneOfModel);
175+
169176
return;
170177
}
171178

templates/java/oneof_interface.mustache

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import java.io.IOException;
1111
@JsonAdapter({{classname}}.Adapter.class)
1212
{{>additionalModelTypeAnnotations}}
1313
public abstract class {{classname}} implements CompoundType {
14-
{{#oneOf}}
15-
public static {{classname}} of({{.}} inside) {
16-
return new {{classname}}{{.}}(inside);
14+
{{#vendorExtensions.x-is-one-of-list}}
15+
public static {{classname}} of{{name}}({{{type}}} inside) {
16+
return new {{classname}}{{name}}(inside);
1717
}
1818

19-
{{/oneOf}}
19+
{{/vendorExtensions.x-is-one-of-list}}
2020
public abstract Object getInsideValue();
2121

2222
public static class Adapter extends TypeAdapter<{{classname}}> {
@@ -37,18 +37,18 @@ public abstract class {{classname}} implements CompoundType {
3737
}
3838
}
3939

40-
{{#oneOf}}
40+
{{#vendorExtensions.x-is-one-of-list}}
4141
@JsonAdapter({{classname}}.Adapter.class)
42-
class {{classname}}{{.}} extends {{classname}} {
43-
private final {{.}} insideValue;
42+
class {{classname}}{{name}} extends {{classname}} {
43+
private final {{{type}}} insideValue;
4444

45-
{{classname}}{{.}}({{.}} insideValue) {
45+
{{classname}}{{name}}({{{type}}} insideValue) {
4646
this.insideValue = insideValue;
4747
}
4848

4949
@Override
50-
public {{.}} getInsideValue() {
50+
public {{{type}}} getInsideValue() {
5151
return insideValue;
5252
}
5353
}
54-
{{/oneOf}}
54+
{{/vendorExtensions.x-is-one-of-list}}

tests/CTS/methods/requests/templates/java/requests.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class {{client}}Tests {
3636
{{#parametersWithDataType}}{{> generateParams}}{{/parametersWithDataType}}
3737

3838
EchoResponseInterface req = (EchoResponseInterface) assertDoesNotThrow(() -> {
39-
return client.{{method}}({{#parametersWithDataType}}{{#oneOfModel}}{{{.}}}.of({{{key}}}{{suffix}}){{/oneOfModel}}{{^oneOfModel}}{{{key}}}{{suffix}}{{/oneOfModel}}{{^-last}},{{/-last}}{{/parametersWithDataType}});
39+
return client.{{method}}({{#parametersWithDataType}}{{#oneOfModel}}{{{classname}}}.of{{{name}}}({{{key}}}{{suffix}}){{/oneOfModel}}{{^oneOfModel}}{{{key}}}{{suffix}}{{/oneOfModel}}{{^-last}},{{/-last}}{{/parametersWithDataType}});
4040
});
4141

4242
assertEquals(req.getPath(), "{{{request.path}}}");

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,10 @@ void deleteByTest0() {
723723
}
724724

725725
EchoResponseInterface req = (EchoResponseInterface) assertDoesNotThrow(() -> {
726-
return client.deleteBy(indexName0, SearchParams.of(searchParams0));
726+
return client.deleteBy(
727+
indexName0,
728+
SearchParams.ofSearchParamsObject(searchParams0)
729+
);
727730
}
728731
);
729732

@@ -1722,7 +1725,10 @@ void searchTest0() {
17221725
}
17231726

17241727
EchoResponseInterface req = (EchoResponseInterface) assertDoesNotThrow(() -> {
1725-
return client.search(indexName0, SearchParams.of(searchParams0));
1728+
return client.search(
1729+
indexName0,
1730+
SearchParams.ofSearchParamsObject(searchParams0)
1731+
);
17261732
}
17271733
);
17281734

0 commit comments

Comments
 (0)