json schema $ref resolution error

902 views
Skip to first unread message

Jiajie Duan

unread,
Jul 21, 2014, 5:59:12 PM7/21/14
to json-...@googlegroups.com

I have 3 schemas:

child schema:

{
    "title": "child_schema",
    "type": "object",
    "properties": {
        "wkid": {
            "type": "number",
            "description": " well-known ID"
        }
    },
    "additionalProperties": false,
    "required": ["wkid"]
}

parent schema: (this parent schema has a $ref pointing to the location where I stored this child schema)

{
    "title": "parent_schema",
    "type": "object",
    "properties": {
        "x": {
            "type": "number"
        },
        "y": {
            "type": "number"
        },
        "spatialReference": {
            "$ref": ".....(for sure it is correct)"
        }
    },
    "additionalProperties": false
}

grandparent schema: (this schema has a $ref key word pointing to the location where I store my parent schema)

{
    "title": "grandpa_schema",
    "type": "object",
    "properties": {
        "geometry": {
            "type": "object",
            "oneOf": [{
                "$ref": ".....(for sure it is correct)"
            }]
        }
    },
    "additionalProperties": false
}

Now comes the problem:

I am trying to use the grandparent schema to validate this JSON data, but I keep on getting a resolution error.

{
    "geometry": {
        "x": -118.15,
        "y": 33.80,
        "spatialReference": {
            "wkid": 4326
        }
    }
}

conditions: 1. The child schema itself is correct for sure! 2. I can use the parent schema to validate the data below successfully:

{
        "x": -118.15,
        "y": 33.80,
        "spatialReference": {
            "wkid": 4326
        }
}

So why am I getting a exception RefResolutionError?

Jiajie Duan

unread,
Jul 21, 2014, 6:07:32 PM7/21/14
to json-...@googlegroups.com
Additional information:

If I change the parent schema like this (by copy the whole content of child schema to parent schema), everything will work. But I do not want to do like this

{
    "title": "parent_schema",
    "type": "object",
    "properties": {
        "x": {
            "type": "number"
        },
        "y": {
            "type": "number"
        },
        "spatialReference": {

             "title": "child_schema",
            "type": "object",
            "properties": {
                 "wkid": {
                     "type": "number",           
                 }
},
            "additionalProperties": false,
            "required": ["wkid"]
} }, "additionalProperties": false }

Geraint

unread,
Jul 22, 2014, 11:54:23 AM7/22/14
to json-...@googlegroups.com
What tool/library are you using?  For some tools, schemas are not fetched automatically, so you have to explicitly load/add schemas before they're available.

Geraint

Jiajie Duan

unread,
Jul 22, 2014, 5:58:06 PM7/22/14
to json-...@googlegroups.com

Hi Geraint, I am using jsonschema library.
I did load the schema.

My schema works fine as long as I dont use the grandpa schema. (seems like the $ref only goes to one level deep of a sub schema and does not go further)

The $ref value for parent schema is : "file:child.json"
The $ref value for grandpa schema is: "file:parent.json"
Both are using relative path. If I use absolute path, there is no issue.

Geraint

unread,
Jul 24, 2014, 11:10:46 AM7/24/14
to json-...@googlegroups.com
Do you mean the Python one?

Those $ref values might be incorrect.  "file:child.json" is not a relative path (because "file:" is a protocol, just like "http://").  Have you tried just using "child.json"?

Jiajie Duan

unread,
Jul 24, 2014, 11:36:58 AM7/24/14
to json-...@googlegroups.com
Yes. I am using the python one.
I tried just using "child.json" and it does not work.
If I use "file:child.json", it works. But it wont work if I use recursively. For example: in grandpa I have "file:parent.json", in parent I have "file:child.json". Then it wont work. 

Geraint

unread,
Jul 24, 2014, 12:13:40 PM7/24/14
to json-...@googlegroups.com
Could be a bug in the library - you should probably raise an issue on that particular project.

Joe McIntyre

unread,
Jul 25, 2014, 5:46:48 PM7/25/14
to json-...@googlegroups.com
Jiajie,

I was able to use the schemas for both valid and invalid JSON content using the jsonschema (Python) 2.3.0 library. I used both web based refs (http://localhost) and file based refs (pre-loading the schemas). For the schemas above,
 - I added an "id" element to the top of the parent and child elements with their locations ("id":"http://localhost:8080/child_schema.json" and "id":"http://localhost:8080/parent_schema.json").
 - in the grandparent schema, the "$ref":"http://localhost:8080/parent_schema.json"
 - in the parent schema, the "$ref":"http://localhost:8080/child_schema.json"
I created a directory with child_schema.json and parent_schema.json in it, then used the built in Python HTTP server (python -m SimpleHTTPServer 8080) to make them accessible to the grandparent schema.

I suspect then that the issue is with the $ref content and/or the lack of id in the referenced schemas.

Jiajie Duan

unread,
Aug 5, 2014, 2:04:10 PM8/5/14
to json-...@googlegroups.com
Hi Joe,

Thank you for your help.
If I push all the schema on a server, I do not even need to use id to define any scope. I just need to change the relative path to http path...

Joe McIntyre

unread,
Aug 5, 2014, 2:13:55 PM8/5/14
to json-...@googlegroups.com
Jiajie,

Yes, the server reference will fetch the schema. However, it is still useful to retain the id.

Using the id at the top level of the referenced schema has the advantage that the schema can be referenced using the same $ref independent of how it is loaded. This is helpful if you think the schema might be loaded from a file system or local repository, in addition to the original URI location.

Good to hear you have made progress,
Joe.
Reply all
Reply to author
Forward
0 new messages