OpenApi v3:附加属性 false 引用的架构

Gab*_*res 7 jsonschema swagger openapi

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

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

我的架构具有以下结构:

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

SchemaFull:
  allOf:
    - $ref: '#/components/schemas/SchemaBase'
    - type: object
      properties:
        bar:
          type: string
Run Code Online (Sandbox Code Playgroud)

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

这是一个解决方案,但它不是我正在寻找的,因为这是针对属性的,而不是整个架构。

Man*_*lon 0

你需要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
Run Code Online (Sandbox Code Playgroud)

您可以在这里看到并使用它: 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"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 抱歉,可能是我没有解释清楚。我想要的是使用“additionalProperties: false”来验证模式的联合,但似乎这是不可能的。我已经尝试过几种不同的组合,但没有成功。 (2认同)
  • 我使用的是express-openapi,“additionalProperties: false”适用于它,但不能与“allOf”一起使用,因为只验证一个或另一个模式。 (2认同)
  • 我想说的是,模式不起作用,它在语法上是正确的,但它会检查参数是否有效,只是因为“additionalProperties”在同级级别工作,并且没有进入“allOf”内部。 (2认同)