Swagger UI 不显示 image/png 中的内容 POST

Swagger UI does not display image/png content in POST

我有一个场景,其中 POST 方法 returns 和 "image/png" 内容。 API 在 Postman 中完美运行,我能够看到图像。

我使用 Swagger 记录我的 APIs。出于某种原因,"Try out" 功能会使 returns 图像的任何 POST 方法崩溃。它在加载时冻结,我在浏览器控制台中看到以下内容:

cannot parse JSON/YAML content 
swagger-ui-min-js:14 Uncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
at C.n.showStatus (swagger-ui-min-js:14)
at showCompleteStatus (swagger-ui-min-js:14)
at response (swagger-ui-min-js:9)
at h (swagger-ui-min-js:7)
at t.on.response (swagger-ui-min-js:7)
at swagger-ui-min-js:7
at h.callback (swagger-ui-min-js:13)
at h.<anonymous> (swagger-ui-min-js:13)
at h.r.emit (swagger-ui-min-js:13)
at XMLHttpRequest.n.onreadystatechange (swagger-ui-min-js:13)

关于如何解决这个问题的任何想法? API 有效(至少在 Postman 中是这样),只是 Swagger UI 似乎有问题。 我通过 nuget 包 Swashbuckle.Core 和 Swashbuckle 版本 5.6.0

安装了 Swagger

swagger-ui 团队确认这是一个仅在 POST:
上发生的错误 https://github.com/swagger-api/swagger-ui/issues/3435
作为解决方法,我建议您使用 GET(如果可能)


更新(Jul/22)

swagger-ui 团队已修复 3.0.20 版的错误
我将该版本合并到我的 fork 中,您可以在此处获取最新版本:
https://www.nuget.org/packages/Swagger-Net/8.3.0.2001

如评论中所述,M_M 最初使用 Swagger UI 2.x,规范如下所示:

paths:
  /api/PngImage:
    post:
      produces:
        - application/json
        - text/json
        - text/html
      responses:
        200:
          description: OK
          schema:
            type: object

规范有两个问题:

  • 端点 returns PNG 图像,但据说会生成 JSON 和 HTML。
  • 文件回复,包括图片,应该有 type: file,而不是 type: object

在 Swagger UI 2.x 中,"cannot parse JSON/YAML content" 错误是由错误的 produces - UI 期望 JSON 响应但得到相反,二进制响应。将 produces 更改为 image/png 应该可以解决 UI 2.x 的问题。

然而,

Swagger UI 3.0.19 有一个不同的问题 - 当响应是图像时,UI 将请求重复到同一端点但使用 GET 以显示图像而不是渲染现有的响应。因此,如果端点不接受 GET,或者 GET 返回的图像与 POST 不同,则不会显示原始图像响应。 HelderSepu reported the double request issue 给 Swagger UI 开发人员,它已在 Swagger UI 3.0.20.

中修复