嵌套对象中的OpenApi必需属性不起作用 [英] OpenApi required property in nested objects not working

查看:196
本文介绍了嵌套对象中的OpenApi必需属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要描述一个在请求主体中具有一个带有必填字段的对象的api,这些字段之一是一个对象本身具有另一组必填字段的对象.

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.

我正在使用开放式API v3和swagger编辑器( https://editor.swagger.io/ ) 将.yaml文件放到编辑器后,我生成一个html客户端(> generate client> html).然后,我使用以下模式打开在.zip文件中生成的静态页面index.html:

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

我希望field1是必需的,而field2是可选的,但不是必需的.

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

这是我的.yaml文件

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

推荐答案

已解决!

我使用了组件和架构,但是我认为这可能是一个错误,在swagger编辑器存储库上打开了一个问题: https://github.com/swagger-api/swagger-editor/issues/1952

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

这篇关于嵌套对象中的OpenApi必需属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆