Skip to content

CSVProducer returns error 'data type must be byte array' #263

@ahamtat

Description

@ahamtat

Hello,

I want to use CSV producer in the response to get data in CSV format. My swagger specification is the following

swagger: '2.0'
info:
  version: 1.0.0
  description: Provides an API for response in CSV format
  title: Foo example
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html

host:
  localhost:8080

basePath: "/"

paths:
  /:
    get:
      summary: Produces an response with data in CSV format
      description: Produces an response with data in CSV format
      tags:
        - Query
      produces:
        - "text/csv"
      consumes:
        - "application/json"
      operationId: getCSVResponse
      responses:
        200:
          description: "OK"
          schema:
            type: string
            $ref: "#/definitions/GetCSVFileResponse"
          headers:
            Content-Disposition:
              type: string
              description: "Attachment; filename=example.csv"

definitions:
  GetCSVFileResponse:
    description: CSV file response
    type: string
    format: binary
    x-go-mimetype: text/csv
    x-go-fileName: example.csv
    x-nullable: false

Then I generate code with the swagger v0.30.4 utility:

swagger \
	generate server \
	--target=internal/app/rest \
	-f api/app.swagger.yml

The generated GetCSVResponseOK struct has payload of models.GetCSVFileResponse type which is io.ReadCloser:

/*
GetCSVResponseOK OK

swagger:response getCSVResponseOK
*/
type GetCSVResponseOK struct {
	/*Attachment; filename=example.csv

	 */
	ContentDisposition string `json:"Content-Disposition"`

	/*
	  In: Body
	*/
	Payload models.GetCSVFileResponse `json:"body,omitempty"`
}

When I try to run my code the CSV producer fails making type cast from this type to a byte slice and returns an error 'data type must be byte array' at the line https://github.com/go-openapi/runtime/blob/master/csv.go#L60
Also I've tried type 'string' in swagger specification only:

  /:
    get:
      summary: Produces an response with data in CSV format
      description: Produces an response with data in CSV format
      tags:
        - Query
      produces:
        - "text/csv"
      operationId: getCSVResponse
      responses:
        200:
          description: "OK"
          schema:
            type: string
          headers:
            Content-Disposition:
              type: string
              description: A header specifying the file name

This case the generated GetCSVResponseOK struct has payload of string type but type cast into the CSV producer fails the same way.

Please tell how to write a swagger specification to get a byte slice type as a response or to convert other types into a byte slice before calling CSV producer?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions