Skip to content

Commit aa5f6a4

Browse files
authored
clean up validator implementation
1 parent 96eabbf commit aa5f6a4

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

airbyte_cdk/sources/utils/schema_helpers.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from jsonschema import validate
1414
from jsonschema.exceptions import ValidationError
1515
from pydantic.v1 import BaseModel, Field
16-
from referencing import Registry, Resource
16+
from referencing import Registry, Resource, Resolver
1717
from referencing.jsonschema import DRAFT7
1818

1919
from airbyte_cdk.models import ConnectorSpecification, FailureType
@@ -65,21 +65,27 @@ def resolve_ref_links(obj: Any) -> Any:
6565
return obj
6666

6767

68-
def _expand_refs(schema: Any, ref_resolver: Optional[Registry] = None) -> None:
68+
def _expand_refs(schema: Any, ref_resolver: Optional[Resolver] = None) -> None:
6969
"""Internal function to iterate over schema and replace all occurrences of $ref with their definitions. Recursive.
7070
7171
:param schema: schema that will be patched
7272
:param ref_resolver: resolver to get definition from $ref, if None pass it will be instantiated
7373
"""
7474
if ref_resolver is None:
75-
resource = Resource.from_contents(schema, default_specification=DRAFT7)
76-
ref_resolver = Registry().with_resource("", resource)
77-
resolver = ref_resolver.resolver()
75+
resource = Resource.from_contents(
76+
contents=schema,
77+
default_specification=DRAFT7,
78+
)
79+
resolver_registry = Registry().with_resource(
80+
uri="",
81+
resource=resource,
82+
)
83+
ref_resolver = resolver_registry.resolver()
7884

7985
if isinstance(schema, MutableMapping):
8086
if "$ref" in schema:
8187
ref_url = schema.pop("$ref")
82-
definition = resolver.lookup(ref_url).contents
88+
definition = ref_resolver.lookup(ref_url).contents
8389
_expand_refs(
8490
definition, ref_resolver=ref_resolver
8591
) # expand refs in definitions as well

airbyte_cdk/sources/utils/transform.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +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
10-
from referencing import Registry, Resource
9+
from jsonschema import Draft7Validator, ValidationError, Validator, validators
10+
from referencing import Registry, Resource, Resolver
1111
from referencing.jsonschema import DRAFT7
1212

1313
MAX_NESTING_DEPTH = 3
@@ -195,33 +195,15 @@ def normalizator(
195195
"""
196196

197197
def resolve(subschema: dict[str, Any]) -> dict[str, Any]:
198-
if "$ref" in subschema:
199-
ref_url = subschema["$ref"]
200-
201-
try:
202-
root_schema = validator_instance.schema
203-
resource = Resource.from_contents(root_schema, default_specification=DRAFT7)
204-
registry = Registry().with_resource("", resource)
205-
resolver = registry.resolver()
206-
resolved = resolver.lookup(ref_url).contents
207-
return cast(dict[str, Any], resolved)
208-
except Exception:
209-
try:
210-
if (
211-
hasattr(validator_instance, "resolver")
212-
and validator_instance.resolver is not None
213-
):
214-
_, resolved = cast(
215-
RefResolver, validator_instance.resolver
216-
).resolve(ref_url)
217-
return cast(dict[str, Any], resolved)
218-
except Exception:
219-
# If both fail, we'll return original subschema, below.
220-
# If both fail, we'll return original subschema, below.
221-
pass
222-
198+
if "$ref" not in subschema:
199+
# Nothing to resolve
223200
return subschema
224-
return subschema
201+
202+
# Else, we need to resolve "$ref":
203+
ref_url = subschema["$ref"]
204+
resolver: Resolver = validator_instance.resolver
205+
resolved_contents = resolver.lookup(ref_url).contents
206+
return cast(dict[str, Any], resolved_contents)
225207

226208
# Transform object and array values before running json schema type checking for each element.
227209
# Recursively normalize every value of the "instance" sub-object,

0 commit comments

Comments
 (0)