为了账号安全,请及时绑定邮箱和手机立即绑定

在 anyOf 属性下解析具有多个引用的 json 模式时,输出中仅返回一个引用(最后一个)

在 anyOf 属性下解析具有多个引用的 json 模式时,输出中仅返回一个引用(最后一个)

C#
jeck猫 2022-11-22 16:20:44
我有一个有效的 json 模式,如下所示 {      "$schema": "http://json-schema.org/draft-07/schema#",      "$id": "abcd",      "title": "test schema",      "description": "............",      "type": "object",      "properties": {         "a": {           ...........           ...........          },         "b": {          .........          ........          .........          },         "c": {          ...........          ..........          },         "d": {          ...........          ..........          }       },    "anyOf": [        {        "type": "object",              "$ref": "#/properties/a",              "$ref": "#/properties/b"        },            {             "type": "object",              "$ref": "#/properties/c",              "$ref": "#/properties/d"            }        ]    }上面的模式存储在一个文件中,我正在加载它进行解析,如下所示JSchema schema =    JSchema.Parse(File.ReadAllText(@"D:\Backups\testschema.json"));所以当我查看模式的输出时,它如下所示My Json Schema{  "$schema": "http://json-schema.org/draft-07/schema#",  "$id": "abcd",  "title": "test schema",  "description": "............",  "type": "object",  "properties": {     "a": {       ...........       ...........      },     "b": {      .........      ........      .........      },     "c": {      ...........      ..........      },     "d": {      ...........      ..........      }   },"anyOf": [    {          "$ref": "#/properties/b"    },        {          "$ref": "#/properties/d"        }    ]}I'm wondering why I'm getting only the last reference under the anyOf propertyOn parsing shouldn't the output be the same as that in the file?Am I missing something?My desired output under anyOf is "anyOf": [    {    "type": "object",          "$ref": "#/properties/a",          "$ref": "#/properties/b"    },        {         "type": "object",          "$ref": "#/properties/c",          "$ref": "#/properties/d"        }    ]关于如何实现我想要的输出有什么想法吗?
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

在 Json 中,每个对象只能有一个特定的键一次。所以在一个对象中,你只能有一个名称为 key 的键$ref。您在上面发布的 Json 无效;这取决于它的实现——理想情况下它应该抛出一个错误,但在这种情况下,看起来第二个正在覆盖第一个。


请注意,对于 a $ref,其他属性将被忽略,因此type除了$ref.


我不完全确定,但看起来你想要实现的是说属性“a”和“b”应该存在,或者属性“c”和“d”应该存在。


您可以通过将anyOf子句替换为:


"anyOf": [

    {

        "required": ["a", "b"]

    },

    {

        "required": ["c", "d"]

    }

]


查看完整回答
反对 回复 2022-11-22
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信