33#
44
55import logging
6+ from copy import deepcopy
67from enum import Flag , auto
7- from typing import Any , Callable , Dict , Generator , Mapping , Optional , cast
8+ from typing import TYPE_CHECKING , Any , Callable , Dict , Generator , Mapping , Optional , cast
89
9- from jsonschema import Draft7Validator , ValidationError , Validator , validators
10+ from jsonschema import Draft7Validator , ValidationError , validators
11+ from jsonschema ._typing import SchemaKeywordValidator
1012from referencing import Registry , Resource
11- from referencing ._core import Resolver # used for type hints
13+ from referencing ._core import Resolver
14+ from referencing .exceptions import Unresolvable
1215from referencing .jsonschema import DRAFT7
1316
17+ from airbyte_cdk .sources .utils .schema_helpers import expand_refs
18+
19+ from .schema_helpers import get_ref_resolver_registry
20+
21+ try :
22+ from jsonschema .validators import Validator
23+ except :
24+ from jsonschema import Validator
25+
26+
1427MAX_NESTING_DEPTH = 3
1528json_to_python_simple = {
1629 "string" : str ,
@@ -194,20 +207,22 @@ def normalizator(
194207 validators parameter for detailed description.
195208 :
196209 """
210+ # Very first step is to expand references in the schema itself
211+ expand_refs (schema )
212+ if isinstance (property_value , dict ):
213+ expand_refs (property_value )
214+ # resolver_registry: Registry = get_ref_resolver_registry(schema)
215+ # ref_resolver: Resolver = resolver_registry.resolver()
197216
198217 def resolve (subschema : dict [str , Any ]) -> dict [str , Any ]:
199- if "$ref" in subschema :
200- ref_url = subschema ["$ref" ]
201- try :
202- if hasattr (validator_instance .resolver , 'lookup' ):
203- resolved = validator_instance .resolver .lookup (ref_url ).contents
204- return cast (dict [str , Any ], resolved )
205- elif hasattr (validator_instance .resolver , 'resolve' ):
206- _ , resolved = validator_instance .resolver .resolve (ref_url )
207- return cast (dict [str , Any ], resolved )
208- except Exception :
209- pass
210- return subschema
218+ # if "$ref" in subschema:
219+ # try:
220+ # resolved = ref_resolver.lookup(subschema["$ref"]).contents
221+ # except Unresolvable as e:
222+ # raise ValidationError(
223+ # f"Failed to resolve $ref '{subschema['$ref']}' from {ref_resolver!r} and schema {schema!r}: {e}"
224+ # ) from e
225+ # return cast(dict[str, Any], resolved)
211226 return subschema
212227
213228 # Transform object and array values before running json schema type checking for each element.
@@ -216,14 +231,14 @@ def resolve(subschema: dict[str, Any]) -> dict[str, Any]:
216231 if schema_key == "properties" and isinstance (instance , dict ):
217232 for k , subschema in property_value .items ():
218233 if k in instance :
219- subschema = resolve (subschema )
234+ # subschema = resolve(subschema)
220235 instance [k ] = self .__normalize (instance [k ], subschema )
221236 # Recursively normalize every item of the "instance" sub-array,
222237 # if "instance" is an incorrect type - skip recursive normalization of "instance"
223238 elif schema_key == "items" and isinstance (instance , list ):
224- subschema = resolve (property_value )
239+ # subschema = resolve(property_value)
225240 for index , item in enumerate (instance ):
226- instance [index ] = self .__normalize (item , subschema )
241+ instance [index ] = self .__normalize (item , property_value )
227242
228243 # Running native jsonschema traverse algorithm after field normalization is done.
229244 yield from original_validator (
0 commit comments