From 617f1aab2b43e714d62e8a21565e46f6b458b6f9 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Mon, 18 Aug 2025 22:27:49 -0300 Subject: [PATCH 1/2] Adds Failing Test Case to Test --- tests/test_schema_converter.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_schema_converter.py b/tests/test_schema_converter.py index fbba3c9..96adf34 100644 --- a/tests/test_schema_converter.py +++ b/tests/test_schema_converter.py @@ -204,6 +204,9 @@ class TestSchemaConverter(TestCase): model(friends=["John", "Jane", "John"]).friends, {"John", "Jane"} ) + with self.assertRaises(ValueError): + model() + with self.assertRaises(ValueError): model(friends=[]) -- 2.49.1 From 7b9464f458633bf81299119f99a914ae6a562f1e Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Mon, 18 Aug 2025 22:53:28 -0300 Subject: [PATCH 2/2] Fixes Array So No DefaultFactory is Created When no Default is Set and Field is Required --- jambo/parser/array_type_parser.py | 2 +- jambo/parser/object_type_parser.py | 2 +- tests/test_schema_converter.py | 48 +++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/jambo/parser/array_type_parser.py b/jambo/parser/array_type_parser.py index c92de3d..7d59ea5 100644 --- a/jambo/parser/array_type_parser.py +++ b/jambo/parser/array_type_parser.py @@ -35,7 +35,7 @@ class ArrayTypeParser(GenericTypeParser): mapped_properties = self.mappings_properties_builder(properties, **kwargs) - if "default" not in mapped_properties: + if "default" in properties or not kwargs.get("required", False): mapped_properties["default_factory"] = self._build_default_factory( properties.get("default"), wrapper_type ) diff --git a/jambo/parser/object_type_parser.py b/jambo/parser/object_type_parser.py index 8deb5ac..0f0ab7e 100644 --- a/jambo/parser/object_type_parser.py +++ b/jambo/parser/object_type_parser.py @@ -59,7 +59,7 @@ class ObjectTypeParser(GenericTypeParser): fields = {} for name, prop in properties.items(): - sub_property = kwargs.copy() + sub_property: TypeParserOptions = kwargs.copy() sub_property["required"] = name in required_keys parsed_type, parsed_properties = GenericTypeParser.type_from_properties( diff --git a/tests/test_schema_converter.py b/tests/test_schema_converter.py index 96adf34..472ea8d 100644 --- a/tests/test_schema_converter.py +++ b/tests/test_schema_converter.py @@ -181,7 +181,7 @@ class TestSchemaConverter(TestCase): self.assertEqual(model(is_active="true").is_active, True) - def test_validation_list(self): + def test_validation_list_with_valid_items(self): schema = { "title": "Person", "description": "A person", @@ -204,15 +204,52 @@ class TestSchemaConverter(TestCase): model(friends=["John", "Jane", "John"]).friends, {"John", "Jane"} ) - with self.assertRaises(ValueError): - model() - with self.assertRaises(ValueError): model(friends=[]) with self.assertRaises(ValueError): model(friends=["John", "Jane", "Invalid"]) + def test_validation_list_with_missing_items(self): + model = SchemaConverter.build( + { + "title": "Person", + "description": "A person", + "type": "object", + "properties": { + "friends": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1, + "maxItems": 2, + "default": ["John", "Jane"], + }, + }, + } + ) + + self.assertEqual(model().friends, ["John", "Jane"]) + + model = SchemaConverter.build( + { + "title": "Person", + "description": "A person", + "type": "object", + "properties": { + "friends": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1, + "maxItems": 2, + }, + }, + "required": ["friends"], + } + ) + + with self.assertRaises(ValueError): + model() + def test_validation_object(self): schema = { "title": "Person", @@ -238,6 +275,9 @@ class TestSchemaConverter(TestCase): self.assertEqual(obj.address.street, "123 Main St") self.assertEqual(obj.address.city, "Springfield") + with self.assertRaises(ValueError): + model() + def test_default_for_string(self): schema = { "title": "Person", -- 2.49.1