I'm using OpenAPI-Generator for Go server: https://github.com/OpenAPITools/openapi-generator, and I'm trying to generate an endpoint which allows file download (excel file to be precise). I checked OpenAPI documentation and based on it I made this endpoint definition:
/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
But instead of creating an file download endpoint, it creates endpoint which returns JSON:
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)
}
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 .