swagger-api / swagger-api/swagger-codegen

bug: incorrect logic and missing imports in generated Golang client

Open
#12,142 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Problem statement

Swagger UI https://editor.swagger.io/ generated some invalid Golang client code and the code has some logical errors.

Steps to reproduce

  1. Go to the Swagger editor https://editor.swagger.io/
  2. Copy paste our OpenAPI V3 spec json https://github.com/konveyor/move2kube-api/blob/a8382d16546ff3da9867357b0787bbb45bb4fcd4/assets/openapi.json
  3. Generate a client for Golang.
image 4. Unpack the archive/zip file and look at the code. Use it as a package in a new project and try to build.

Actual behaviour

$ go run main.go 
# foo.com/b/swagger
swagger/api_project_outputs.go:110:131: undefined: os
swagger/api_project_outputs.go:116:24: undefined: os
swagger/api_project_outputs.go:176:11: undefined: os

The code did not contain an import for os package in api_project_outputs.go and few other files.

		localVarReturnValue *os.File

Also the code contains logical errors like this snippet in api_workspaces.go which checks if the status is >= 300 and also if it is == 200 inside the nested if block.

	if localVarHttpResponse.StatusCode >= 300 {
		newErr := GenericSwaggerError{
			body:  localVarBody,
			error: localVarHttpResponse.Status,
		}
		if localVarHttpResponse.StatusCode == 200 {
			var v []Workspace
			err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarReturnValue, localVarHttpResponse, newErr
			}
			newErr.model = v
			return localVarReturnValue, localVarHttpResponse, newErr
		}
		return localVarReturnValue, localVarHttpResponse, newErr
	}

Swagger specification

Open API V3
openapi: 3.0.1

Environment

swagger version: Swagger UI
go version: go version go1.19.2 darwin/amd64
OS: MacOS Ventura 13.3

Related

https://github.com/go-swagger/go-swagger/issues/2939

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 report in Swagger UI using the linked OpenAPI V3 specification, then inspect the generated api_project_outputs.go and api_workspaces.go files. Confirm that the generated Go package builds without missing os imports and that the response-status logic handles successful Workspace responses correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.