python-jsonschema / python-jsonschema/jsonschema

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

Open
#1,547 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5k
Forks
671
Avg merge
1d 1h
Merged PRs (30d)
10

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Validator._resolver and reference_registry.resolver(base_uri=...), then review how Resource.id() and the referencing interfaces handle schemas without "$id". Compare the behavior described in check-jsonschema#640 and identify the public API boundaries that would need coverage. Done means an explicit base URI can be set without mutating input schemas or relying on private attributes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.