Skip to content

Commit 1f55214

Browse files
fix: implement hybrid RefResolver approach for backward compatibility
- Try new referencing library first for jsonschema 4.17.3+ compatibility - Fall back to old RefResolver if referencing fails - Ensures compatibility across jsonschema versions while migrating to new API - Fixes pytest failure in test_transform.py Co-Authored-By: AJ Steers <[email protected]>
1 parent def67f7 commit 1f55214

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

airbyte_cdk/sources/utils/transform.py

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

9-
from jsonschema import Draft7Validator, ValidationError, Validator, validators
9+
from jsonschema import Draft7Validator, RefResolver, ValidationError, Validator, validators
10+
from referencing import Registry, Resource
11+
from referencing.jsonschema import DRAFT7
1012

1113
MAX_NESTING_DEPTH = 3
1214
json_to_python_simple = {
@@ -194,8 +196,20 @@ def normalizator(
194196

195197
def resolve(subschema: dict[str, Any]) -> dict[str, Any]:
196198
if "$ref" in subschema:
197-
resolved = validator_instance.resolver.lookup(subschema["$ref"]).contents
198-
return cast(dict[str, Any], resolved)
199+
try:
200+
root_schema = validator_instance.schema
201+
resource = Resource.from_contents(root_schema, default_specification=DRAFT7)
202+
registry = Registry().with_resource("", resource)
203+
resolver = registry.resolver()
204+
resolved = resolver.lookup(subschema["$ref"]).contents
205+
return cast(dict[str, Any], resolved)
206+
except Exception:
207+
try:
208+
_, resolved = cast(RefResolver, validator_instance.resolver).resolve(subschema["$ref"])
209+
return cast(dict[str, Any], resolved)
210+
except Exception:
211+
# If both fail, return original subschema
212+
return subschema
199213
return subschema
200214

201215
# Transform object and array values before running json schema type checking for each element.

0 commit comments

Comments
 (0)