Skip to content
UniKit

OpenAPI-Vorlage

Ein OpenAPI-3.1-Gerüst erzeugen: info, servers, tags, pro Ressource aufgefächerte CRUD-Pfade, components.schemas, securitySchemes und gängige Antwortcodes — als YAML, das direkt ins Repository kann.

Läuft lokal im BrowserAlle Berechnungen laufen in Ihrem Browser; Ihre Daten verlassen dieses Gerät nicht.

Dokumentinformationen

Authentifizierung
400 / 401 / 500 als allgemeine Antworten ergänzen

Ressourcen

Verfügbare Operationen: list, create, get, update, delete

openapi.yaml

Den Inhalt als openapi.yaml speichern; Swagger UI, Redoc und openapi-generator lesen die Datei direkt.

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: []

Statistik

Pfade2
Operationen5
Schemas4
Tags1
Zeilen0

Was dieses Werkzeug kann

  • Beim Projektstart zuerst den API-Rahmen festlegen: Titel, servers und Ressourcennamen eintragen, alle CRUD-Pfade und Schemas auf einmal erzeugen und das YAML im Team besprechen.
  • Mocks oder SDKs versorgen: dasselbe YAML in Swagger UI für die Doku und in openapi-generator für den Client-Code geben — eine gemeinsame Definition.
  • Ein Team standardisieren: Authentifizierung, Paging-Parameter und Fehlerstruktur kommen aus der Vorlage, statt pro Endpunkt neu erfunden zu werden.
  • Wenn Doku mehr kostet als der Code: Modellnamen und Regeln für den Primärschlüssel in die Ressourcenzeile eintragen, Pfade, operationIds und $refs entstehen automatisch.

Beispiel

Eingabe

Titel: Pet Store API
Version: 1.0.0
servers: https://api.example.com/v1
Authentifizierung: Keine
Ressourcen: pets:list:id:integer:no
Allgemeine Antworten: aus

Ausgabe

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

Statuscodes werden als String-Schlüssel ('200') geschrieben, weil OpenAPI das verlangt; der Modellname wird aus dem Ressourcennamen abgeleitet, aus pets wird Pet.

Häufige Fragen

Kann das erzeugte YAML direkt verwendet werden?

Ja. Das Dokument entspricht OpenAPI 3.1 und lässt sich direkt an Swagger UI, Redoc oder openapi-generator geben. Jedes Schema wird per $ref in components.schemas referenziert, es gibt also keine toten Verweise.

Wie sollten Ressourcen benannt werden?

Mit dem kleingeschriebenen Plural als Pfadsegment, etwa pets oder order-items. Modellnamen werden automatisch abgeleitet: order-items wird OrderItem, categories wird Category; passt die Ableitung nicht, lässt sich der Modellname in der Ressourcenzeile setzen.

Wann erscheinen die Paging-Parameter?

Nur wenn es eine list-Operation gibt und Paging aktiv ist: dann kommen page und pageSize als Query-Parameter hinzu und die Antwort zeigt auf den Wrapper <Modell>List. Ohne Paging referenziert list das Modell direkt und es wird kein List-Schema erzeugt.

Was erzeugt die Authentifizierungsoption?

bearer erzeugt ein Security-Schema http + bearer + JWT, basic eines mit http + basic und der API-Key eines als Header X-API-Key, jeweils referenziert im Feld security auf oberster Ebene. Bei „Keine“ entfallen securitySchemes und die 401-Antwort.

Können operationIds kollidieren?

Nein. Sammlungsoperationen nutzen den Plural des Ressourcennamens (listPets), Operationen auf einem Element den Singular des Modells (getPet, replacePet, deletePet). Doppelte Ressourcennamen werden schon bei der Prüfung abgelehnt.

Schlagwörter:openapiswaggerapi specrest apiswagger yaml

Ähnliche Werkzeuge