Skip to content

Commit 8c84b6d

Browse files
fix: migrate deprecated RefResolver to referencing library for jsonschema 4.18+ compatibility
- Replace RefResolver.from_schema() with Registry().with_resource() pattern - Update resolver.resolve() calls to use resolver.lookup().contents - Addresses jsonschema 4.18+ deprecation warnings - Maintains backward compatibility with existing functionality Co-Authored-By: AJ Steers <[email protected]>
1 parent 987f454 commit 8c84b6d

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

airbyte_cdk/sources/utils/schema_helpers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
from typing import Any, ClassVar, Dict, List, Mapping, MutableMapping, Optional, Tuple
1111

1212
import jsonref
13-
from jsonschema import RefResolver, validate
13+
from jsonschema import validate
1414
from jsonschema.exceptions import ValidationError
1515
from pydantic.v1 import BaseModel, Field
16+
from referencing import Registry, Resource
1617

1718
from airbyte_cdk.models import ConnectorSpecification, FailureType
1819
from airbyte_cdk.utils.traced_exception import AirbyteTracedException
@@ -63,18 +64,21 @@ def resolve_ref_links(obj: Any) -> Any:
6364
return obj
6465

6566

66-
def _expand_refs(schema: Any, ref_resolver: Optional[RefResolver] = None) -> None:
67+
def _expand_refs(schema: Any, ref_resolver: Optional[Registry] = None) -> None:
6768
"""Internal function to iterate over schema and replace all occurrences of $ref with their definitions. Recursive.
6869
6970
:param schema: schema that will be patched
7071
:param ref_resolver: resolver to get definition from $ref, if None pass it will be instantiated
7172
"""
72-
ref_resolver = ref_resolver or RefResolver.from_schema(schema)
73+
if ref_resolver is None:
74+
resource = Resource.from_contents(schema)
75+
ref_resolver = Registry().with_resource("", resource)
76+
resolver = ref_resolver.resolver()
7377

7478
if isinstance(schema, MutableMapping):
7579
if "$ref" in schema:
7680
ref_url = schema.pop("$ref")
77-
_, definition = ref_resolver.resolve(ref_url)
81+
definition = resolver.lookup(ref_url).contents
7882
_expand_refs(
7983
definition, ref_resolver=ref_resolver
8084
) # expand refs in definitions as well

airbyte_cdk/sources/utils/transform.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from enum import Flag, auto
77
from typing import Any, Callable, Dict, Generator, Mapping, Optional, cast
88

9-
from jsonschema import Draft7Validator, RefResolver, ValidationError, Validator, validators
9+
from jsonschema import Draft7Validator, ValidationError, Validator, validators
10+
from referencing import Registry, Resource
1011

1112
MAX_NESTING_DEPTH = 3
1213
json_to_python_simple = {
@@ -194,10 +195,7 @@ def normalizator(
194195

195196
def resolve(subschema: dict[str, Any]) -> dict[str, Any]:
196197
if "$ref" in subschema:
197-
_, resolved = cast(
198-
RefResolver,
199-
validator_instance.resolver,
200-
).resolve(subschema["$ref"])
198+
resolved = validator_instance.resolver.lookup(subschema["$ref"]).contents
201199
return cast(dict[str, Any], resolved)
202200
return subschema
203201

airbyte_cdk/utils/spec_schema_transformations.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
import re
77
from typing import Any
88

9-
from jsonschema import RefResolver
9+
from referencing import Registry, Resource
1010

1111

1212
def resolve_refs(schema: dict[str, Any]) -> dict[str, Any]:
1313
"""
1414
For spec schemas generated using Pydantic models, the resulting JSON schema can contain refs between object
1515
relationships.
1616
"""
17-
json_schema_ref_resolver = RefResolver.from_schema(schema)
17+
resource = Resource.from_contents(schema)
18+
registry = Registry().with_resource("", resource)
19+
resolver = registry.resolver()
1820
str_schema = json.dumps(schema)
1921
for ref_block in re.findall(r'{"\$ref": "#\/definitions\/.+?(?="})"}', str_schema):
2022
ref = json.loads(ref_block)["$ref"]
21-
str_schema = str_schema.replace(
22-
ref_block, json.dumps(json_schema_ref_resolver.resolve(ref)[1])
23-
)
23+
resolved = resolver.lookup(ref).contents
24+
str_schema = str_schema.replace(ref_block, json.dumps(resolved))
2425
pyschema: dict[str, Any] = json.loads(str_schema)
2526
del pyschema["definitions"]
2627
return pyschema

0 commit comments

Comments
 (0)