Skip to content

Commit a9c85af

Browse files
committed
also flatten when there is a single option
1 parent ee6d198 commit a9c85af

File tree

3 files changed

+96
-95
lines changed

3 files changed

+96
-95
lines changed

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

Lines changed: 79 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -152,106 +152,16 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
152152
String[] lines = ope.unescapedNotes.split("\n");
153153
ope.notes = (lines[0] + "\n" + Arrays.stream(lines).skip(1).map(line -> "// " + line).collect(Collectors.joining("\n"))).trim();
154154

155+
// enrich the params
155156
for (CodegenParameter param : ope.optionalParams) {
156157
param.nameInPascalCase = Helpers.capitalize(param.baseName);
157158
}
158159

159160
CodegenParameter bodyParam = ope.bodyParam;
160-
if (bodyParam == null) {
161-
continue;
162-
}
163-
bodyParam.nameInPascalCase = Helpers.capitalize(bodyParam.baseName);
164-
if (!bodyParam.isModel) {
165-
continue;
166-
}
167-
168-
// check for colision with other params
169-
boolean hasCollision = false;
170-
for (CodegenProperty prop : bodyParam.getVars()) {
171-
for (CodegenParameter param : ope.allParams) {
172-
if (param.paramName.equals(prop.baseName)) {
173-
hasCollision = true;
174-
break;
175-
}
176-
}
177-
}
178-
if (hasCollision) {
179-
System.out.println("Operation " + ope.operationId + " has a body param with the same name as another param, skipping flattening");
180-
continue;
181-
}
182-
183-
if (ope.operationId.equals("Browse")) {
184-
System.out.println(
185-
ope.allParams.size() +
186-
" params " +
187-
ope.requiredParams.size() +
188-
" required params " +
189-
ope.optionalParams.size() +
190-
" optional params"
191-
);
192-
}
193-
194-
bodyParam.vendorExtensions.put("x-flat-body", bodyParam.getVars().size() > 0);
195-
196-
if (bodyParam.getVars().size() > 0) {
197-
ope.allParams.removeIf(param -> param.isBodyParam);
198-
ope.requiredParams.removeIf(param -> param.isBodyParam);
199-
ope.optionalParams.removeIf(param -> param.isBodyParam);
200-
}
201-
202-
for (CodegenProperty prop : bodyParam.getVars()) {
203-
// there is no easy way to convert a prop to a param, we need to copy all the fields
204-
CodegenParameter param = new CodegenParameter();
205-
206-
prop.nameInLowerCase = toParamName(prop.baseName);
207-
param.nameInPascalCase = Helpers.capitalize(prop.baseName);
208-
param.paramName = toParamName(prop.baseName);
209-
param.baseName = prop.baseName;
210-
param.baseType = prop.baseType;
211-
param.dataType = prop.dataType;
212-
param.datatypeWithEnum = prop.datatypeWithEnum;
213-
param.description = prop.description;
214-
param.example = prop.example;
215-
param.isModel = prop.isModel;
216-
param.isArray = prop.isArray;
217-
param.isContainer = prop.isContainer;
218-
param.isMap = prop.isMap;
219-
param.isEnum = prop.isEnum;
220-
param.isEnumRef = prop.isEnumRef;
221-
param.isPrimitiveType = prop.isPrimitiveType;
222-
param.isString = prop.isString;
223-
param.isNumeric = prop.isNumeric;
224-
param.isBoolean = prop.isBoolean;
225-
param.isDate = prop.isDate;
226-
param.isDateTime = prop.isDateTime;
227-
param.isFreeFormObject = prop.isFreeFormObject;
228-
param.isNullable = prop.isNullable;
229-
param.jsonSchema = prop.jsonSchema;
230-
param.required = prop.required;
231-
param.vendorExtensions = prop.vendorExtensions;
232-
param.allowableValues = prop.allowableValues;
233-
234-
if (prop.required) {
235-
ope.requiredParams.add(param);
236-
ope.hasRequiredParams = true;
237-
} else {
238-
ope.optionalParams.add(param);
239-
ope.hasOptionalParams = true;
240-
}
241-
ope.allParams.add(param);
161+
if (bodyParam != null) {
162+
flattenBody(ope);
242163
}
243164

244-
System.out.println(
245-
ope.operationId +
246-
" has " +
247-
ope.requiredParams.size() +
248-
" required params and " +
249-
ope.optionalParams.size() +
250-
" optional params " +
251-
bodyParam.getVars().size() +
252-
" body params "
253-
);
254-
255165
// If the optional param struct only has 1 param, we can remove the wrapper
256166
if (ope.optionalParams.size() == 1) {
257167
CodegenParameter param = ope.optionalParams.get(0);
@@ -270,4 +180,80 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
270180
GenericPropagator.propagateGenericsToOperations(operations, models);
271181
return operations;
272182
}
183+
184+
private void flattenBody(CodegenOperation ope) {
185+
CodegenParameter bodyParam = ope.bodyParam;
186+
bodyParam.nameInPascalCase = Helpers.capitalize(bodyParam.baseName);
187+
if (!bodyParam.isModel) {
188+
return;
189+
}
190+
191+
// check for colision with other params
192+
for (CodegenProperty prop : bodyParam.getVars()) {
193+
for (CodegenParameter param : ope.allParams) {
194+
if (param.paramName.equals(prop.baseName)) {
195+
System.out.println(
196+
"Operation " +
197+
ope.operationId +
198+
" has body param " +
199+
bodyParam.paramName +
200+
" in colision with param " +
201+
param.paramName +
202+
", skipping flattening"
203+
);
204+
return;
205+
}
206+
}
207+
}
208+
209+
bodyParam.vendorExtensions.put("x-flat-body", bodyParam.getVars().size() > 0);
210+
211+
if (bodyParam.getVars().size() > 0) {
212+
ope.allParams.removeIf(param -> param.isBodyParam);
213+
ope.requiredParams.removeIf(param -> param.isBodyParam);
214+
ope.optionalParams.removeIf(param -> param.isBodyParam);
215+
}
216+
217+
for (CodegenProperty prop : bodyParam.getVars()) {
218+
// there is no easy way to convert a prop to a param, we need to copy all the fields
219+
CodegenParameter param = new CodegenParameter();
220+
221+
prop.nameInLowerCase = toParamName(prop.baseName);
222+
param.nameInPascalCase = Helpers.capitalize(prop.baseName);
223+
param.paramName = toParamName(prop.baseName);
224+
param.baseName = prop.baseName;
225+
param.baseType = prop.baseType;
226+
param.dataType = prop.dataType;
227+
param.datatypeWithEnum = prop.datatypeWithEnum;
228+
param.description = prop.description;
229+
param.example = prop.example;
230+
param.isModel = prop.isModel;
231+
param.isArray = prop.isArray;
232+
param.isContainer = prop.isContainer;
233+
param.isMap = prop.isMap;
234+
param.isEnum = prop.isEnum;
235+
param.isEnumRef = prop.isEnumRef;
236+
param.isPrimitiveType = prop.isPrimitiveType;
237+
param.isString = prop.isString;
238+
param.isNumeric = prop.isNumeric;
239+
param.isBoolean = prop.isBoolean;
240+
param.isDate = prop.isDate;
241+
param.isDateTime = prop.isDateTime;
242+
param.isFreeFormObject = prop.isFreeFormObject;
243+
param.isNullable = prop.isNullable;
244+
param.jsonSchema = prop.jsonSchema;
245+
param.required = prop.required;
246+
param.vendorExtensions = prop.vendorExtensions;
247+
param.allowableValues = prop.allowableValues;
248+
249+
if (prop.required) {
250+
ope.requiredParams.add(param);
251+
ope.hasRequiredParams = true;
252+
} else {
253+
ope.optionalParams.add(param);
254+
ope.hasOptionalParams = true;
255+
}
256+
ope.allParams.add(param);
257+
}
258+
}
273259
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ public static void setOptionalParameters(CodegenOperation ope, Map<String, Objec
154154
}
155155
}
156156

157+
System.out.println(
158+
ope.operationId +
159+
" has " +
160+
ope.allParams.size() +
161+
" params " +
162+
ope.requiredParams.size() +
163+
" required params " +
164+
ope.optionalParams.size() +
165+
" optional params " +
166+
bodyPropsOptional +
167+
" bodyPropsOptional " +
168+
(ope.optionalParams.size() + bodyPropsOptional > 1) +
169+
" hasOptionalWrapper "
170+
);
171+
157172
// hasOptionalWrapper if there is more that one optional param, after the body has been
158173
// flattened, only relevant for go
159174
test.put("hasOptionalWrapper", ope.optionalParams.size() + bodyPropsOptional > 1);

templates/go/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func (o *{{operationId}}Options) With{{#lambda.titlecase}}{{baseName}}{{/lambda.
408408
//
409409
// Deprecated: {{operationId}} is deprecated
410410
{{/isDeprecated}}
411-
func (c *APIClient) {{nickname}}({{#requiredParams}}{{paramName}} {{#required}}{{#isModel}}*{{/isModel}}{{/required}}{{^required}}*{{/required}}{{{dataType}}}, {{/requiredParams}}{{#hasOptionalParams}}optionalParams *{{operationId}}Options, {{/hasOptionalParams}}opts ...RequestOption) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
411+
func (c *APIClient) {{nickname}}({{#requiredParams}}{{paramName}} {{#required}}{{#isModel}}*{{/isModel}}{{/required}}{{^required}}{{^isMap}}*{{/isMap}}{{/required}}{{{dataType}}}, {{/requiredParams}}{{#hasOptionalParams}}optionalParams *{{operationId}}Options, {{/hasOptionalParams}}opts ...RequestOption) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
412412
{{#returnType}}
413413
var returnValue {{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}
414414
{{/returnType}}
@@ -459,7 +459,7 @@ func (c *APIClient) {{nickname}}({{#requiredParams}}{{paramName}} {{#required}}{
459459
//
460460
// Deprecated: {{operationId}} is deprecated
461461
{{/isDeprecated}}
462-
func (c *APIClient) {{nickname}}WithHTTPInfo({{#requiredParams}}{{paramName}} {{#required}}{{#isModel}}*{{/isModel}}{{/required}}{{^required}}*{{/required}}{{{dataType}}}, {{/requiredParams}}{{#hasOptionalParams}}optionalParams *{{operationId}}Options, {{/hasOptionalParams}}opts ...RequestOption) (*http.Response, []byte, error) {
462+
func (c *APIClient) {{nickname}}WithHTTPInfo({{#requiredParams}}{{paramName}} {{#required}}{{#isModel}}*{{/isModel}}{{/required}}{{^required}}{{^isMap}}*{{/isMap}}{{/required}}{{{dataType}}}, {{/requiredParams}}{{#hasOptionalParams}}optionalParams *{{operationId}}Options, {{/hasOptionalParams}}opts ...RequestOption) (*http.Response, []byte, error) {
463463
{{#vendorExtensions}}
464464
requestPath := "{{{path}}}"{{#pathParams}}
465465
requestPath = strings.ReplaceAll(requestPath, {{=<% %>=}}"{<%baseName%>}"<%={{ }}=%>, {{#x-is-custom-request}}utils.ParameterToString({{paramName}}){{/x-is-custom-request}}{{^x-is-custom-request}}url.PathEscape(utils.ParameterToString({{paramName}})){{/x-is-custom-request}}){{/pathParams}}

0 commit comments

Comments
 (0)