Skip to content

Commit 40793bb

Browse files
committed
cts is working
1 parent 0d540fa commit 40793bb

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
@@ -138,13 +138,16 @@ public static void setOptionalParameters(
138138
) {
139139
int bodyPropsOptional = 0;
140140
boolean actuallyHasOptional = false;
141+
boolean isBodyTooBig = false;
142+
boolean alreadyInlinedBody = ope.allParams.size() == 1 && ope.bodyParam != null && ope.bodyParam.isModel;
141143

142144
if (AlgoliaGoGenerator.canFlattenBody(ope)) {
143145
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
146+
isBodyTooBig = ope.bodyParam.getVars().size() == 0;
144147

145148
// edge case where the body is already flattened
146149
Map<String, Object> paramBody = paramBody = parameters;
147-
if (ope.allParams.size() > 1) {
150+
if (!alreadyInlinedBody) {
148151
paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
149152
}
150153

@@ -164,29 +167,21 @@ public static void setOptionalParameters(
164167
}
165168
}
166169

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

170175
// hasOptionalWrapper if there is more that one optional param, after the body has been
171176
// flattened, only relevant for go
172177
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-
);
178+
test.put("hasInlineOptional", ((totalOptional == 1 || isSFFV) && actuallyHasOptional) || isBodyTooBig);
179+
if (isBodyTooBig) {
180+
boolean isBodySet = alreadyInlinedBody ? parameters.size() > 0 : parameters.containsKey(ope.bodyParam.paramName);
181+
System.out.println("isBodySet: " + isBodySet);
182+
test.put("hasNilOptional", isBodyRequired ? totalOptional > 0 && !actuallyHasOptional : !isBodySet);
183+
} else {
184+
test.put("hasNilOptional", totalOptional > 0 && !actuallyHasOptional && !isHelper);
185+
}
191186
}
192187
}

templates/go/api.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,16 @@ func (c *APIClient) {{nickname}}WithHTTPInfo({{#requiredParams}}{{paramName}} {{
519519
postBody := {{paramName}}
520520
{{/required}}
521521
{{^required}}
522-
{{#hasOptionalParams}}
523522
var postBody any = "{}"
523+
{{#hasOptionalParams}}
524524
if optionalParams != nil {
525525
postBody = optionalParams.{{nameInPascalCase}}
526526
}
527527
{{/hasOptionalParams}}
528528
{{^hasOptionalParams}}
529-
postBody := {{paramName}}
529+
if !utils.IsNilOrEmpty({{paramName}}) {
530+
postBody = {{paramName}}
531+
}
530532
{{/hasOptionalParams}}
531533
{{/required}}
532534
{{/vendorExtensions.x-flat-body}}

0 commit comments

Comments
 (0)