swagger-api / swagger-api/swagger-codegen

[Go] redundant code block in client GET method template

Open
#11,808 0 comments 3 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

When read the generated code, I find some repeated weird(redundant) code block in generated client code

  • GET method
  • status 2XX
Swagger-codegen version

3.0.34

Swagger declaration file content or url

copy from petstore
https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml#L532

/user/{username}:
    get:
      tags:
      - "user"
      summary: "Get user by user name"
      description: ""
      operationId: "getUserByName"
      produces:
      - "application/xml"
      - "application/json"
      parameters:
      - name: "username"
        in: "path"
        description: "The name that needs to be fetched. Use user1 for testing. "
        required: true
        type: "string"
      responses:
        "200":
          description: "successful operation"
          schema:
            $ref: "#/definitions/User"
        "400":
          description: "Invalid username supplied"
        "404":
          description: "User not found"

the petstore client golang sample code already has this issue
https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/go/go-petstore/api_user.go#L381

	if localVarHttpResponse.StatusCode >= 300 {
		newErr := GenericSwaggerError{
			body: localVarBody,
			error: localVarHttpResponse.Status,
		}
		// inside this block, the statuscode should be >=300
                // so the following block is unused.
		if localVarHttpResponse.StatusCode == 200 {
			var v User
			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
	}

It happens when GET method in client with 2xx code

Suggest a fix/enhancement

if no edge case need this code block, we could remove the redundant part from mustache template
or we only have to check 400 and 404(skip 200) in the above example.

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

Inspect the Mustache template that produces the GET method and compare it with samples/client/petstore/go/go-petstore/api_user.go around the reported block. Reproduce generation from the linked petstore OpenAPI declaration, then verify that the generated 2xx GET client no longer contains the unreachable redundant block.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.