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

我有一个有效的 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 property


On 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"

        }

    ]

关于如何实现我想要的输出有什么想法吗?


jeck猫
浏览 65回答 1
1回答

手掌心

在 Json 中,每个对象只能有一个特定的键一次。所以在一个对象中,你只能有一个名称为 key 的键$ref。您在上面发布的 Json 无效;这取决于它的实现——理想情况下它应该抛出一个错误,但在这种情况下,看起来第二个正在覆盖第一个。请注意,对于 a $ref,其他属性将被忽略,因此type除了$ref.我不完全确定,但看起来你想要实现的是说属性“a”和“b”应该存在,或者属性“c”和“d”应该存在。您可以通过将anyOf子句替换为:"anyOf": [    {        "required": ["a", "b"]    },    {        "required": ["c", "d"]    }]
打开App,查看更多内容
随时随地看视频慕课网APP