swagger-codegen
swagger-codegen copied to clipboard
Required nested properties in RequestBody not working as expected
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

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
Any news? 👀
Any news ???)
lol
Bump. I assume no news?
👀 (bump)
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.