Fixes Array So No DefaultFactory is Created When no Default is Set and Field is Required
This commit is contained in:
@@ -35,7 +35,7 @@ class ArrayTypeParser(GenericTypeParser):
|
|||||||
|
|
||||||
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
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(
|
mapped_properties["default_factory"] = self._build_default_factory(
|
||||||
properties.get("default"), wrapper_type
|
properties.get("default"), wrapper_type
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class ObjectTypeParser(GenericTypeParser):
|
|||||||
|
|
||||||
fields = {}
|
fields = {}
|
||||||
for name, prop in properties.items():
|
for name, prop in properties.items():
|
||||||
sub_property = kwargs.copy()
|
sub_property: TypeParserOptions = kwargs.copy()
|
||||||
sub_property["required"] = name in required_keys
|
sub_property["required"] = name in required_keys
|
||||||
|
|
||||||
parsed_type, parsed_properties = GenericTypeParser.type_from_properties(
|
parsed_type, parsed_properties = GenericTypeParser.type_from_properties(
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ class TestSchemaConverter(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(model(is_active="true").is_active, True)
|
self.assertEqual(model(is_active="true").is_active, True)
|
||||||
|
|
||||||
def test_validation_list(self):
|
def test_validation_list_with_valid_items(self):
|
||||||
schema = {
|
schema = {
|
||||||
"title": "Person",
|
"title": "Person",
|
||||||
"description": "A person",
|
"description": "A person",
|
||||||
@@ -204,15 +204,52 @@ class TestSchemaConverter(TestCase):
|
|||||||
model(friends=["John", "Jane", "John"]).friends, {"John", "Jane"}
|
model(friends=["John", "Jane", "John"]).friends, {"John", "Jane"}
|
||||||
)
|
)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
model()
|
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
model(friends=[])
|
model(friends=[])
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
model(friends=["John", "Jane", "Invalid"])
|
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):
|
def test_validation_object(self):
|
||||||
schema = {
|
schema = {
|
||||||
"title": "Person",
|
"title": "Person",
|
||||||
@@ -238,6 +275,9 @@ class TestSchemaConverter(TestCase):
|
|||||||
self.assertEqual(obj.address.street, "123 Main St")
|
self.assertEqual(obj.address.street, "123 Main St")
|
||||||
self.assertEqual(obj.address.city, "Springfield")
|
self.assertEqual(obj.address.city, "Springfield")
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
model()
|
||||||
|
|
||||||
def test_default_for_string(self):
|
def test_default_for_string(self):
|
||||||
schema = {
|
schema = {
|
||||||
"title": "Person",
|
"title": "Person",
|
||||||
|
|||||||
Reference in New Issue
Block a user