“请求主体"没有出现在Swagger UI上 [英] "Request body" doesn't appears on Swagger UI

查看:679
本文介绍了“请求主体"没有出现在Swagger UI上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Swagger文档中的 POST 请求进行测试,但是遇到以下麻烦:

I'm trying to do a test with a POST request on my Swagger Documentation, but I'm having the following trouble:

在Swagger编辑器上,我可以正常进行测试,但是在Swagger UI上,不会显示请求正文"字段,因此,我无法输入我的电子邮件和密码来测试请求.

On Swagger Editor I can test it normally, but on Swagger UI the Request body field doesn't appears, so I can't put my email and password to test the request.

在这里您可以看到请求正文"字段,其中包含可供编辑的数据示例:
Swagger编辑器示例

Here you can see the Request body field with data examples ready to be edited:
Swagger Editor Example

在这里您可以看到Swagger UI没有显示它:
Swagger UI示例

And here you can see that Swagger UI doesn't shows it:
Swagger UI Example

注意:我已经生成了一个 nodejs服务器.

Note: I've generated a nodejs-server.

我的.yaml代码:

openapi: '3.0.1'

info:
  version: 1.0.0
  title: Test
  description: Test openapi.

servers:
  - url: http://localhost:3005

paths:
  /login:
    post:
      summary: Login
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/login'
      responses:
        200:
          description: Test

components:
  schemas:
    login:
      type: object
      properties:
        email:
          type: string
          example: example@example.com.br
        password:
          type: string
          format: password
          example: example123
        expires:
          type: integer
          example: 86400
      required:
       - email
       - password

推荐答案

TL; DR:将Swagger UI更新到最新版本.

只是为了阐明情况(供以后的读者使用):OP将这个OpenAPI 3.0定义粘贴到Swagger Editor中,生成了一个Node.js服务器并运行它.请求正文显示问题与Swagger UI有关,而Swagger UI是此Node.js服务器(http://localhost:8080/docs)的一部分.

Just to clarify the scenario (for future readers): The OP pasted this OpenAPI 3.0 definition into Swagger Editor, generated a Node.js server and ran it. The request body display issue is with Swagger UI that is part of this Node.js server (http://localhost:8080/docs).

问题是此Node.js服务器使用的是Swagger UI的非常旧的版本.更具体地说,它使用 oas3-tools 软件包1.0.1版,其中包含Swagger UI v.3.3. 1个捆绑在一起. Swagger UI的最新版本(截至撰写本文时)为3.22.1,并且没有所描述的问题.

The issue is that this Node.js server uses a very old version of Swagger UI. More specifically, it uses the oas3-tools package v. 1.0.1, which has Swagger UI v. 3.3.1 bundled with it. The latest Swagger UI version (as of this writing) is 3.22.1, and it does not have the described issue.

解决方案是将Swagger UI更新到最新版本:

The solution is to update Swagger UI to the latest version:

  • 存在的问题 >存储库,将捆绑的Swagger UI更新到最新版本.考虑提交PR来解决此问题.

  • There's an existing issue in the oas3-tools repository to update the bundled Swagger UI to the latest version. Consider submitting a PR to fix this.

作为快速解决方案,您可以分叉oas3-tools,更新<oas3-tools>\middleware\swagger-ui文件夹中捆绑的Swagger UI文件,然后更新所生成的Node.js服务器的package.json以引用您的oas3-tools分叉.

As a quick fix, you can fork oas3-tools, update the bundled Swagger UI files in the <oas3-tools>\middleware\swagger-ui folder, and then update the package.json of the generated Node.js server to refer to your oas3-tools fork instead.

这篇关于“请求主体"没有出现在Swagger UI上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆