OpenApi required property in nested objects not working

断了今生、忘了曾经 提交于 2021-02-07 06:44:06

问题


I need to describe an api having in request body an object with required fields and one of these fields it's an object itself having another set of required fields.

I'm using open api v3 and swagger editor (https://editor.swagger.io/) After i put my .yaml file onto the editor I generate an html client (> generate client > html). Then I open the static page index.html generated in the .zip file obtaning this schema:

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

I expect field1 to be required and field2 to be optional but it's not.

This is my .yaml file

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:


Solved!

I used components and schemas, but I think this could be a bug, opened an issue on swagger editor repo: 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


来源:https://stackoverflow.com/questions/54803837/openapi-required-property-in-nested-objects-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!