Skip to content

Root level $ref and $id in Draft 7 #894

@lud

Description

@lud

Hello,

In my JSON schema library a user reported a case that was not properly handled by my library. The case when $ref has a sibling $id but there is no "outer scope".

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/schema.json",
  "$ref": "#/definitions/foo",
  "definitions": {"foo":  {"type": "object"}}
}

From what I understand from the docs, $ref should resolve against the $id because $id defines the base URI of the whole schema. Though I am not 100% sure of what should happen depending on the $ref relative URL. In my fix the above schema is now valid and the $ref resolves to http://example.com/schema.json#/definitions/foo.

I also added a test to cover what happens if there is NO base URI defined with $id. In that case, if the ref is a pointer like #/definitions/foo then everything is fine, but if the ref is something like other.json then it is a schema parse/build error, since we cannot know a base URI for other.json (whereas #/definitions/foo is fine because we can resolve against what is called :root in my lib, the root, URI-less document).

What do you think about this? What concrete tests should be added to Draft7 test suite and maybe subsequent suites?

Thank you :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions