0

目前,我将 Swagger YAML 设置为application/json默认接受每个路由,在顶级 Swagger 定义中具有以下内容:

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

我现在有一个可以返回文件的路由,我不想指定所有可能返回的潜在文件。我在 Swagger GitHub 页面上看到,可能有某种通配符允许返回任何内容类型。我在我的路线中尝试了以下方法:

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

但是 Swagger UI 不允许我在 UI 中发送请求,因为它认为这没有Accept填充字段。

然后:

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

但是 Swagger UI 会抛出一个错误,指出unidentified alias "/*". 我也尝试过\*/*,以防万一这与需要逃脱有关,但这没有用。

最后:

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

这个允许我在 Swagger UI 中测试路由,但响应仍然未能通过验证,并声称预期的内容类型仍然是application/json.

是否有通配符有效,或者我是否正在尝试做一些 Swagger 没有设置的事情?

4

1 回答 1

0

*/*匹配所有媒体类型并且等价于application/octet-stream.

produces是一个数组,所以需要使用数组语法。另外,由于它是 YAML,'*/*'需要用引号括起来,因为*是一个特殊字符,用于表示别名节点

produces:
  - '*/*'
  # or
  - application/octet-stream
于 2017-08-17T10:37:43.263 回答