Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenApi required property in nested objects not working

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
like image 433
M. Black Avatar asked Feb 21 '19 09:02

M. Black


People also ask

Does OpenAPI use JSON schema?

By default, schemas in OpenAPI 3.1 are assumed to use OpenAPI 3.1's custom JSON Schema dialect. This dialect includes full support for all draft 2020-12 features plus a few additional keywords and format values.

What is oneOf in OpenAPI?

OpenAPI 3.0 provides several keywords which you can use to combine schemas. You can use these keywords to create a complex schema, or validate a value against multiple criteria. oneOf – validates the value against exactly one of the subschemas. allOf – validates the value against all the subschemas.

What is $ref in OpenAPI?

With OpenAPI 3.0, you can reference a definition hosted on any location. It can be the same server, or another one – for example, GitHub, SwaggerHub, and so on. To reference a definition, use the $ref keyword: $ref: 'reference to definition'

What is oas3 in swagger?

OAS 3.0, which is based on the original Swagger 2.0 specification, is meant to provide a standard format to unify how an industry defines and describes RESTful APIs.


1 Answers

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

EDIT 23/08/21: I opened a bug in swagger-codegen github in april 2019 but it still has no response whatsoever

like image 150
M. Black Avatar answered Sep 25 '22 21:09

M. Black