Skip to content

Commit

Permalink
fix: Replace use of deprecated jsonschema._RefResolver with recomme…
Browse files Browse the repository at this point in the history
…nded `referencing` library (#2187)

* refactor: Replace use of deprecated `jsonschema._RefResolver` with recommended `referencing` library

* Fix types

* Continue on error
  • Loading branch information
edgarrmondragon committed Jan 29, 2024
1 parent c4d31ba commit 35a3c54
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
tests:
name: "Test on ${{ matrix.python-version }} (${{ matrix.session }}) / ${{ matrix.os }} / SQLAlchemy: ${{ matrix.sqlalchemy }}"
runs-on: ${{ matrix.os }}
continue-on-error: true
env:
NOXPYTHON: ${{ matrix.python-version }}
NOXSESSION: ${{ matrix.session }}
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ PyJWT = "~=2.4"
python-dateutil = ">=2.8.2"
python-dotenv = ">=0.20"
PyYAML = ">=6.0"
referencing = ">=0.30.0"
requests = ">=2.25.1"
simpleeval = ">=0.9.13"
simplejson = ">=3.17.6"
Expand Down
22 changes: 17 additions & 5 deletions singer_sdk/_singerlib/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import typing as t
from dataclasses import dataclass

from jsonschema import RefResolver
from referencing import Registry
from referencing.jsonschema import DRAFT202012

if t.TYPE_CHECKING:
from referencing._core import Resolver

# These are keys defined in the JSON Schema spec that do not themselves contain
# schemas (or lists of schemas)
Expand Down Expand Up @@ -148,17 +152,25 @@ def resolve_schema_references(
A schema dict with all $refs replaced with the appropriate dict.
"""
refs = refs or {}
return _resolve_schema_references(schema, RefResolver("", schema, store=refs))
registry: Registry = Registry()
schema_resource = DRAFT202012.create_resource(schema)
registry = registry.with_resource("", schema_resource)
registry = registry.with_resources(
[(k, DRAFT202012.create_resource(v)) for k, v in refs.items()]
)

resolver = registry.resolver()
return _resolve_schema_references(schema, resolver)


def _resolve_schema_references(
schema: dict[str, t.Any],
resolver: RefResolver,
resolver: Resolver,
) -> dict[str, t.Any]:
if _SchemaKey.ref in schema:
reference_path = schema.pop(_SchemaKey.ref, None)
resolved = resolver.resolve(reference_path)[1]
schema.update(resolved)
resolved = resolver.lookup(reference_path)
schema.update(resolved.contents)
return _resolve_schema_references(schema, resolver)

if _SchemaKey.properties in schema:
Expand Down

0 comments on commit 35a3c54

Please sign in to comment.