Skip to content

[BUG][GO-SERVER] The generated api has unused imports and does not compile in certain cases #8847

@jagobagascon

Description

@jagobagascon
Description

The code generated by the go-server generator fails to compile if the API does not contain any endpoints with a path parameter or a request body. This is happening because the imports declared in the generated Go file are fixed:

import (
	"encoding/json" // <-------------- Only used when the API contains a request body
	"net/http"
	"strings"

	"github.com/gorilla/mux" // <----- Only used when the API contains Path parameters "/path/{param1}"
)
openapi-generator version

5.0.1

OpenAPI declaration file content or url

This simple spec generates code that fails to compile:

openapi: 3.0.0
info:
  description: This is a sample server.
  license:
    name: Apache-2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
  title: OpenAPI
  version: 1.0.0
externalDocs:
  description: Find out more about this
  url: http://test.eu
servers:
- url: http://test.eu/
paths:
  /simpleGet:
    get:
      operationId: simpleGet
      responses:
        "200":
          description: Ok
Generation Details

openapi-generator-cli generate -i spec.yml -g go-server -o out

Steps to reproduce
  • Generate a simple api using go-server generator (command above).
  • Try to compile the generated code using go build out/go/*.go.
  • You will get the following error:
# command-line-arguments
out/go/api_default.go:13:2: imported and not used: "encoding/json"
out/go/api_default.go:17:2: imported and not used: "github.com/gorilla/mux"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions