How can I set up my swagger-node route to accept any content type?

1.2k Views Asked by At

Currently, I have my Swagger YAML set up to accept application/json by default for each route with the following on the top-level Swagger definition:

swagger: "2.0"
info:
  version: "0.0.1"
  title: my App
# during dev, should point to your local machine
host: localhost:5054
# basePath prefixes all resource paths
basePath: /
schemes:
# tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
produces:
  - application/json

I now have a route that can return files, and I don't want to have to specify all the potential files that may be returned. I have seen on the Swagger GitHub page that there may be some sort of wildcard that allows any content type to be returned. I have tried the following in my route:

get:
   description: Download a single document
   operationId: getDocument
   produces: []

but Swagger UI will not allow me to send the request in the UI, as it sees this as having no Accept field populated.

Then:

get:
   description: Download a single document
   operationId: getDocument
   produces: */*

But Swagger UI throws an error stating unidentified alias "/*". I also tried \*/* just in case it was something to do with needing to be escaped but that didn't work.

Finally:

get:
   description: Download a single document
   operationId: getDocument
   produces: "*/*"

This one allowed me to test the route in Swagger UI but the response still failed validation and claimed the expected content type was still to be application/json.

Is there a wildcard that works or am I trying to do something Swagger isn't set up for?

1

There are 1 best solutions below

6
On

*/* matches all media types and is equivalent to application/octet-stream.

produces is an array, so you need to use the array syntax. Also, since it's YAML, '*/*' needs to be enclosed in quotes, because * is a special character used to indicate alias nodes.

produces:
  - '*/*'
  # or
  - application/octet-stream