【问题标题】:How can I set up my swagger-node route to accept any content type?如何设置我的 swagger-node 路由以接受任何内容类型?
【发布时间】:2017-08-17 09:24:17
【问题描述】:

目前,我已将我的 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 没有设置的事情?

【问题讨论】:

    标签: swagger swagger-2.0 swagger-node


    【解决方案1】:

    */* 匹配所有媒体类型,等效于application/octet-stream

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

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

    【讨论】:

    • 嗨 Helen - 试过了,但出现以下错误:Error: Response validation failed: invalid content type (image/png). These are valid: application/json, */*, application/octet-stream
    • 是的。响应验证。
    • 您使用哪种服务器/框架?验证码长什么样子?
    • 验证是通过swagger,不存在自定义验证代码。后端很招摇。
    • “Swagger”是lots of projects 的统称。您的服务器实际使用哪一个? swagger-tools、swagger-node-express、springfox、...?
    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 2011-12-24
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    相关资源
    最近更新 更多