additionalProperties: false 在 OpenAPI3.0.2 中不起作用

问题描述 投票:0回答:1

我使用的是OpenAPI:3.0.2

My openAPI properties are as follows:
    abc:
      type: object
      additionalProperties: false
      required:
        - something
        - id
      properties:
        something:
          $ref: '#/xyz'
        id:
          description: id.
          type: string
          minLength: 1
          maxLength: 100


When my request body is like this:
"abc": [
            {
                "something": "dfg",
                "id": "45345342",
                "ccc": "ugfyc"
            },
            {
                "something": "iut",
                "id": "45345342"
            }

        ]

即使发送了其他属性,它也不会引发任何错误或使架构无效

我尝试更改它的级别并添加未评估的属性,但它仍然不起作用

尝试使用 unevaluatedProperties: false,仍然不起作用

spring swagger openapi
1个回答
0
投票

您的 OpenAPI 架构和数据实例不同,这就是验证无法按预期工作的原因。

您的架构中有

abc
作为
type: object

    abc:
      type: object
      additionalProperties: false
      required:
        - something
        - id
      properties:
        something:
          $ref: '#/xyz'
        id:
          description: id.
          type: string
          minLength: 1
          maxLength: 100

并且您的数据实例的

abc
type: array
。因此,验证永远不会执行,因为您的
abc
数据实例与预期的
type
不匹配。

架构可以调整为以下内容:

  abc:
    type: array
    items:
      type: object
      additionalProperties: false
      properties:
        id:
          description: id
          type: string
          minLength: 1
          maxLength: 100
        ccc:
           type: string
        something:
          $ref: '#/xyz'
© www.soinside.com 2019 - 2024. All rights reserved.