swagger-api / swagger-api/swagger-codegen

[Go] [Bug] APIResponse should not embed a pointer to *http.Response

Open
#3,691 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go Enhancement: General
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

APIResponse currently embeds a *http.Response, which leads to unexpected behavior. If including a http.Response, then it should be a named field. As it currently stands, one can receive a *APIResponse object which is not nil, but in which the http.Response pointer is nil. This leads to behavior where myAPIResponse != nil evaluates to true, but myAPIResponse.Status results in a panic.

Swagger-codegen version

master

Suggest a Fix

Instead of embedding the *http.Response, it should be a named element. That is, instead of:

// WRONG
type APIResponse struct {
    *http.Response
    Message string `json:"message,omitempty"`
}

We should do:

// BETTER
type APIResponse struct {
    Response *http.Response
    Message string `json:"message,omitempty"`
}

That is to say, the generated code should always return an APIResponse, but sometimes (e.g. in the case of a network error), there's no http.Response.

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

Start by locating the Go generation template that defines APIResponse and the generated response-handling path. Confirm how network errors are represented, then update the response shape so the HTTP response is named and verify that callers receive a non-nil APIResponse with no embedded Status access when no HTTP response exists.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.