Swagger использует ту же модель, но разные примеры - PullRequest
0 голосов
/ 29 января 2019

Мой API может повторить 200 или 400, произошла ошибка.Я хочу, чтобы они использовали одну и ту же модель, но разные примеры.В моем коде ниже я хочу, чтобы мои 200 Info говорили что-то вроде «Все в порядке», а 400 - «Not ok».

 responses:
        '200':
          description: >-
            Invoice received for processing but beware of potentional warnings
            and information
          schema:
            $ref: '#/definitions/Response'
        '400':
          description: Invoice could not be processed due to fatal errors
          schema:
            $ref: '#/definitions/Response'

  Response:
    type: object
    properties:
      Info:
        type: string
          information
      Messages:
        type: object
        properties:
          Fatal:
            type: array
            items:
              type: object
          Warning:
            type: array
            items:
              type: object
          Information:
            type: array
            items:
              type: object

1 Ответ

0 голосов
/ 29 января 2019

В OpenAPI 2.0 ответы поддерживают ключевое слово examples для указания примеров ответов для различных кодов состояния HTTP и типов носителей.

      responses:
        '200':
          description: >-
            Invoice received for processing but beware of potential warnings
            and information
          schema:
            $ref: '#/definitions/Response'
          examples:
            application/json:
              Info: OK
            # Or using JSON syntax for the example value:
            # application/json: {"Info": "OK"}

        '400':
          description: Invoice could not be processed due to fatal errors
          schema:
            $ref: '#/definitions/Response'
          examples:
            application/json:
              Info: Oops
              Messages:
                Fatal:
                  - Houston, we have a problem
            # Or using JSON syntax for the example value:
            # application/json:
            #   {
            #     "Info": "Oops",
            #     "Messages": [
            #       {
            #         "Fatal": [
            #           {"Custom error": "Houston, we have a problem"}
            #         ]
            #       }
            #     ]
            #   }
...