swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.02k stars 6.03k forks source link

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

Open ymping opened 3 years ago

ymping commented 3 years ago
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

patrickjmcd commented 3 years ago

This is happening on 3.0.29 as well.