Skip to content

Add support for standard "$ref" behaviour #1671

@UnasZole

Description

@UnasZole

Problem statement

jsonschema2pojo currently supports the "$ref" keyword, but in a way that is specific to jsonschema2pojo and incompatible with all other tools I use to work with the schemas.

The behaviour in jsonschema2pojo is simple and well explained in your documentation : https://github.com/joelittlejohn/jsonschema2pojo/wiki/Reference#ref
In short, you expect the $ref to contain a URL (possibly a relative path) with which you can retrieve the referenced schema.

However, the JSON Schema specifications defines a different behaviour :
https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#name-dereferencing
https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#name-identifying-the-root-schema

Basically :

  • All schemas documents are expected (SHOULD) have an "$id" on the root element, that is considered the Base URI of the document.
  • "$ref" URIs are resolved against the current Base URI (so relative paths are computed against the "$id" of the current schema, not against the path of the current file)
  • "$ref" URIs are used only as identifiers, not as URLs. It's up to the tool processing the schema to have its own "directory" of schemas to know where to find the schema with the requested "$id".

This causes a problem in particular when wanting to reference another schema via a relative path :

  • jsonschema2pojo resolves the relative path against the URI of the document.
  • the spec, and most tools following it, resolve the relative path against the "$id" of the document.

Technically, the spec does mention that if a document has no ID, it's base URI will be the URI used to retrieve the document itself - so for documents which do not specify an "$id", jsonschema2pojo's behaviour mostly falls back in line... But since this is explicitly discouraged by the spec, other tools tend not to support it well.

Possible solution

jsonschema2pojo should build internally a small index of all the schemas that are part of the build.
When processing a "$ref" keyword, it should first resolve the ref URI against the current "$id" and check if it matches the "$id" of any other schema within the index.
And only if it does not find any, then it should fall back to the current behaviour : interpreting the ref URI as a URL and resolve it against the current document's URL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions