fix: fixes save object after parsing

This commit is contained in:
2025-11-24 01:48:48 -03:00
parent 4de711075e
commit 328eb66034
2 changed files with 18 additions and 4 deletions

View File

@@ -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

View File

@@ -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: