0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenAPIのallOfの使い方

Posted at

概要

OpenAPIのallOfの使用方法です。

利用シーン

同じ要素を含む複数のオブジェクトを定義する場合に使用します。

サンプルjson

以下のようなjsonを生成します。

{
    "object_a": {
      "id": 1,
      "name": "NONE",
      "element1": 3,
      "element2": "string",
      "element3": "SUNDAY"
    },
    "object_b": {
      "id": 1,
      "name": "NONE",
      "element1": 5,
      "element3": "enum_B",
      "element4": "JANUARY"
    }
}

共通要素

  • id
  • name
  • element1 (integer)

固有要素

  • element2 (string)
  • element3 (enumの型違い)
    • object_a は、SUNDAY,MONDAY,TUESDAY,...
    • object_b は、JANUARY,FEBURUARY,MARCH,...
  • element4 (string)

yamlファイルの定義

paths:
  /example:
    get:
      parameters:
      - description: API Key
        in: query
        name: key
        required: true
        type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                - $ref: '#/components/schemas/response_type1'
                - $ref: '#/components/schemas/response_type2'

components:
  schemas:
    response_type1:
        type: object
        allOf:
          - $ref: '#/components/schemas/object_base'
          - properties:
              element2:
                type: integer
              element3:
                type: string
                enum:
                    - SUNDAY
                    - MONDAY
                    - TUESDAY
        required:
          - element2
          - element3
    response_type2
        type: object
        allOf:
          - $ref: '#/components/schemas/object_base'
          - properties:
              element3:
                type: string
                enum:
                    - JANUARY
                    - FEBURUARY
                    - MARCH
              element4:
                type: string
        required:
          - element3
    object_base:
        type: object
        nullable: false
        properties:
          id:
            type: integer
            format: int64
          name:
            type: string
          element1:
            type: integer
        required:
          - id
          - name
          - element1
  • element3のように同じ名前でEnumの型が違うような場合にも利用できます
  • object_base でrequired指定した要素とallOf内でrequired指定した要素が必須要素になります
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?