【问题标题】:OpenApi v3: additionalProperties false with referenced schemasOpenApi v3:additionalProperties false 与引用的架构
【发布时间】:2020-09-24 12:02:44
【问题描述】:

关于这个主题有一些问题,但我没有找到正确的方法。

我想要的是在一个地方定义所有参数并重复使用它而无需再次编写它。我已经通过使用“allOf”得到了这一点,但这限制了“additionalProperties”的使用。

我的架构有这样的结构:

SchemaBase:
  type: object
  properties:
    foo:
      type: string

SchemaFull:
  allOf:
    - $ref: '#/components/schemas/SchemaBase'
    - type: object
      properties:
        bar:
          type: string

我尝试使用 using definitions,但在 OpenApi 版本 3 中似乎不再使用了。

Here is a solution 但它不是我要找的,因为那是属性,而不是整个架构。

【问题讨论】:

    标签: swagger jsonschema openapi


    【解决方案1】:

    你需要像这样使用components

    openapi: 3.0.1
    info:
      title: OAS 3
      version: 1.0.0
    tags:
    - name: example
    paths:
      /exe:
        post:
          requestBody:
            content:
              application/json:
                schema:
                  additionalProperties: false
                  allOf:
                    - $ref: '#/components/schemas/SchemaBase'
                    - type: object
                      properties:
                        bar:
                          type: string
          responses:
            200:
              description: Foo
              content: {}
    components:
      schemas:
        SchemaBase:
          type: object
          properties:
            foo:
              type: string
    

    你可以在这里看到和玩这个:https://editor.swagger.io/


    JSON 模式映射:

    {
      "additionalProperties": false,
      "allOf": [
        { "$ref": "#/definitions/SchemaBase" },
        {
          "type": "object",
          "properties": {
            "foo": {
              "type": "string"
            }
          }
        }
      ],
      "definitions": {
        "SchemaBase": {
          "type": "object",
          "properties": {
            "foo": {
              "type": "string"
            }
          }
        }
      }
    }
    

    【讨论】:

    • 对不起,可能是我没有解释自己。我想要的是使用“additionalProperties:false”来验证模式的联合,但似乎不可能。我已经尝试了几种不同的组合,但没有成功。
    • 您使用的是什么验证器? - 我添加了additionalProperties: false,这是一个有效的 OAS 架构
    • 我正在使用 express-openapi,additionalProperties: false 可以使用它,但不能与 allOf 一起使用,因为只验证一种或另一种模式。
    • 我说的是模式不起作用,它在语法上是正确的,但它会检查没有任何参数是有效的,因为 additionalProperties 在兄弟级别工作,并且在 @987654331 内部没有输入@.
    • 明白,我已经添加了相应的架构并使用jsonschemavalidator.net 重新创建了问题。我试试看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2017-05-05
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多