swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Required nested properties in RequestBody not working as expected

Open mbianchidev opened this issue 6 years ago • 6 comments
trafficstars

I tried to generate an OpenApi definition using a nested object with a field marked as required and a field marked as optional but it hasn't worked as intended.

This is the Swagger/OpenAPI definition I used:

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

This is the result I got:

Screenshot

image

Issue

As you can see in the screenshot fields are not marked as required as expected. The issue is that I can't make field1 required and field2 optional As you can see, in my html client, and field1 and field2 are both optional, while in my yaml i declared field1 as required and field2 as optional.

Opened the issue as @shockey requested (I thought it was a swagger-editor issue). I found a workaround using components as follows:

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
Related question on stackoverflow:

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

Related issues/PRs

https://github.com/swagger-api/swagger-editor/issues/1952

mbianchidev avatar Apr 08 '19 08:04 mbianchidev

Any news? 👀

mbianchidev avatar Aug 23 '21 15:08 mbianchidev

Any news ???)

arkadikhach avatar Dec 07 '22 11:12 arkadikhach

lol

AlmightyLks avatar Jan 14 '23 22:01 AlmightyLks

Bump. I assume no news?

k-gifford avatar May 15 '23 16:05 k-gifford

👀 (bump)

unicornzero avatar Aug 27 '23 16:08 unicornzero

As it seems, @swagger-api doesn't do anything about any created issue ever. Attached tags or closed issues are all due to bots or auto-close. So the hope is lost here.

AlmightyLks avatar Aug 28 '23 10:08 AlmightyLks