From 8b1520741b32237e7aaaad5f9f3aa2be93e67898 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sun, 23 Nov 2025 14:31:48 -0300 Subject: [PATCH] feat: fixes and validates that objects have parsed examples --- jambo/parser/object_type_parser.py | 11 +++++-- tests/parser/test_object_type_parser.py | 38 +++++++++++++++++++++++++ tests/parser/test_string_type_parser.py | 22 +++++++------- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/jambo/parser/object_type_parser.py b/jambo/parser/object_type_parser.py index 6cb60e7..516ffc5 100644 --- a/jambo/parser/object_type_parser.py +++ b/jambo/parser/object_type_parser.py @@ -21,13 +21,18 @@ class ObjectTypeParser(GenericTypeParser): properties.get("required", []), **kwargs, ) - type_properties = {} + type_properties = self.mappings_properties_builder(properties, **kwargs) - if "default" in properties: + if (default_value := type_properties.get("default")) is not None: type_properties["default_factory"] = lambda: type_parsing.model_validate( - properties["default"] + default_value ) + if (example_values := type_properties.get("examples")) is not None: + type_properties["examples"] = [ + type_parsing.model_validate(example) for example in example_values + ] + return type_parsing, type_properties @classmethod diff --git a/tests/parser/test_object_type_parser.py b/tests/parser/test_object_type_parser.py index 4adbc52..ed50167 100644 --- a/tests/parser/test_object_type_parser.py +++ b/tests/parser/test_object_type_parser.py @@ -22,6 +22,44 @@ class TestObjectTypeParser(TestCase): self.assertEqual(obj.name, "name") self.assertEqual(obj.age, 10) + def test_object_type_parser_with_object_example(self): + parser = ObjectTypeParser() + + properties = { + "type": "object", + "properties": { + "name": {"type": "string"}, + "age": {"type": "integer"}, + "address": { + "type": "object", + "properties": { + "street": {"type": "string"}, + "city": {"type": "string"}, + }, + "examples": [ + { + "street": "123 Main St", + "city": "Anytown", + } + ], + }, + }, + } + + parsed_type, type_validator = parser.from_properties_impl( + "placeholder", properties + ) + + model_schema = parsed_type.model_json_schema() + + # # Check example value + address_schema = model_schema["properties"]["address"] + self.assertIn("examples", address_schema) + + example_address = address_schema["examples"][0] + self.assertEqual(example_address["street"], "123 Main St") + self.assertEqual(example_address["city"], "Anytown") + def test_object_type_parser_with_default(self): parser = ObjectTypeParser() diff --git a/tests/parser/test_string_type_parser.py b/tests/parser/test_string_type_parser.py index 0971588..b00b487 100644 --- a/tests/parser/test_string_type_parser.py +++ b/tests/parser/test_string_type_parser.py @@ -288,6 +288,17 @@ class TestStringTypeParser(TestCase): ], ) + def test_string_parser_with_invalid_example_value(self): + with self.assertRaises(InvalidSchemaException): + StringTypeParser().from_properties( + "placeholder", + { + "type": "string", + "format": "email", + "examples": ["invalid-email"], + }, + ) + @unittest.skip("Duration parsing not yet implemented") def test_string_parser_with_timedelta_format(self): parser = StringTypeParser() @@ -310,14 +321,3 @@ class TestStringTypeParser(TestCase): timedelta(seconds=0.5), ], ) - - def test_string_parser_with_invalid_example_value(self): - with self.assertRaises(InvalidSchemaException): - StringTypeParser().from_properties( - "placeholder", - { - "type": "string", - "format": "email", - "examples": ["invalid-email"], - }, - )