OpenApi обязательное свойство во вложенных объектах не работает - PullRequest
0 голосов
/ 21 февраля 2019

Мне нужно описать API, имеющий в теле запроса объект с обязательными полями, и одно из этих полей - это сам объект, имеющий другой набор обязательных полей.

Я использую open api v3 и редактор swagger (https://editor.swagger.io/) После того, как я помещаю свой файл .yaml в редактор, я генерирую html-клиент (> генерируем client> html). Затем я открываюСтатическая страница index.html, сгенерированная в файле .zip, которая содержит следующую схему:

Table of Contents
body
secureoauthservicesv2Nested_nestedobj
body
id
Integer id of nested obj
nestedobj
secureoauthservicesv2Nested_nestedobj
secureoauthservicesv2Nested_nestedobj
nested object
field1 (optional)
String
field2 (optional)
String

Я ожидаю, что field1 будет обязательным, а field2 необязательным, но это не так.

Это мой .yamlфайл

openapi: 3.0.0
info:
    title: Example API
    description: Example API specification
    version: 0.0.1
servers:
  - url: https://example/api

paths:
  /secure/oauth/services/v2/Nested:
    post:
      summary: Try nested
      description: Used to post Nested obj
      requestBody:
        required: true
        content:
          application/json:
            schema:
                type: object 
                required:
                - id
                - nestedobj
                properties:
                    id:
                      type: integer
                      description: id of nested obj
                    nestedobj:
                      type: object 
                      required:
                      - field1
                      description: nested object
                      properties:
                        field1:
                          type: string
                        field2:
                          type: string
      responses:
        '200':
          description: Nested object OK

1 Ответ

0 голосов
/ 21 февраля 2019

Решено!

Я использовал компоненты и схемы, но я думаю, что это может быть ошибкой, открыл проблему в репозитории Swagger Editor: https://github.com/swagger-api/swagger-editor/issues/1952

openapi: 3.0.0
info:
    title: Example API
    description: Example API specification
    version: 0.0.2
servers:
  - url: https://example/api

paths:
  /secure/oauth/services/v2/Nested:
    post:
      summary: Try nested
      description: Used to post Nested obj
      requestBody:
        required: true
        content:
          application/json:
            schema:
                type: object 
                required:
                - id
                - nestedobj
                properties:
                    id:
                      type: integer
                      description: id of nested obj
                    nestedobj:
                      $ref: '#/components/schemas/nestedobj'
      responses:
        '200':
          description: Nested object OK

components:
  schemas:
    element:
      type: object
      required:
      - fieldArray1
      properties:
        fieldArray1:
          type: string
          description: field array
        fieldArray2:
          type: number
    nestedobj:
      type: object
      required:
      - field1
      description: nested object
      properties:
        field1:
          $ref: '#/components/schemas/woah'
        field2:
          type: string
    woah:
      type: object
      required:
      - woahthis
      description: woah this
      properties:
        field3:
          type: array
          items:
            $ref: '#/components/schemas/element'
        woahthis:
          type: number
          description: numeber woah this
...