swagger-api / swagger-api/swagger-codegen

[Go] Unable to specify Content-Type for form data file upload

Open
#9,880 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

For a Swagger spec of

      "post": {
        "summary": "upload",
        "description": "Upload a file.",
        "operationId": "uploadUsingPOST",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "file",
            "in": "formData",
            "description": "file",
            "required": true,
            "type": "file"
          },

the generated code will always set the Content-Type of the file to be application/octet-stream.

Some services expect the Content-Type of the upload file to be set correctly. Therefore, I would like to be able to set the Content-Type of a file upload.

This seems to be a result of the CreateFormFile() function from the stdin MIME library.
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/go/client.mustache#L210

Swagger-codegen version

v2.4.10

Command line used for generation
docker run -v $(pwd)/output:/output --rm swaggerapi/swagger-codegen-cli generate \
  -i https://my.internal.service.com/api-docs \
  -l go \
  -o /output
Suggest a fix/enhancement

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 with modules/swagger-codegen/src/main/resources/go/client.mustache around line 210 and inspect how CreateFormFile assigns the upload MIME type. Trace the generated Go client's multipart form-data path and determine how a caller could specify the file Content-Type. Done means generated clients can send the requested file MIME type instead of always using application/octet-stream, with coverage for the upload case.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Feature
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.