swagger-api / swagger-api/swagger-codegen

[go] Should not use pointer when HTTP post body type is string

Open
#9,174 0 comments 0 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

Swagger generated code is trying to get address of POST body. However, if body's type is string, such behavior will lead to sending an address to server.

func (c *MyClient) CreateObject(ctx context.Context, someOtherArgs, body string) (interface{},  *http.Response, error) {
    var (
        localVarHttpMethod = strings.ToUpper("Post")
        localVarPostBody interface{}
        localVarFileName string
        localVarFileBytes []byte
         successPayload  interface{}
    )

    // create path and map variables

    // to determine the Content-Type header

    // set Content-Type header

    // to determine the Accept header

    // set Accept header

    // body params
    localVarPostBody = &body

    // rest of the function
}
Swagger-codegen version

2.4.1
It's not a regression, and such problem appears in all current releases

Swagger declaration file content or url

Here is a simplified snippet

{
    "operationId": "createObject",
    "parameters": [
        {
            "description": "input",
            "in": "body",
            "name": "body",
            "required": true,
            "schema": {
                "type": "string"
            }
       }
    ]
}
Command line used for generation

generate -i my_swagger.json -l go -o my_directory

Steps to reproduce

I think this is a general problem - any HTTP POST body

Related issues/PRs
Suggest a fix/enhancement

Check argument type and decide whether to use pointer or not

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 running the supplied generate command with the simplified Swagger JSON and inspect the generated Go client method, especially the POST body assignment. Trace the Go generator's body-parameter template or entry point to see how string types are handled. Done means generated HTTP POST code sends a string body correctly without taking its address, while other body types retain expected behavior.

Written by the indexing model from the issue text.

Assessment

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