feat: saves object after parsing

This commit is contained in:
2025-11-24 01:29:21 -03:00
parent 10bad254d7
commit abc8bc2e40
3 changed files with 41 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InternalAssertionException
from jambo.parser._type_parser import GenericTypeParser
from jambo.types.json_schema_type import JSONSchema
from jambo.types.type_parser_options import TypeParserOptions
@@ -6,6 +7,8 @@ from pydantic import BaseModel, ConfigDict, Field, create_model
from pydantic.fields import FieldInfo
from typing_extensions import Unpack
import warnings
class ObjectTypeParser(GenericTypeParser):
mapped_type = object
@@ -15,6 +18,12 @@ class ObjectTypeParser(GenericTypeParser):
def from_properties_impl(
self, name: str, properties: JSONSchema, **kwargs: Unpack[TypeParserOptions]
) -> tuple[type[BaseModel], dict]:
ref_cache = kwargs.get("ref_cache")
if ref_cache is None:
raise InternalAssertionException(
"`ref_cache` must be provided in kwargs for RefTypeParser"
)
type_parsing = self.to_model(
name,
properties.get("properties", {}),
@@ -37,6 +46,13 @@ class ObjectTypeParser(GenericTypeParser):
type_parsing.model_validate(example) for example in example_values
]
if name in ref_cache:
warnings.warn(
f"Type '{name}' is already in the ref_cache and will be overwritten.",
UserWarning,
)
ref_cache[name] = type_parsing
return type_parsing, type_properties
@classmethod