简体   繁体   English

在Swagger UI的参数列表中显示json对象属性

[英]Display json object properties in parameters list in Swagger UI

I' writing the docs for web API with swagger, OpenAPI version 3. I use swagger php package to generate documented json from annotations. 我正在使用swagger(OpenAPI版本3)编写Web API文档。我使用swagger php包从注释生成文档化的json。 I have service, where I send post request to add new user, and requested body is json(so parameters are sent as json object). 我有服务,我在其中发送发布请求以添加新用户,并且请求的主体为json(因此参数作为json对象发送)。 it has 2 parameters - email and password. 它有2个参数-电子邮件和密码。 request body looks like 请求正文看起来像

{
   "email": "test@test.com",
   "password": "test"
}

here's YAML of swagger 这是大张旗鼓的YAML

paths:
  /users:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SignUp'
      responses:
        '200':
          description: successful operation

here's reference schema which contains request parameters ( /components/schemas/SignUp ) 这是包含请求参数的参考架构( /components/schemas/SignUp

SignUp:
  title: SignUp
  description: Adds new user
  type:object
  required:
    - email
    - password
  properties:
    email:
      description: User's email
      type: string
      maximum: 255
      pattern: email
    password:
      description: User's password
      type: string
      maximum: 255

here's how it looks in swagger UI 这是在招摇的用户界面中的外观 在此处输入图片说明 as you can see on image, requested body is empty(while I have 2 parameters) and this is the issue. 如您在图像上看到的,请求的主体为空(而我有2个参数),这就是问题所在。 if I change the header from application/json to application/x-www-form-urlencoded then it will work(it will show all parameters in parameters list). 如果我将标题从application/json更改为application/x-www-form-urlencoded那么它将起作用(它将在参数列表中显示所有参数)。 how to make it show json object parameters in that list? 如何使其在该列表中显示json对象参数?

Your spec is correct and the displayed result in Swagger UI is correct and exactly as expected for OpenAPI 3.0 definitions. 您的规格正确,并且在Swagger UI中显示的结果正确,并且完全符合OpenAPI 3.0定义的预期。

Note there are two sections, "Parameters" (for parameters ) and "Request body" (for requestBody ). 请注意,有两个部分,“参数”(用于parameters )和“请求正文”(用于requestBody )。 In OpenAPI 3.0, parameters is only used for query parameters, path parameters, request headers and cookies; 在OpenAPI 3.0中, parameters仅用于查询参数,路径参数,请求标头和cookie; whereas requestBody is displayed in the "Request body" section. requestBody显示在“请求正文”部分。 You can click "Model" link to view the request body schema with property descriptions. 您可以单击“模型”链接以查看带有属性描述的请求主体架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM