我有一个带参考的JSON模式.我正在尝试使用JsonSchemaResolver解决所有引用.但是,不幸的是,ref无法解决,并出现如下错误.
我正在尝试通过解析所有引用来获取替换的JSON.
码:
var schemaFileContents = File.ReadAllText(schemaFileName);
JsonSchemaResolver resolver = new JsonSchemaResolver();
var result = JsonSchema.Parse(schemaFileContents, resolver);
Console.WriteLine(result);
JSON模式:
{
"$schema": "YYYYYYY",
"id": "XXXXXX",
"title": "Current Employee Details",
"description": "XXXXXXXXXXXXX",
"type": "object",
"properties": {
"EMP": {
"title": "Employee ",
"description": "Details of the Employee",
"$ref": "#/definitions/Employee"
}},
"definitions": {
"EmployeeAddress": {
"title": "Address",
"description": "EmployeeAddress - Present Address",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"EmployeeAddress"
]
},
"address": {
"title": "Address",
"type": "string"
},
"postalCode": {
"title": "Postal Code",
"type": "string"
}
},
"required": [
"postalCode",
"address"
]
},
"Employee": {
"title": "Party",
"description": "Employee Details",
"type": "object",
"properties": {
"firstName": {
"title": "First name",
"type": "string"
},
"address": {
"title": "Employee Address",
"$ref": "#/definitions/EmployeeAddress"
}
},
"required": [
"firstName"
]
}
}
}
错误:
Unhandled Exception: System.ArgumentException: Can not convert Array to Boolean.
at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.ProcessSchemaProperties(JObject schemaObject)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.BuildSchema(JToken token)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.Read(JsonReader reader)
at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader, JsonSchemaResolver resolver)
at Newtonsoft.Json.Schema.JsonSchema.Parse(String json, JsonSchemaResolver resolver)
解决方法:
看来您正在使用json模式V4,但JsonSchemaResolver希望使用json模式V3.它们之间的区别在于必填字段.尝试在具有bool值的属性级别上使用它,而不是在更高级别上使用数组值:
"address": {
"title": "Address",
"type": "string",
"required": true
}
根据docs,JsonSchemaResolver已过时.要以最新标准使用json模式,您需要使用separate package.使用JSchemaPreloadedResolver,请参见示例here.