Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace use of deprecated jsonschema._RefResolver with recommended referencing library #2187

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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