feature: add instance level ref cache #63
@@ -46,9 +46,10 @@ class ObjectTypeParser(GenericTypeParser):
|
|||||||
type_parsing.model_validate(example) for example in example_values
|
type_parsing.model_validate(example) for example in example_values
|
||||||
]
|
]
|
||||||
|
|
||||||
if name in ref_cache:
|
if name in ref_cache and isinstance(ref_cache[name], type):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
f"Type '{name}' is already in the ref_cache and will be overwritten.",
|
f"Type '{name}' is already in the ref_cache and will be overwritten."
|
||||||
|
" This may indicate a circular reference in the schema or a collision in the schema.",
|
||||||
UserWarning,
|
UserWarning,
|
||||||
)
|
)
|
||||||
ref_cache[name] = type_parsing
|
ref_cache[name] = type_parsing
|
||||||
|
|||||||
@@ -26,14 +26,28 @@ class SchemaConverter:
|
|||||||
self,
|
self,
|
||||||
schema: JSONSchema,
|
schema: JSONSchema,
|
||||||
ref_cache: Optional[RefCacheDict] = None,
|
ref_cache: Optional[RefCacheDict] = None,
|
||||||
|
without_cache: bool = False,
|
||||||
) -> type[BaseModel]:
|
) -> type[BaseModel]:
|
||||||
"""
|
"""
|
||||||
Converts a JSON Schema to a Pydantic model.
|
Converts a JSON Schema to a Pydantic model.
|
||||||
|
This is the instance method version of `build` and uses the instance's reference cache if none is provided.
|
||||||
|
Use this method if you want to utilize the instance's reference cache.
|
||||||
|
|
||||||
:param schema: The JSON Schema to convert.
|
:param schema: The JSON Schema to convert.
|
||||||
:param ref_cache: An optional reference cache to use during conversion.
|
:param ref_cache: An optional reference cache to use during conversion.
|
||||||
|
:param without_cache: Whether to use a clean reference cache for this conversion.
|
||||||
:return: The generated Pydantic model.
|
:return: The generated Pydantic model.
|
||||||
"""
|
"""
|
||||||
return self.build(schema, ref_cache or self._ref_cache)
|
local_ref_cache: RefCacheDict
|
||||||
|
|
||||||
|
if without_cache:
|
||||||
|
local_ref_cache = dict()
|
||||||
|
elif ref_cache is None:
|
||||||
|
local_ref_cache = self._ref_cache
|
||||||
|
else:
|
||||||
|
local_ref_cache = ref_cache
|
||||||
|
|
||||||
|
return self.build(schema, local_ref_cache)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def build(
|
def build(
|
||||||
@@ -43,7 +57,6 @@ class SchemaConverter:
|
|||||||
Converts a JSON Schema to a Pydantic model.
|
Converts a JSON Schema to a Pydantic model.
|
||||||
:param schema: The JSON Schema to convert.
|
:param schema: The JSON Schema to convert.
|
||||||
:param ref_cache: An optional reference cache to use during conversion, if provided `with_clean_cache` will be ignored.
|
:param ref_cache: An optional reference cache to use during conversion, if provided `with_clean_cache` will be ignored.
|
||||||
:param with_clean_cache: Whether to use a clean reference cache for this conversion. Set to rue due to API compatibility. Will be set to False in future versions.
|
|
||||||
:return: The generated Pydantic model.
|
:return: The generated Pydantic model.
|
||||||
"""
|
"""
|
||||||
if ref_cache is None:
|
if ref_cache is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user