go-openapi CSVProducer returns a "data type must be byte array" error

121 Views Asked by At

I've made an swagger 2.0 specification endpoint to produce a response in CSV format:

  /query/csv:
    post:
      summary: Execute query and return response in CSV format
      description: Executes query and returns response in CSV format
      tags:
        - Query
      produces:
        - "text/csv"
      
      ...

      responses:
        200:
          description: "OK"
          schema:
            type: string
          headers:
            Content-Disposition:
              type: string
              description: A header specifying the file name
        400:
          description: "Bad request"
        500:
          description: "Internal error during request processing"

I use quay.io/goswagger/swagger:v0.25.0 image while generating a code:

docker run --rm -v $(PWD):/axxondatago -w /axxondatago -t $(SWAGGER_IMAGE) \
        generate server \
        --target=internal/app/query/rest \
        --exclude-main \
        -f api/openapi-spec/query.swagger.yml

After code generation phase I get the string payload type for the query response:

...
return queryRest.NewExecuteQueryCSVOK().WithPayload(buffer) // var buffer string

But the CSVProducer is expecting byte slice for the type cast https://github.com/go-openapi/runtime/blob/1ade6d4fa0ddfcfdcad57761eed3588b40cf41ae/csv.go#L58 and returns the error:

// CSVProducer creates a new CSV producer
func CSVProducer() Producer {
    return ProducerFunc(func(writer io.Writer, data interface{}) error {
        if writer == nil {
            return errors.New("CSVProducer requires a writer")
        }

        dataBytes, ok := data.([]byte)
        if !ok {
            return errors.New("data type must be byte array")
        }

I tried different strings formats like 'byte', 'binary' and type 'file' in a schema section of endpoint but did not get a byte slice for the response.

Which types and formats should I use in schema to make CSVProducer work properly?

0

There are 0 best solutions below