Skip to content

Commit 05229fe

Browse files
committed
self review
1 parent 286fbb0 commit 05229fe

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
@@ -141,61 +141,61 @@ public static void setOptionalParameters(
141141
Map<String, Object> parameters,
142142
boolean isHelper
143143
) {
144-
if (!language.equals("go")) {
145-
return;
146-
}
144+
if (!"go".equals(language)) return;
145+
146+
boolean isBodyRequired = ope.bodyParam != null && ope.bodyParam.required;
147+
boolean alreadyInlinedBody = ope.allParams.size() == 1 && ope.bodyParam != null && !ope.bodyParam.isArray;
148+
// I can't figure out the correct condition for this one so it's harcoded for now
149+
boolean isSFFV =
150+
"searchForFacetValues".equals(ope.operationId) && !ope.tags.isEmpty() && "composition".equals(ope.tags.get(0).getName());
151+
147152
int bodyPropsOptional = 0;
148-
boolean actuallyHasOptional = false;
149153
boolean isBodyTooBig = false;
150-
boolean isBodyRequired = (ope.bodyParam != null && ope.bodyParam.required);
151-
boolean alreadyInlinedBody = ope.allParams.size() == 1 && ope.bodyParam != null && !ope.bodyParam.isArray;
154+
boolean actuallyHasOptional = false;
152155

153-
if (AlgoliaGoGenerator.canFlattenBody(ope)) {
154-
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
155-
isBodyTooBig = ope.bodyParam.getVars().size() == 0;
156+
if (AlgoliaGoGenerator.canFlattenBody(ope) && ope.bodyParam != null) {
157+
List<CodegenProperty> vars = ope.bodyParam.getVars();
158+
bodyPropsOptional = (int) vars.stream().filter(p -> !p.required).count();
159+
isBodyTooBig = vars.isEmpty();
156160

157-
// edge case where the body is already flattened
158-
Map<String, Object> paramBody = paramBody = parameters;
161+
Map<String, Object> paramBody = parameters;
159162
if (!alreadyInlinedBody) {
160-
Object paramBodyObj = parameters.get(ope.bodyParam.paramName);
161-
if (paramBodyObj instanceof String) {
162-
// this is a verbatim paramater, we use it as is
163-
System.out.println(ope.operationId + " is a verbatim body " + paramBodyObj);
163+
Object paramObj = parameters.get(ope.bodyParam.paramName);
164+
if (paramObj instanceof String) {
164165
actuallyHasOptional = !isBodyRequired;
165-
} else {
166-
paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
166+
} else if (paramObj instanceof Map) {
167+
paramBody = (Map<String, Object>) paramObj;
167168
}
168169
}
169170

170-
for (CodegenProperty prop : ope.bodyParam.getVars()) {
171+
for (CodegenProperty prop : vars) {
171172
if (!prop.required && paramBody != null && paramBody.containsKey(prop.baseName)) {
172173
actuallyHasOptional = true;
173174
}
174175
}
175176
}
176177

177-
int totalOptional = ope.optionalParams.size() + bodyPropsOptional;
178-
179178
for (CodegenParameter param : ope.allParams) {
180179
if (!param.required && parameters.containsKey(param.baseName)) {
181180
actuallyHasOptional = true;
182181
break;
183182
}
184183
}
185184

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

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

templates/go/api.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ func (o *{{operationId}}Options) With{{#lambda.titlecase}}{{baseName}}{{/lambda.
398398
// Parameters:
399399
{{/hasParams}}
400400
{{#requiredParams}}
401-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}}
401+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}}
402402
{{/requiredParams}}
403403
{{#optionalParams}}
404-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}} (in optionalParams)
404+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}} (in optionalParams)
405405
{{/optionalParams}}
406406
// - opts - Optional parameters for the API call (e.g. WithContext, WithHeaderParam...)
407407
{{#isDeprecated}}
@@ -449,10 +449,10 @@ func (c *APIClient) {{nickname}}({{#requiredParams}}{{paramName}} {{#required}}{
449449
// Parameters:
450450
{{/hasParams}}
451451
{{#requiredParams}}
452-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}}
452+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}}
453453
{{/requiredParams}}
454454
{{#optionalParams}}
455-
// - {{paramName}} {{#description}} - {{{.}}}{{/description}} (in optionalParams)
455+
// - {{paramName}} {{#description}}- {{{.}}}{{/description}} (in optionalParams)
456456
{{/optionalParams}}
457457
// - opts - Optional parameters for the API call (e.g. WithContext, WithHeaderParam...)
458458
{{#isDeprecated}}

0 commit comments

Comments
 (0)