Skip to content

Commit

Permalink
fix(go): only import reflect when using it (OpenAPITools#19967)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
perhallgren and perhallgren authored Oct 30, 2024
1 parent cbc64e8 commit 03c29e7
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,6 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
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)) {
Expand All @@ -577,6 +571,14 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}
}

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);
Expand Down

0 comments on commit 03c29e7

Please sign in to comment.