OpenAPI Basic Structure

ouer1994發表於2019-09-06
  1. OpenAPI 版本
  2. 專案原資訊
  3. servers
  4. paths
  5. parameters
  6. request body
  7. responses
  8. 輸入輸出模型: schemas
  9. Authentication
# 指定 openapi 版本 3.0.0
openapi: 3.0.0
# 你的 Api 的資訊
info:
  # 專案名稱
  title: Sample API
  # 描述
  description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  # Api 專案版本
  version: 0.1.9

# 你的 Api 專案的訪問路徑(可以指定多個)
servers:
  - url: http://api.example.com/v1
    description: Optional server description, e.g. Main (production) server
  - url: http://staging-api.example.com
    description: Optional server description, e.g. Internal staging server for testing

# 你的 Api 介面
paths:
  # 介面路徑
  /users:
    # 請求方法 get/post ...
    get:
      # 簡介
      summary: Returns a list of users.
      # 描述
      description: Optional extended description in CommonMark or HTML.
      # 返回
      responses:
        # 狀態碼
        '200':  
          # 返回的描述
          description: A JSON array of user names
          # 返回 Content-Type: application/json
          content:
            application/json:
              schema: 
                type: array
                items: 
                  type: string
  /user/{userId}:
    get:
      summary: Returns a user by ID.
      # 請求引數 userId, 是 路徑上的請求引數, 必填, 整型, 最小值為1
      parameters:
        - name: userId
          # 路徑上的請求引數
          in: path
          # 必填
          required: true
          description: Parameter description in CommonMark or HTML.
          # 整型, 最小值為1
          schema:
            type : integer
            format: int64
            minimum: 1
      responses: 
        '200':
          description: OK
  /users/private:
    post:
      summary: Creates a user.
      # 請求體
      requestBody:
        required: true
        content:
          # Content-Type: application/json
          application/json:
            # 請求體資訊 定義
            schema:
              type: object
              properties:
                username:
                  type: string
      responses:
        '200':
          description: A user object.
          content:
            application/json:
              # 返回的模型
              schema:
                $ref: '#/components/schemas/User'
        '400':
          description: The specified user ID is invalid (not a number).
        '404':
          description: A user with the specified ID was not found.
        default:
          description: Unexpected error
      # 指定這個介面使用 BasicAuth 授權
      security:
        - BasicAuth: []

components:
  # 這是全域性的模型, 可以被引用 $ref
  schemas:
    User:
      # 模型中的屬性
      properties:
        id:
          type: integer
          format: int64
          # 返回示例 4
          example: 4
        name:
          type: string
          example: Jessica Smith
      # Both properties are required
      required:  
        - id
        - name
  # 定義授權
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章