在解析 json 模式时,在 anyOf 属性 下有多个引用,输出中只返回一个引用(最后一个)
On parsing a json schema with mulitple references under anyOf property, only one reference(last one) is returned in the output
我有一个有效的 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"
}
]
关于如何实现我想要的输出有什么想法吗?
在Json中,每个对象只能有一个特定的键一次。因此在一个对象中,您只能有一个名称为 $ref
的键。您在上面发布的 Json 无效;这取决于它的实现——理想情况下它应该抛出一个错误,但在这种情况下,看起来第二个正在覆盖第一个。
请注意,对于 $ref
,其他属性将被忽略,因此除了 $ref
.[=16= 之外,使用其他关键字(如 type
几乎没有意义]
我不完全确定,但看起来你想要实现的是说属性 "a" 和 "b" 应该存在,或者属性 "c" 和 "d" 应该存在。
您可以将 anyOf
子句替换为:
"anyOf": [
{
"required": ["a", "b"]
},
{
"required": ["c", "d"]
}
]
我有一个有效的 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"
}
]
关于如何实现我想要的输出有什么想法吗?
在Json中,每个对象只能有一个特定的键一次。因此在一个对象中,您只能有一个名称为 $ref
的键。您在上面发布的 Json 无效;这取决于它的实现——理想情况下它应该抛出一个错误,但在这种情况下,看起来第二个正在覆盖第一个。
请注意,对于 $ref
,其他属性将被忽略,因此除了 $ref
.[=16= 之外,使用其他关键字(如 type
几乎没有意义]
我不完全确定,但看起来你想要实现的是说属性 "a" 和 "b" 应该存在,或者属性 "c" 和 "d" 应该存在。
您可以将 anyOf
子句替换为:
"anyOf": [
{
"required": ["a", "b"]
},
{
"required": ["c", "d"]
}
]