跳到主內容
UniKit

OpenAPI 範本

產生 OpenAPI 3.1 骨架:info、servers、tags、依資源展開的 CRUD 路徑、components.schemas、securitySchemes 與常見回應碼,輸出可直接放進儲存庫的 YAML。

瀏覽器本機執行所有計算都在你的瀏覽器裡完成,資料不會離開本機。

文件資訊

驗證方式
補上 400 / 401 / 500 通用回應

資源

可用操作:list, create, get, update, delete

openapi.yaml

把內容存成 openapi.yaml,可直接被 Swagger UI、Redoc 或 openapi-generator 讀取。

openapi: 3.1.0
info:
  title: Example API
  version: 1.0.0
  description: 由 UniKit 生成的 OpenAPI 骨架,按需增删字段即可。
servers:
  - url: https://api.example.com/v1
tags:
  - name: pets
    description: Pet resources
paths:
  /pets:
    get:
      tags:
        - pets
      summary: List pets
      operationId: listPets
      parameters:
        - name: page
          in: query
          description: Page number
          schema:
            type: integer
            minimum: 1
            default: 1
        - name: pageSize
          in: query
          description: Items per page
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 20
        - name: sort
          in: query
          description: Sort expression, e.g. -createdAt
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PetList'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    post:
      tags:
        - pets
      summary: Create a pet
      operationId: createPet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PetInput'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /pets/{id}:
    get:
      tags:
        - pets
      summary: Get a pet
      operationId: getPet
      parameters:
        - name: id
          in: path
          required: true
          description: Pet identifier
          schema:
            type: integer
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: Not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    put:
      tags:
        - pets
      summary: Replace a pet
      operationId: replacePet
      parameters:
        - name: id
          in: path
          required: true
          description: Pet identifier
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PetInput'
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: Not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    delete:
      tags:
        - pets
      summary: Delete a pet
      operationId: deletePet
      parameters:
        - name: id
          in: path
          required: true
          description: Pet identifier
          schema:
            type: integer
      responses:
        '204':
          description: Deleted
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: Not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
        message:
          type: string
        details:
          type: array
          items:
            type: string
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
        name:
          type: string
          description: Display name
        description:
          type:
            - string
            - 'null'
          description: Optional long text
        createdAt:
          type: string
          format: date-time
          readOnly: true
        updatedAt:
          type: string
          format: date-time
          readOnly: true
    PetInput:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          description: Display name
        description:
          type:
            - string
            - 'null'
          description: Optional long text
        createdAt:
          type: string
          format: date-time
          readOnly: true
        updatedAt:
          type: string
          format: date-time
          readOnly: true
    PetList:
      type: object
      required:
        - items
        - total
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/Pet'
        total:
          type: integer
          description: Total number of records
        page:
          type: integer
          minimum: 1
        pageSize:
          type: integer
          minimum: 1
          maximum: 100
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
security:
  - bearerAuth: []

統計

路徑數2
操作數5
Schema 數4
標籤數1
行數0

這個工具能做什麼

  • 新專案立項先把介面骨架定下來:填好標題、servers 和資源名,一次生成所有 CRUD 路徑與 schema,團隊先對著這份 YAML 討論欄位。
  • 給前端造 mock 或 SDK:把 YAML 丟給 Swagger UI 看文件、給 openapi-generator 生成客戶端程式碼,兩邊共用同一份定義。
  • 統一團隊規範:鑑權方式、分頁引數、錯誤響應結構都由模板給出,避免每個介面各寫一套。
  • 補文件比寫程式碼還累時:已有模型名和主鍵規則填進資源行,剩下的路徑、operationId、$ref 全部自動生成。

範例

輸入

標題:Pet Store API
版本:1.0.0
servers:https://api.example.com/v1
鑑權:無
資源:pets:list:id:integer:no
通用響應:關閉

輸出

openapi: 3.1.0
info:
  title: Pet Store API
  version: 1.0.0
servers:
  - url: https://api.example.com/v1
tags:
  - name: pets
    description: Pet resources
paths:
  /pets:
    get:
      tags:
        - pets
      summary: List pets
      operationId: listPets
      parameters:
        - name: sort
          in: query
          description: Sort expression, e.g. -createdAt
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
        message:
          type: string
        details:
          type: array
          items:
            type: string
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
        name:
          type: string
          description: Display name
        description:
          type:
            - string
            - 'null'
          description: Optional long text
        createdAt:
          type: string
          format: date-time
          readOnly: true
        updatedAt:
          type: string
          format: date-time
          readOnly: true
    PetInput:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          description: Display name
        description:
          type:
            - string
            - 'null'
          description: Optional long text
        createdAt:
          type: string
          format: date-time
          readOnly: true
        updatedAt:
          type: string
          format: date-time
          readOnly: true

狀態碼會被寫成字串鍵('200'),這是 OpenAPI 的要求;模型名由資源名推導,pets 得到 Pet。

常見問題

生成的 YAML 能直接跑起來嗎?

能。文件符合 OpenAPI 3.1,可以直接餵給 Swagger UI、Redoc 或 openapi-generator;路徑裡的 schema 用 $ref 指向 components.schemas,不會出現懸空引用。

資源名該怎麼填?

用小寫複數的路徑片段,例如 pets、order-items。模型名會自動推導:order-items → OrderItem,categories → Category;如果推導不符合預期,可以在資源行裡顯式給出模型名。

分頁引數是什麼時候出現的?

只有 list 操作且開啟分頁時才會加 page / pageSize 查詢引數,並把響應指向 <模型>List 包裝物件;關掉分頁後 list 直接返回模型陣列的引用,文件裡也不會出現 List schema。

鑑權部分生成什麼?

bearer 生成 http + bearer + JWT 的 securityScheme,basic 生成 http + basic,API Key 生成 header 形式的 X-API-Key,並在頂層加 security 引用。選擇「無」時既不寫 securitySchemes 也不寫 401 響應。

operationId 會衝突嗎?

不會。list 用資源複數名(listPets),單個資源的操作統一用模型名單數(getPet、replacePet、deletePet)。同一資源只生成一份,重複資源名會在校驗階段直接報錯。

關鍵字:openapi 模板接口文档api 规范crud 接口接口骨架openapiswaggerapi specrest apiswagger yaml

同類工具