fix(go): only import reflect when using it (#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 <perhallgren@users.noreply.github.com>
This commit is contained in:
Per Hallgren 2024-10-30 07:20:25 +01:00 committed by GitHub
parent cbc64e86d3
commit 03c29e72c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -558,12 +558,6 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
addedTimeImport = true;
}
// import "reflect" package if the parameter is collectionFormat=multi
if (!addedReflectImport && param.isCollectionFormatMulti) {
imports.add(createMapping("import", "reflect"));
addedReflectImport = true;
}
// set x-exportParamName
char nameFirstChar = param.paramName.charAt(0);
if (Character.isUpperCase(nameFirstChar)) {
@ -577,6 +571,14 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
}
}
for (CodegenParameter param : operation.queryParams) {
// import "reflect" package if the parameter is collectionFormat=multi
if (!addedReflectImport && param.isCollectionFormatMulti) {
imports.add(createMapping("import", "reflect"));
addedReflectImport = true;
}
}
setExportParameterName(operation.queryParams);
setExportParameterName(operation.formParams);
setExportParameterName(operation.headerParams);