OpenAPITools / OpenAPITools/openapi-generator

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

Open
#8,847 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI spec and the go-server generator, then inspect the generated out/go/api_default.go imports. Verify the generated server builds for APIs without path parameters or request bodies, and also preserves required imports when those features are present.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
backend, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.