Skip to content

Commit 5655451

Browse files
committed
self review
1 parent b28ecf5 commit 5655451

File tree

4 files changed

+54
-82
lines changed

4 files changed

+54
-82
lines changed

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.algolia.codegen.cts.lambda.ScreamingSnakeCaseLambda;
44
import com.algolia.codegen.exceptions.*;
55
import com.algolia.codegen.utils.*;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.google.common.collect.ImmutableMap;
79
import com.google.common.collect.Iterables;
810
import com.samskivert.mustache.Mustache;
@@ -206,36 +208,13 @@ private void flattenBody(CodegenOperation ope) {
206208
}
207209

208210
for (CodegenProperty prop : bodyParam.getVars()) {
209-
// there is no easy way to convert a prop to a param, we need to copy all the fields
210-
CodegenParameter param = new CodegenParameter();
211-
212211
prop.nameInLowerCase = toParamName(prop.baseName);
212+
213+
CodegenParameter param = new ObjectMapper()
214+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
215+
.convertValue(prop, CodegenParameter.class);
213216
param.nameInPascalCase = Helpers.capitalize(prop.baseName);
214217
param.paramName = toParamName(prop.baseName);
215-
param.baseName = prop.baseName;
216-
param.baseType = prop.baseType;
217-
param.dataType = prop.dataType;
218-
param.datatypeWithEnum = prop.datatypeWithEnum;
219-
param.description = prop.description;
220-
param.example = prop.example;
221-
param.isModel = prop.isModel;
222-
param.isArray = prop.isArray;
223-
param.isContainer = prop.isContainer;
224-
param.isMap = prop.isMap;
225-
param.isEnum = prop.isEnum;
226-
param.isEnumRef = prop.isEnumRef;
227-
param.isPrimitiveType = prop.isPrimitiveType;
228-
param.isString = prop.isString;
229-
param.isNumeric = prop.isNumeric;
230-
param.isBoolean = prop.isBoolean;
231-
param.isDate = prop.isDate;
232-
param.isDateTime = prop.isDateTime;
233-
param.isFreeFormObject = prop.isFreeFormObject;
234-
param.isNullable = prop.isNullable;
235-
param.jsonSchema = prop.jsonSchema;
236-
param.required = prop.required;
237-
param.vendorExtensions = prop.vendorExtensions;
238-
param.allowableValues = prop.allowableValues;
239218

240219
if (prop.required) {
241220
ope.requiredParams.add(param);

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
7272
if (paramName == null) {
7373
if (parameters != null) {
7474
for (Entry<String, Object> param : parameters.entrySet()) {
75-
IJsonSchemaValidationProperties specParam = null;
75+
CodegenParameter specParam = null;
7676
if (operation != null) {
7777
for (CodegenParameter sp : operation.allParams) {
7878
if (sp.paramName.equals(param.getKey())) {
@@ -84,34 +84,27 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
8484
throw new CTSException("Parameter " + param.getKey() + " not found in the root parameter");
8585
}
8686
}
87-
// for go, we flatten the body params
8887
if (
8988
language.equals("go") &&
9089
specParam != null &&
91-
((CodegenParameter) specParam).isBodyParam &&
90+
specParam.isBodyParam &&
9291
operation != null &&
9392
operation.bodyParam != null &&
9493
operation.bodyParam.isModel &&
95-
operation.bodyParam.getVars().size() > 0
94+
operation.bodyParam.getVars().size() > 0 &&
95+
AlgoliaGoGenerator.canFlattenBody(operation)
9696
) {
97-
if (AlgoliaGoGenerator.canFlattenBody(operation)) {
98-
// flatten the body params by skipping one level
99-
Map<String, Object> bodyParams = (Map<String, Object>) param.getValue();
100-
for (String nestedParam : bodyParams.keySet()) {
101-
for (CodegenProperty prop : operation.bodyParam.getVars()) {
102-
if (prop.baseName.equals(nestedParam)) {
103-
Map<String, Object> paramWithType = traverseParams(prop.baseName, bodyParams.get(nestedParam), prop, "", 0, false);
104-
parametersWithDataType.add(paramWithType);
105-
parametersWithDataTypeMap.put((String) paramWithType.get("key"), paramWithType);
106-
break;
107-
}
97+
// flatten the body params by skipping one level
98+
Map<String, Object> bodyParams = (Map<String, Object>) param.getValue();
99+
for (String nestedParam : bodyParams.keySet()) {
100+
for (CodegenProperty prop : operation.bodyParam.getVars()) {
101+
if (prop.baseName.equals(nestedParam)) {
102+
Map<String, Object> paramWithType = traverseParams(prop.baseName, bodyParams.get(nestedParam), prop, "", 0, false);
103+
parametersWithDataType.add(paramWithType);
104+
parametersWithDataTypeMap.put((String) paramWithType.get("key"), paramWithType);
105+
break;
108106
}
109107
}
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);
115108
}
116109
} else {
117110
Map<String, Object> paramWithType = traverseParams(param.getKey(), param.getValue(), specParam, "", 0, false);

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -137,61 +137,61 @@ public static void setOptionalParameters(
137137
Map<String, Object> parameters,
138138
boolean isHelper
139139
) {
140-
if (!language.equals("go")) {
141-
return;
142-
}
140+
if (!"go".equals(language)) return;
141+
142+
boolean isBodyRequired = ope.bodyParam != null && ope.bodyParam.required;
143+
boolean alreadyInlinedBody = ope.allParams.size() == 1 && ope.bodyParam != null && !ope.bodyParam.isArray;
144+
// I can't figure out the correct condition for this one so it's harcoded for now
145+
boolean isSFFV =
146+
"searchForFacetValues".equals(ope.operationId) && !ope.tags.isEmpty() && "composition".equals(ope.tags.get(0).getName());
147+
143148
int bodyPropsOptional = 0;
144-
boolean actuallyHasOptional = false;
145149
boolean isBodyTooBig = false;
146-
boolean isBodyRequired = (ope.bodyParam != null && ope.bodyParam.required);
147-
boolean alreadyInlinedBody = ope.allParams.size() == 1 && ope.bodyParam != null && !ope.bodyParam.isArray;
150+
boolean actuallyHasOptional = false;
148151

149-
if (AlgoliaGoGenerator.canFlattenBody(ope)) {
150-
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
151-
isBodyTooBig = ope.bodyParam.getVars().size() == 0;
152+
if (AlgoliaGoGenerator.canFlattenBody(ope) && ope.bodyParam != null) {
153+
List<CodegenProperty> vars = ope.bodyParam.getVars();
154+
bodyPropsOptional = (int) vars.stream().filter(p -> !p.required).count();
155+
isBodyTooBig = vars.isEmpty();
152156

153-
// edge case where the body is already flattened
154-
Map<String, Object> paramBody = paramBody = parameters;
157+
Map<String, Object> paramBody = parameters;
155158
if (!alreadyInlinedBody) {
156-
Object paramBodyObj = parameters.get(ope.bodyParam.paramName);
157-
if (paramBodyObj instanceof String) {
158-
// this is a verbatim paramater, we use it as is
159-
System.out.println(ope.operationId + " is a verbatim body " + paramBodyObj);
159+
Object paramObj = parameters.get(ope.bodyParam.paramName);
160+
if (paramObj instanceof String) {
160161
actuallyHasOptional = !isBodyRequired;
161-
} else {
162-
paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
162+
} else if (paramObj instanceof Map) {
163+
paramBody = (Map<String, Object>) paramObj;
163164
}
164165
}
165166

166-
for (CodegenProperty prop : ope.bodyParam.getVars()) {
167+
for (CodegenProperty prop : vars) {
167168
if (!prop.required && paramBody != null && paramBody.containsKey(prop.baseName)) {
168169
actuallyHasOptional = true;
169170
}
170171
}
171172
}
172173

173-
int totalOptional = ope.optionalParams.size() + bodyPropsOptional;
174-
175174
for (CodegenParameter param : ope.allParams) {
176175
if (!param.required && parameters.containsKey(param.baseName)) {
177176
actuallyHasOptional = true;
178177
break;
179178
}
180179
}
181180

182-
// I can't figure out the correct condition for this one so it's harcoded for now
183-
boolean isSFFV = ope.operationId.equals("searchForFacetValues") && "composition".equals(ope.tags.get(0).getName());
181+
int totalOptional = ope.optionalParams.size() + bodyPropsOptional;
184182

185183
// hasOptionalWrapper if there is more that one optional param, after the body has been
186-
// flattened, only relevant for go
187-
test.put("hasOptionalWrapper", totalOptional > 1 && actuallyHasOptional && !isSFFV);
188-
test.put("hasInlineOptional", ((totalOptional == 1 || isSFFV) && actuallyHasOptional) || isBodyTooBig);
189-
if (isBodyTooBig) {
190-
boolean isBodySet = alreadyInlinedBody ? parameters.size() > 0 : parameters.containsKey(ope.bodyParam.paramName);
191-
System.out.println(ope.operationId + " isBodySet: " + isBodySet + " isBodyRequired: " + isBodyRequired);
192-
test.put("hasNilOptional", isBodyRequired ? totalOptional > 0 && !actuallyHasOptional : !isBodySet);
193-
} else {
194-
test.put("hasNilOptional", totalOptional > 0 && !actuallyHasOptional && !isHelper);
184+
// flattened.
185+
boolean hasOptionalWrapper = totalOptional > 1 && actuallyHasOptional && !isSFFV;
186+
boolean hasInlineOptional = ((totalOptional == 1 || isSFFV) && actuallyHasOptional) || isBodyTooBig;
187+
boolean hasNilOptional = totalOptional > 0 && !actuallyHasOptional && !isHelper;
188+
if (isBodyTooBig && !isBodyRequired) {
189+
boolean isBodySet = alreadyInlinedBody ? !parameters.isEmpty() : parameters.containsKey(ope.bodyParam.paramName);
190+
hasNilOptional = !isBodySet;
195191
}
192+
193+
test.put("hasOptionalWrapper", hasOptionalWrapper);
194+
test.put("hasInlineOptional", hasInlineOptional);
195+
test.put("hasNilOptional", hasNilOptional);
196196
}
197197
}

templates/go/api.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,10 @@ func (o *{{operationId}}Options) With{{#lambda.titlecase}}{{baseName}}{{/lambda.
350350
// Parameters:
351351
{{/hasParams}}
352352
{{#requiredParams}}
353-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}}
353+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}}
354354
{{/requiredParams}}
355355
{{#optionalParams}}
356-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}} (in optionalParams)
356+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}} (in optionalParams)
357357
{{/optionalParams}}
358358
// - opts - Optional parameters for the API call (e.g. WithContext, WithHeaderParam...)
359359
{{#isDeprecated}}
@@ -401,10 +401,10 @@ func (c *APIClient) {{nickname}}({{#requiredParams}}{{paramName}} {{#required}}{
401401
// Parameters:
402402
{{/hasParams}}
403403
{{#requiredParams}}
404-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}}
404+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}}
405405
{{/requiredParams}}
406406
{{#optionalParams}}
407-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}} (in optionalParams)
407+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}} (in optionalParams)
408408
{{/optionalParams}}
409409
// - opts - Optional parameters for the API call (e.g. WithContext, WithHeaderParam...)
410410
{{#isDeprecated}}

0 commit comments

Comments
 (0)