Swagger OpenAPI有一些未知的值

Swagger OpenAPI有一些未知的值,swagger,openapi,Swagger,Openapi,我正在读: 如果我看一个例子,有些事情我不明白 openapi: 3.0.0 info: version: 1.0.0 title: Simple API description: A simple API to illustrate OpenAPI concepts servers: - url: https://example.io/v1 components: securitySchemes: BasicAuth: type: http

我正在读:

如果我看一个例子,有些事情我不明白

openapi: 3.0.0
info:
  version: 1.0.0
  title: Simple API
  description: A simple API to illustrate OpenAPI concepts

servers:
  - url: https://example.io/v1

components:
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
security:
  - BasicAuth: []

paths:
  /artists:
    get:
      description: Returns a list of artists 
      parameters:
        - name: limit
          in: query
          description: Limits the number of items on a page
          schema:
            type: integer
        - name: offset
          in: query
          description: Specifies the page number of the artists to be displayed
          schema:
            type: integer
      responses:
        '200':
          description: Successfully returned a list of artists
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  required:
                    - username
                  properties:
                    artist_name:
                      type: string
                    artist_genre:
                      type: string
                    albums_recorded:
                      type: integer
                    username:
                      type: string
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                type: object
                properties:   
                  message:
                    type: string

    #  ----- Added lines  ----------------------------------------
    post:
      description: Lets a user post a new artist
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object 
              required:
                - username
              properties:
                artist_name:
                  type: string
                artist_genre:
                  type: string
                albums_recorded:
                  type: integer
                username:
                  type: string

      responses:
        '200':
          description: Successfully created a new artist

        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                type: object
                properties:   
                  message:
                    type: string
    #  ---- /Added lines  ----------------------------------------
  • 为什么
    /get
    的响应包含:
    required:username
    ?这是什么意思? 如果我不需要基本身份验证,可以删除此行吗

  • 为什么我们需要在
    post
    下为
    requestBody
    编写
    required:true
    post
    包含body不是最基本的吗


  • 你的问题的答案是:

  • 为什么/get的响应包含:必需:username?这是什么意思?如果我不需要基本身份验证,可以删除此行吗?
    Ans:用户名为必填项意味着它是必填项,即响应必须包含此字段,其中包含一些值。它与基本身份验证无关。因此,如果应用程序未使用该行,则可以删除该行

  • 为什么我们需要在帖子下面为requestBody写required:true?帖子包含正文不是最基本的吗?
    Ans:
    必需:true
    不是必须在此处写入的。这是一个可选字段,post必须有一个请求主体。是的,你说得对。帖子必须包含请求主体,这是一件基本的事情

  • 如果您有任何疑问,请告诉我。谢谢