OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.9k stars 6.59k forks source link

[BUG][GO] go-server generates api-files with unused imports #11097

Open feech opened 2 years ago

feech commented 2 years ago

Bug Report Checklist

Description

if api does not include path-parameters files "api_***.go" includes unused imports: "encoding/json" and "github.com/go-chi/chi/v5" or "github.com/gorilla/mux" (depends on selected routed)

if you remove these unused imports generated project can be compiled

openapi-generator version

5.3.0

OpenAPI declaration file content or url

https://app.swaggerhub.com/apis/acceleratedlife/alife/1.0.0

Generation Details

java -jar ./openapi-generator-cli-5.3.0.jar generate -i acceleratedLife-1.0.0-resolved.json -g go-server -o path\

Steps to reproduce

once code generated need to populate package-name in main.go and run 'go build'

compiler will show the errors:

go\api_users.go:14:2: imported and not used: "encoding/json"
go\api_users.go:18:2: imported and not used: "github.com/go-chi/chi/v5" as chi
Related issues/PRs
Suggest a fix

change a template: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache

tigerinus commented 2 years ago

@wing328 - by any chance you can fix this issue? much appreciated!

tigerinus commented 2 years ago

@feech - I just saw your fix - is it possible you push a PR for this? thanks!

tigerinus commented 2 years ago

For those facing the same issue, I am using following post processing script as a workaround:

#!/usr/bin/bash

set -e

if grep -q "api_default.go" <<< "$1"; then
    API_DEFAULT_GO=$1

    cat < "${API_DEFAULT_GO}" | grep -v "github.com/go-chi/chi/v5" > "${API_DEFAULT_GO}.tmp"

    mv "${API_DEFAULT_GO}.tmp" "${API_DEFAULT_GO}"
fi

And here is how I run the CLI via docker:

docker run --rm --user $UID:$GROUPS -v $PWD:/local -e GO_POST_PROCESS_FILE="bash /local/openapi-generator-postprocess.sh" openapitools/openapi-generator-cli generate -i /local/openapi.yaml -g go-server -o /local/target -p=onlyInterfaces=true,outputAsLibrary=true,router=chi,featureCORS=true,sourceFolder=codegen --strict-spec true --enable-post-process-file
0xERR0R commented 1 year ago

bug is still present in 6.2.1

petrilaakso commented 1 year ago

bug is still present in 6.2.1

I noticed that the imports gets called when I actually start to add content such as parameters to the openapi.yaml. For me it was more like little annoyance at the start. Random snippet

    delete:
      operationId: deleteUser
      parameters:
        - name: id
          in: path
// DeleteUser - 
func (c *DefaultApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
    idParam, err := parseInt64Parameter(chi.URLParam(r, "id"), true)