OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO] go-server generator doesn't generate endpoint for file download, it's always JSON

Open
#15,206 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I want to create an endpoint which allows downloading files based on some parameters. I followed the official instructions
on how to describe this kind of response: https://swagger.io/docs/specification/describing-responses/
But no matter what I try the generated endpoint returns JSON.

openapi-generator version

openapitools/openapi-generator-cli:latest

OpenAPI declaration file content or url
...
/v1/address/postcode-ranges:download:
  post:
    requestBody:
      content:
        application/json:
          schema:
            $ref: './schema.yaml#/V1DownloadPostcodeRangesFileRequest'
    responses:
      "200":
        description: Excel file
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
Generation Details
func (c *AddressApiController) V1AddressPostcodeRangesdownloadPost(w http.ResponseWriter, r *http.Request) {
	v1DownloadPostcodeRangesFileRequestParam := V1DownloadPostcodeRangesFileRequest{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&v1DownloadPostcodeRangesFileRequestParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertV1DownloadPostcodeRangesFileRequestRequired(v1DownloadPostcodeRangesFileRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.V1AddressPostcodeRangesdownloadPost(r.Context(), v1DownloadPostcodeRangesFileRequestParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	EncodeJSONResponse(result.Body, &result.Code, w)

}
Steps to reproduce
	docker run $(DOCKER_USER) --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate \
		-i /local/docs.yaml \
		-g go-server \
		-o /local/_build/go-server \
		--additional-properties=onlyInterfaces=true && \
		cd ./_build/go-server/go && \
		go mod tidy && \
		go fmt ./... && \
		goimports -w .
Related issues/PRs
Suggest a fix

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 the go-server generator and the generated AddressApiController method shown in the report, then trace how an application/octet-stream response is handled. Reproduce the generation with the provided OpenAPI declaration and command; done means the generated download endpoint does not encode the binary response as JSON.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, tooling
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.