Skip to content

Commit 03c29e7

Browse files
authored
fix(go): only import reflect when using it (OpenAPITools#19967)
The api.mustache template only uses the reflect package if there is a query parameter which `isCollectionFormatMulti`. The import for the reflect package is however added if _any_ parameter is `isCollectionFormatMulti`. If the parameter which `isCollectionFormatMulti` e.g. is in the body instead of the query, this leads to the import being part of the generated code but not used and the code does not compile. This updates reworks the import handling for the `reflect` package so that it is only added if there is a query parameter which `isCollectionFormatMulti`. Co-authored-by: Per Hallgren <[email protected]>
1 parent cbc64e8 commit 03c29e7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,6 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
558558
addedTimeImport = true;
559559
}
560560

561-
// import "reflect" package if the parameter is collectionFormat=multi
562-
if (!addedReflectImport && param.isCollectionFormatMulti) {
563-
imports.add(createMapping("import", "reflect"));
564-
addedReflectImport = true;
565-
}
566-
567561
// set x-exportParamName
568562
char nameFirstChar = param.paramName.charAt(0);
569563
if (Character.isUpperCase(nameFirstChar)) {
@@ -577,6 +571,14 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
577571
}
578572
}
579573

574+
for (CodegenParameter param : operation.queryParams) {
575+
// import "reflect" package if the parameter is collectionFormat=multi
576+
if (!addedReflectImport && param.isCollectionFormatMulti) {
577+
imports.add(createMapping("import", "reflect"));
578+
addedReflectImport = true;
579+
}
580+
}
581+
580582
setExportParameterName(operation.queryParams);
581583
setExportParameterName(operation.formParams);
582584
setExportParameterName(operation.headerParams);

0 commit comments

Comments
 (0)