当前位置: 首页 > 知识库问答 >
问题:

allOf项的属性可以包含另一个allOf项定义的$ref吗?

翁阳曜
2023-03-14

“#/allof/1/properties/body/properties/foo”是一个有效的$ref吗?
正如本文解释$ref用法时所述,看起来应该是。
但是AJV和https://www.jsonschemavalidator.net/似乎不同意。

尝试使用一个普通的JSON指针,它似乎绝对是可能的:https://repl.it/repls/wretchedspiritedmammoth

这就是我正在测试的模式。

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "allOf": [
    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "$id": "http:/example.org/example.schema.json",
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "body": {
          "type": "object"
        }
      },
      "required": [
        "type",
        "body"
      ]
    },
    {
      "properties": {
        "body": {
          "$schema": "http://json-schema.org/draft-06/schema#",
          "$id": "http://example.org/foo.schema.json",
          "type": "object",
          "properties": {
            "foo": {
              "type": "number",
              "minimum": 1
            },
            "bar": {
              "$ref": "#/allOf/1/properties/body/properties/foo"
            }
          },
          "required": [
            "foo",
            "bar"
          ]
        }
      }
    }
  ]
}

无法解析id http://example.org/foo.schema.json中的引用#/allof/1/properties/body/properties/foo

共有1个答案

莘绍元
2023-03-14

尽管在子模式中设置了$id,但模式基本上是正确的。

“$id”关键字定义架构的URI,以及解析架构中其他URI引用所依据的基URI。

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-00#section-9.2

"bar": {
  "$ref": "#/properties/foo"
}

从另一个属性模式直接引用一个属性模式是有效的,但更常见的最佳实践是将这样的模式放在“definitions”关键字下,并让两个属性都引用该位置。为了最大限度地清晰,我建议如下:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "allOf": [
    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "$id": "http:/example.org/example.schema.json",
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "body": {
          "type": "object"
        }
      },
      "required": [
        "type",
        "body"
      ]
    },
    {
      "properties": {
        "body": {
          "$schema": "http://json-schema.org/draft-06/schema#",
          "$id": "http://example.org/foo.schema.json",
          "type": "object",
          "properties": {
            "foo": {
              "$ref": "#/definitions/whateverYouWantToCallIt"
            },
            "bar": {
              "$ref": "#/definitions/whateverYouWantToCallIt"
            }
          },
          "required": [
            "foo",
            "bar"
          ]
        }
      },
      "definitions": {
        "whateverYouWantToCallIt": {
          "type": "number",
          "minimum": 1
        }
      }
    }
  ]
}
 类似资料:
  • 我正试图通过使用< code>allOf来解决这个swagger API继承的问题。这是我的swagger yaml文件。 当我将其粘贴到在线编辑器中时,我收到以下错误,我很难弄清楚。

  • 我正在生成Restendpoint,包括向生成的代码添加Openapi/Swagger注释。 虽然它可以很好地处理基本类型,但我在自定义类方面有一些问题。 现在我有很多自定义类的重复模式条目(使用@Schema(实现=MyClass.class)),但至少需要的信息在那里。然而,我想找到一种方法来删除重复的模式条目,同时保留附加信息。 在一个讨论$ref和缺乏兄弟属性的github问题上,我发现了

  • 问题内容: 我目前正在使用CompletableFuture supplyAsync()方法向公共线程池提交一些任务。以下是代码片段的样子: 我想知道下面与上面的代码有何不同。我从下面的代码中删除了父completableFuture,并为每个completableFuture添加了join()而不是getNow(): 我在spring服务中使用了它,线程池耗尽有问题。任何指针深表赞赏。 问题答案

  • 我希望避免文件中杂乱的东西,而在我看来,放在单独的文件中会更好。 应该类似于 这有可能吗?如果不是,什么是避免杂乱的明智方法呢?

  • 所以我有一个多项目设置,看起来像这样

  • 问题内容: 我的文件看起来像: 我现在想要的是进行术语搜索,其中给定数组可能看起来像: 预期命中应在哪里: 但是,当我执行类似的查询时: 由于示例1和示例2都包含tag1或tag3,因此获得了“示例1”和“示例2”作为匹配。通过查看文档中的术语,我发现术语实际上是一个包含查询。 在这种情况下,如何在使用tag1和tag3查询时确保示例2是唯一匹配项? 问题答案: 您需要通过添加到过滤器来将 执行模