目前,我将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不允许我在用户界面中发送请求,因为它认为没有填充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没有设置的东西?
答案 0 :(得分:0)
*/*
匹配所有媒体类型,相当于application/octet-stream
。
produces
是一个数组,因此您需要使用数组语法。此外,由于它是YAML,'*/*'
需要用引号括起来,因为*
是用来表示alias nodes的特殊字符。
produces:
- '*/*'
# or
- application/octet-stream