swagger-api / swagger-api/swagger-codegen

[Golang][codegen]Generate code that can never be reached by "if" condition

Open
#11,137 1 comment 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

Description

Generate harbor golang client sdk by harbor api swagger yaml
After the client sdk code generated, some code that can never be reached
Example in api_health.go :

	if localVarHttpResponse.StatusCode >= 300 {
		newErr := GenericSwaggerError{
			body:  localVarBody,
			error: localVarHttpResponse.Status,
		}

		if localVarHttpResponse.StatusCode == 200 {
			var v OverallHealthStatus
			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
		}

		if localVarHttpResponse.StatusCode == 500 {
			var v Errors
			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
	}

The condition localVarHttpResponse.StatusCode == 200 inside localVarHttpResponse.StatusCode >= 300 , it's never be reached.

Swagger-codegen version

2.4.21

Swagger declaration file content or url

https://github.com/goharbor/harbor/blob/master/api/v2.0/swagger.yaml
Raw: https://raw.githubusercontent.com/goharbor/harbor/master/api/v2.0/swagger.yaml

Command line used for generation
java -jar swagger-codegen-cli.jar generate -i https://raw.githubusercontent.com/goharbor/harbor/master/api/v2.0/swagger.yaml -l go -o ./harbor-api 
Steps to reproduce
  1. run java -jar swagger-codegen-cli.jar generate -i https://raw.githubusercontent.com/goharbor/harbor/master/api/v2.0/swagger.yaml -l go -o ./harbor-api
  2. cat ./harbor-api/api_health.go
Related issues/PRs

N/A

Suggest a fix/enhancement

N/A

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

Run the listed swagger-codegen CLI command with Harbor's api/v2.0/swagger.yaml and inspect the generated api_health.go. Trace the Go code-generation template or entry point that produces the status-condition branches, then verify that generated conditions cannot contain an unreachable status check while preserving the declared responses.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.