Skip to content

Commit 3226ff7

Browse files
committed
cts is working
1 parent f26adb4 commit 3226ff7

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,16 @@ public static void setOptionalParameters(
142142
) {
143143
int bodyPropsOptional = 0;
144144
boolean actuallyHasOptional = false;
145+
boolean isBodyTooBig = false;
146+
boolean alreadyInlinedBody = ope.allParams.size() == 1 && ope.bodyParam != null && ope.bodyParam.isModel;
145147

146148
if (AlgoliaGoGenerator.canFlattenBody(ope)) {
147149
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
150+
isBodyTooBig = ope.bodyParam.getVars().size() == 0;
148151

149152
// edge case where the body is already flattened
150153
Map<String, Object> paramBody = paramBody = parameters;
151-
if (ope.allParams.size() > 1) {
154+
if (!alreadyInlinedBody) {
152155
paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
153156
}
154157

@@ -168,29 +171,21 @@ public static void setOptionalParameters(
168171
}
169172
}
170173

174+
boolean isBodyRequired = (ope.bodyParam != null && ope.bodyParam.required);
175+
171176
// I can't figure out the correct condition for this one so it's harcoded for now
172177
boolean isSFFV = ope.operationId.equals("searchForFacetValues") && "composition".equals(ope.tags.get(0).getName());
173178

174179
// hasOptionalWrapper if there is more that one optional param, after the body has been
175180
// flattened, only relevant for go
176181
test.put("hasOptionalWrapper", totalOptional > 1 && actuallyHasOptional && !isSFFV);
177-
test.put("hasInlineOptional", (totalOptional == 1 || isSFFV) && actuallyHasOptional);
178-
test.put("hasNilOptional", totalOptional > 0 && !actuallyHasOptional && !isHelper);
179-
180-
System.out.println(
181-
ope.operationId +
182-
" hasOptionalWrapper: " +
183-
test.get("hasOptionalWrapper") +
184-
" hasNilOptional: " +
185-
test.get("hasNilOptional") +
186-
" hasInlineOptional: " +
187-
test.get("hasInlineOptional") +
188-
" totalOptional: " +
189-
totalOptional +
190-
" actuallyHasOptional: " +
191-
actuallyHasOptional +
192-
" bodyPropsOptional: " +
193-
bodyPropsOptional
194-
);
182+
test.put("hasInlineOptional", ((totalOptional == 1 || isSFFV) && actuallyHasOptional) || isBodyTooBig);
183+
if (isBodyTooBig) {
184+
boolean isBodySet = alreadyInlinedBody ? parameters.size() > 0 : parameters.containsKey(ope.bodyParam.paramName);
185+
System.out.println("isBodySet: " + isBodySet);
186+
test.put("hasNilOptional", isBodyRequired ? totalOptional > 0 && !actuallyHasOptional : !isBodySet);
187+
} else {
188+
test.put("hasNilOptional", totalOptional > 0 && !actuallyHasOptional && !isHelper);
189+
}
195190
}
196191
}

templates/go/api.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,16 @@ func (c *APIClient) {{nickname}}WithHTTPInfo({{#requiredParams}}{{paramName}} {{
567567
postBody := {{paramName}}
568568
{{/required}}
569569
{{^required}}
570-
{{#hasOptionalParams}}
571570
var postBody any = "{}"
571+
{{#hasOptionalParams}}
572572
if optionalParams != nil {
573573
postBody = optionalParams.{{nameInPascalCase}}
574574
}
575575
{{/hasOptionalParams}}
576576
{{^hasOptionalParams}}
577-
postBody := {{paramName}}
577+
if !utils.IsNilOrEmpty({{paramName}}) {
578+
postBody = {{paramName}}
579+
}
578580
{{/hasOptionalParams}}
579581
{{/required}}
580582
{{/vendorExtensions.x-flat-body}}

0 commit comments

Comments
 (0)