Skip to content

Feature request: allow a reference registry to have a fixed base_uri when the schema does not set "$id" #1547

Description

@sirosen

I'm working on fixing python-jsonschema/check-jsonschema#640 , which is a bit of weirdness to do with local schemas and multiple relative $ref values.

Right now, check-jsonschema is doing some path joining logic to combine the file URI for the input schema with the $ref paths it sees, and it ends up playing out incorrectly when combined with referencing's inner resolver object. Second order ref retrieval does some path joining, which then joins improperly with the explicit base URI.

I found that the issue can be resolved by replacing the Validator._resolver with one that has a non-"" base URL:

        if validator._resolver._base_uri == "" and retrieval_uri is not None:
            validator._resolver = reference_registry.resolver(base_uri=retrieval_uri)

(where retrieval_uri is, in this case, the local file URI)

I don't see an obvious public-API way of doing this. The above is a workaround, and I'll probably do a version of it with some safeguard around potential jsonschema and referencing changes. I rejected my other options as being worse workarounds:

  • modifying the input schema to set "$id" -- although this has the right effect, it strikes me as incorrect (I'm not able to substantiate that with some case in which there's a bad outcome, but it's silently tweaking the user's data to make things work; feels weird IMO)
  • subclassing Resource from referencing to override id() ... based on jsonschema's interfaces, I strongly suspect that this is not intended usage

What would work best is to have some public API way of explicitly setting the base URI for resolution. It probably needs support in referencing, but it also potentially needs to be exposed in jsonschema.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions