简体   繁体   中英

Unable to parse JSON Schema and resolve the reference

I am having one JSON Schema with ref. I am trying to resolve all the ref using JsonSchemaResolver . But, unfortunately , ref is not resolved and getting an error as below.

I am trying to get the substituted JSON by resolving all the ref.

Code:

        var schemaFileContents = File.ReadAllText(schemaFileName);
        JsonSchemaResolver resolver = new JsonSchemaResolver();
        var result = JsonSchema.Parse(schemaFileContents, resolver);
        Console.WriteLine(result);

JSON Schema:

{
  "$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"
        ]
      }
    }
  }

Error:

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)

It looks like you are using json schema V4 but JsonSchemaResolver expects the json schema V3 . The difference between them is in required field. Try to use it on the property level with bool value instead of the array value on the higher level:

"address": {
    "title": "Address",
    "type": "string",
    "required": true
}

According to docs JsonSchemaResolver is obsolete. To use json schema with the latest standards you need to use the separate package . Use the JSchemaPreloadedResolver , see the example here

JsonSchemaResolver is now deprecated. Try below and use Newtonsoft.Json.Schema NuGet

var schemaFileContents = File.ReadAllText(schemaFileName);
JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
var result = JSchema.Parse(schemaFileContents, resolver);
Console.WriteLine(result);       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM