From ffbd124cf91034e6fa3cafb2fee5883423517df4 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sun, 23 Nov 2025 18:59:47 -0300 Subject: [PATCH] feat: adds example to allOf --- jambo/parser/allof_type_parser.py | 3 ++ tests/parser/test_allof_type_parser.py | 47 ++++++++++++++++++++++++++ tests/parser/test_anyof_type_parser.py | 17 ++++++++++ 3 files changed, 67 insertions(+) diff --git a/jambo/parser/allof_type_parser.py b/jambo/parser/allof_type_parser.py index 92c5ff2..2c0af9f 100644 --- a/jambo/parser/allof_type_parser.py +++ b/jambo/parser/allof_type_parser.py @@ -27,6 +27,9 @@ class AllOfTypeParser(GenericTypeParser): sub_properties ) + if (examples := properties.get("examples")) is not None: + combined_properties["examples"] = examples + return parser().from_properties_impl(name, combined_properties, **kwargs) @staticmethod diff --git a/tests/parser/test_allof_type_parser.py b/tests/parser/test_allof_type_parser.py index f99c657..a957161 100644 --- a/tests/parser/test_allof_type_parser.py +++ b/tests/parser/test_allof_type_parser.py @@ -309,3 +309,50 @@ class TestAllOfTypeParser(TestCase): with self.assertRaises(InvalidSchemaException): AllOfTypeParser().from_properties("placeholder", properties) + + def test_all_of_with_root_examples(self): + """ + Tests the AllOfTypeParser with examples. + """ + + properties = { + "type": "object", + "allOf": [ + { + "properties": { + "name": { + "type": "string", + "minLength": 1, + } + }, + }, + { + "properties": { + "name": { + "type": "string", + "maxLength": 4, + } + }, + }, + ], + "examples": [ + {"name": "John"}, + {"name": "Jane"}, + {"name": "Doe"}, + {"name": "Jack"}, + ], + } + + type_parsed, type_properties = AllOfTypeParser().from_properties( + "placeholder", properties + ) + + self.assertEqual( + type_properties["examples"], + [ + type_parsed(name="John"), + type_parsed(name="Jane"), + type_parsed(name="Doe"), + type_parsed(name="Jack"), + ], + ) diff --git a/tests/parser/test_anyof_type_parser.py b/tests/parser/test_anyof_type_parser.py index d7740fd..811bc1b 100644 --- a/tests/parser/test_anyof_type_parser.py +++ b/tests/parser/test_anyof_type_parser.py @@ -98,3 +98,20 @@ class TestAnyOfTypeParser(TestCase): with self.assertRaises(InvalidSchemaException): AnyOfTypeParser().from_properties("placeholder", properties) + + def test_any_of_with_examples(self): + """ + Tests the AnyOfTypeParser with a string or int type and examples. + """ + + properties = { + "anyOf": [ + {"type": "string"}, + {"type": "integer"}, + ], + "examples": ["100", 100], + } + + _, type_validator = AnyOfTypeParser().from_properties("placeholder", properties) + + self.assertEqual(type_validator["examples"], ["100", 100])