Skip to content

Commit 0d540fa

Browse files
committed
cts is generating
1 parent 2b98ad6 commit 0d540fa

File tree

16 files changed

+119
-76
lines changed

16 files changed

+119
-76
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,11 @@ private void flattenBody(CodegenOperation ope) {
190190
return;
191191
}
192192

193-
// check for colision with other params
194-
for (CodegenProperty prop : bodyParam.getVars()) {
195-
for (CodegenParameter param : ope.allParams) {
196-
if (param.paramName.equals(prop.baseName)) {
197-
System.out.println(
198-
"Operation " +
199-
ope.operationId +
200-
" has body param " +
201-
bodyParam.paramName +
202-
" in colision with param " +
203-
param.paramName +
204-
", skipping flattening"
205-
);
206-
return;
207-
}
208-
}
193+
if (!canFlattenBody(ope)) {
194+
System.out.println(
195+
"Operation " + ope.operationId + " has body param " + bodyParam.paramName + " in colision with a parameter, skipping flattening"
196+
);
197+
return;
209198
}
210199

211200
bodyParam.vendorExtensions.put("x-flat-body", bodyParam.getVars().size() > 0);
@@ -259,6 +248,25 @@ private void flattenBody(CodegenOperation ope) {
259248
}
260249
}
261250

251+
public static boolean canFlattenBody(CodegenOperation ope) {
252+
if (ope.bodyParam == null || !ope.bodyParam.isModel) {
253+
return false;
254+
}
255+
256+
if (ope.allParams.size() == 1) {
257+
return true;
258+
}
259+
260+
for (CodegenProperty prop : ope.bodyParam.getVars()) {
261+
for (CodegenParameter param : ope.allParams) {
262+
if (param.paramName.equals(prop.baseName)) {
263+
return false;
264+
}
265+
}
266+
}
267+
return true;
268+
}
269+
262270
public static String toEnum(String value) {
263271
return INSTANCE.toEnumVarName(value, "String");
264272
}

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.codegen.cts.tests;
22

3+
import com.algolia.codegen.AlgoliaGoGenerator;
34
import com.algolia.codegen.AlgoliaSwiftGenerator;
45
import com.algolia.codegen.exceptions.*;
56
import com.algolia.codegen.utils.*;
@@ -93,19 +94,8 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
9394
operation.bodyParam.isModel &&
9495
operation.bodyParam.getVars().size() > 0
9596
) {
96-
// check for colision with other params
97-
boolean hasCollision = false;
98-
for (CodegenProperty prop : operation.bodyParam.getVars()) {
99-
for (CodegenParameter otherParam : operation.allParams) {
100-
if (otherParam.paramName.equals(prop.baseName)) {
101-
hasCollision = true;
102-
break;
103-
}
104-
}
105-
}
106-
if (!hasCollision) {
97+
if (AlgoliaGoGenerator.canFlattenBody(operation)) {
10798
// flatten the body params by skipping one level
108-
System.out.println("Flatten the body in " + operation.operationId);
10999
Map<String, Object> bodyParams = (Map<String, Object>) param.getValue();
110100
for (String nestedParam : bodyParams.keySet()) {
111101
for (CodegenProperty prop : operation.bodyParam.getVars()) {
@@ -117,7 +107,11 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
117107
}
118108
}
119109
}
120-
// sortParameters(operation.bodyParam, parametersWithDataType);
110+
} else {
111+
// use the parameter as is
112+
Map<String, Object> paramWithType = traverseParams(param.getKey(), param.getValue(), specParam, "", 0, false);
113+
parametersWithDataType.add(paramWithType);
114+
parametersWithDataTypeMap.put((String) paramWithType.get("key"), paramWithType);
121115
}
122116
} else {
123117
Map<String, Object> paramWithType = traverseParams(param.getKey(), param.getValue(), specParam, "", 0, false);
@@ -126,9 +120,8 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
126120
}
127121
}
128122
}
129-
} else if (language.equals("go") && parameters != null) {
123+
} else if (language.equals("go") && parameters != null && operation.bodyParam.getVars().size() > 0) {
130124
// also flatten when the body is the only parameter
131-
System.out.println("Skipping unique body in " + operation.operationId);
132125
for (String nestedParam : parameters.keySet()) {
133126
for (CodegenProperty prop : operation.bodyParam.getVars()) {
134127
if (prop.baseName.equals(nestedParam)) {
@@ -139,7 +132,6 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
139132
}
140133
}
141134
}
142-
// sortParameters(operation.bodyParam, parametersWithDataType);
143135
} else {
144136
Map<String, Object> paramWithType = traverseParams(paramName, parameters, spec, "", 0, false);
145137
parametersWithDataType.add(paramWithType);

generators/src/main/java/com/algolia/codegen/cts/tests/Snippet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ public void addMethodCall(Map<String, Object> context, ParametersWithDataType pa
5353
}
5454

5555
try {
56+
boolean isHelper = (boolean) ope.vendorExtensions.getOrDefault("x-helper", false);
5657
context.put("isGeneric", (boolean) ope.vendorExtensions.getOrDefault("x-is-generic", false));
5758
context.put("isCustomRequest", Helpers.CUSTOM_METHODS.contains(ope.operationIdOriginal));
5859
context.put("isAsyncMethod", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
5960
context.put("hasParams", ope.getHasParams());
60-
context.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
61+
context.put("isHelper", isHelper);
6162
context.put("hasRequestOptions", requestOptions != null);
6263

6364
if (requestOptions != null) {
@@ -86,7 +87,7 @@ public void addMethodCall(Map<String, Object> context, ParametersWithDataType pa
8687
}
8788
}
8889

89-
TestsGenerator.setOptionalParameters(ope, context, parameters);
90+
TestsGenerator.setOptionalParameters(ope, context, parameters, isHelper);
9091

9192
paramsType.enhanceParameters(parameters, context, ope);
9293
} catch (CTSException e) {

generators/src/main/java/com/algolia/codegen/cts/tests/TestsClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
8080
testOut.put("autoCreateClient", test.autoCreateClient);
8181
testOut.put("useEchoRequester", true);
8282
testOut.put("isBenchmark", withBenchmark);
83+
84+
if (language.equals("go") && "`addApiKey` throws with invalid parameters".equals(test.testName)) {
85+
// skip this test because the body is flattened in go
86+
continue;
87+
}
88+
8389
for (Step step : test.steps) {
8490
Map<String, Object> stepOut = new HashMap<>();
8591
if (step.times > 1) stepOut.put("times", step.times);
@@ -158,7 +164,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
158164
// default to true because most api calls are asynchronous
159165
testOut.put("isAsyncMethod", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
160166

161-
setOptionalParameters(ope, stepOut, step.parameters);
167+
setOptionalParameters(ope, stepOut, step.parameters, isHelper);
162168
addRequestOptions(paramsType, step.requestOptions, stepOut);
163169

164170
methodCount++;
@@ -180,6 +186,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
180186
item -> (boolean) item.getOrDefault("isNullObject", false) || (boolean) item.getOrDefault("isNull", false)
181187
);
182188
if (isNotTestable) {
189+
System.out.println("Skipping test " + test.testName + " for " + language + " because of nil object");
183190
continue;
184191
}
185192
}

generators/src/main/java/com/algolia/codegen/cts/tests/TestsGenerator.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.codegen.cts.tests;
22

3+
import com.algolia.codegen.AlgoliaGoGenerator;
34
import com.algolia.codegen.cts.manager.CTSManager;
45
import com.algolia.codegen.exceptions.CTSException;
56
import com.algolia.codegen.utils.*;
@@ -129,36 +130,29 @@ protected void addRequestOptions(ParametersWithDataType paramsType, RequestOptio
129130
}
130131
}
131132

132-
public static void setOptionalParameters(CodegenOperation ope, Map<String, Object> test, Map<String, Object> parameters) {
133+
public static void setOptionalParameters(
134+
CodegenOperation ope,
135+
Map<String, Object> test,
136+
Map<String, Object> parameters,
137+
boolean isHelper
138+
) {
133139
int bodyPropsOptional = 0;
134140
boolean actuallyHasOptional = false;
135-
if (ope.bodyParam != null && ope.bodyParam.isModel) {
136-
// check for colision with other params
137-
boolean hasCollision = false;
138-
Map<String, Object> paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
139-
if (ope.allParams.size() == 1) { // edge case where the body is already flattened
140-
paramBody = parameters;
141+
142+
if (AlgoliaGoGenerator.canFlattenBody(ope)) {
143+
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
144+
145+
// edge case where the body is already flattened
146+
Map<String, Object> paramBody = paramBody = parameters;
147+
if (ope.allParams.size() > 1) {
148+
paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
141149
}
142150

143-
System.out.println(ope.bodyParam.paramName + " len " + ope.bodyParam.getVars().size());
144151
for (CodegenProperty prop : ope.bodyParam.getVars()) {
145-
for (CodegenParameter param : ope.allParams) {
146-
if (param.paramName.equals(prop.baseName)) {
147-
hasCollision = true;
148-
}
149-
}
150-
151-
if (paramBody != null) System.out.println(
152-
prop.baseName + " is required " + prop.required + " " + paramBody.containsKey(prop.baseName)
153-
);
154152
if (!prop.required && paramBody != null && paramBody.containsKey(prop.baseName)) {
155153
actuallyHasOptional = true;
156154
}
157155
}
158-
159-
if (!hasCollision) {
160-
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
161-
}
162156
}
163157

164158
int totalOptional = ope.optionalParams.size() + bodyPropsOptional;
@@ -170,14 +164,29 @@ public static void setOptionalParameters(CodegenOperation ope, Map<String, Objec
170164
}
171165
}
172166

167+
// I can't figure out the correct condition for this one so it's harcoded for now
168+
boolean isSFFV = ope.operationId.equals("searchForFacetValues") && "composition".equals(ope.tags.get(0).getName());
169+
173170
// hasOptionalWrapper if there is more that one optional param, after the body has been
174171
// flattened, only relevant for go
175-
test.put("hasOptionalWrapper", (totalOptional > 1) && actuallyHasOptional);
176-
test.put("hasNilOptional", (totalOptional > 0) && !actuallyHasOptional);
177-
test.put("hasOptionalRequired", (totalOptional == 1) && actuallyHasOptional);
178-
179-
System.out.println(ope.operationId + " hasOptionalWrapper: " + test.get("hasOptionalWrapper"));
180-
System.out.println("hasNilOptional: " + test.get("hasNilOptional"));
181-
System.out.println("hasOptionalRequired: " + test.get("hasOptionalRequired"));
172+
test.put("hasOptionalWrapper", totalOptional > 1 && actuallyHasOptional && !isSFFV);
173+
test.put("hasInlineOptional", (totalOptional == 1 || isSFFV) && actuallyHasOptional);
174+
test.put("hasNilOptional", totalOptional > 0 && !actuallyHasOptional && !isHelper);
175+
176+
System.out.println(
177+
ope.operationId +
178+
" hasOptionalWrapper: " +
179+
test.get("hasOptionalWrapper") +
180+
" hasNilOptional: " +
181+
test.get("hasNilOptional") +
182+
" hasInlineOptional: " +
183+
test.get("hasInlineOptional") +
184+
" totalOptional: " +
185+
totalOptional +
186+
" actuallyHasOptional: " +
187+
actuallyHasOptional +
188+
" bodyPropsOptional: " +
189+
bodyPropsOptional
190+
);
182191
}
183192
}

generators/src/main/java/com/algolia/codegen/cts/tests/TestsRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
171171
test.put("hasParams", ope.getHasParams());
172172
test.put("isHelper", isHelper);
173173

174-
setOptionalParameters(ope, test, req.parameters);
174+
setOptionalParameters(ope, test, req.parameters, isHelper);
175175
addRequestOptions(paramsType, req.requestOptions, test);
176176

177177
// Determines whether the endpoint is expected to return a response payload deserialized

templates/go/client.mustache

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,25 @@ func reportError(format string, a ...any) error {
312312
return fmt.Errorf(format, a...)
313313
}
314314

315-
// Set request body from an any.
315+
// A wrapper for strict JSON decoding
316+
func newStrictDecoder(data []byte) *json.Decoder {
317+
dec := json.NewDecoder(bytes.NewBuffer(data))
318+
dec.DisallowUnknownFields()
319+
return dec
320+
}
321+
322+
// A wrapper for validating a struct, returns nil if value is not a struct
323+
func validateStruct(v any) error {
324+
err := validator.New().Struct(v)
325+
validationErrors, ok := err.(validator.ValidationErrors)
326+
if ok && len(validationErrors) > 0 {
327+
return validationErrors
328+
}
329+
330+
return nil
331+
}
332+
333+
// Set request body from an any
316334
func setBody(body any, c compression.Compression) (*bytes.Buffer, error) {
317335
if body == nil {
318336
return nil, nil
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#hasInlineOptional}}{{#parametersWithDataType}}{{^required}}{{^isFreeFormObject}}{{^isObject}}utils.ToPtr({{/isObject}}{{/isFreeFormObject}}{{> tests/generateParams}}{{^isFreeFormObject}}{{^isObject}}){{/isObject}}{{/isFreeFormObject}},{{/required}}{{/parametersWithDataType}}{{/hasInlineOptional}}

templates/go/tests/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{#parametersWithDataType}}{{#required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}} {{#hasOptionalWrapper}}{{clientPrefix}}.New{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Options(){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{#requestOptions}},{{/requestOptions}}{{/hasOptionalWrapper}}{{#hasNilOptional}}nil,{{/hasNilOptional}}{{#hasOptionalRequired}}{{#parametersWithDataType}}{{^required}}{{^isFreeFormObject}}utils.ToPtr({{/isFreeFormObject}}{{> tests/generateParams}}{{^isFreeFormObject}}){{/isFreeFormObject}},{{/required}}{{/parametersWithDataType}}{{/hasOptionalRequired}}{{#requestOptions}}{{#queryParameters.parametersWithDataType}}{{clientPrefix}}.WithQueryParam("{{{key}}}", {{> tests/generateInnerParams}}),{{/queryParameters.parametersWithDataType}}{{#headers.parametersWithDataType}}{{clientPrefix}}.WithHeaderParam("{{{key}}}", {{> tests/generateInnerParams}}),{{/headers.parametersWithDataType}} {{#timeouts.read}} ,{{clientPrefix}}.WithReadTimeout({{.}} * time.Millisecond), {{/timeouts.read}} {{#timeouts.write}} {{clientPrefix}}.WithWriteTimeout({{.}} * time.Millisecond), {{/timeouts.write}} {{#timeouts.connect}} ,{{clientPrefix}}.WithConnectTimeout({{.}} * time.Millisecond), {{/timeouts.connect}} {{/requestOptions}})
1+
client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{> tests/requiredParams}}{{> tests/optionalWrapper}}{{> tests/nilWrapper}}{{> tests/inlineOptional}}{{> tests/requestOptions}})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#hasNilOptional}}nil,{{/hasNilOptional}}

0 commit comments

Comments
 (0)