Add null type parser #32
@@ -5,21 +5,14 @@ from typing_extensions import Unpack
|
|||||||
|
|
||||||
|
|
||||||
class NullTypeParser(GenericTypeParser):
|
class NullTypeParser(GenericTypeParser):
|
||||||
mapped_type = None
|
mapped_type = type(None)
|
||||||
|
|
||||||
json_schema_type = "type:null"
|
json_schema_type = "type:null"
|
||||||
|
|
||||||
type_mappings = {
|
|
||||||
"default": "default",
|
|
||||||
}
|
|
||||||
|
|
||||||
def from_properties_impl(
|
def from_properties_impl(
|
||||||
self, name, properties, **kwargs: Unpack[TypeParserOptions]
|
self, name, properties, **kwargs: Unpack[TypeParserOptions]
|
||||||
):
|
):
|
||||||
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
||||||
|
mapped_properties["default"] = None
|
||||||
|
|
||||||
default_value = properties.get("default")
|
return self.mapped_type, mapped_properties
|
||||||
if default_value is not None:
|
|
||||||
raise ValueError(f"Default value for {name} must be None.")
|
|
||||||
|
|
||||||
return None, mapped_properties
|
|
||||||
|
|||||||
@@ -13,31 +13,17 @@ class TestNullTypeParser(TestCase):
|
|||||||
"placeholder", properties
|
"placeholder", properties
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(type_parsing, None)
|
self.assertEqual(type_parsing, type(None))
|
||||||
self.assertEqual(type_validator, {"default": None})
|
self.assertEqual(type_validator, {"default": None})
|
||||||
|
|
||||||
def test_null_parser_with_default(self):
|
def test_null_parser_with_invalid_default(self):
|
||||||
parser = NullTypeParser()
|
parser = NullTypeParser()
|
||||||
|
|
||||||
properties = {
|
properties = {"type": "null", "default": "invalid"}
|
||||||
"type": "null",
|
|
||||||
"default": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
type_parsing, type_validator = parser.from_properties_impl(
|
type_parsing, type_validator = parser.from_properties_impl(
|
||||||
"placeholder", properties
|
"placeholder", properties
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(type_parsing, None)
|
self.assertEqual(type_parsing, type(None))
|
||||||
self.assertEqual(type_validator["default"], None)
|
self.assertEqual(type_validator, {"default": None})
|
||||||
|
|
||||||
def test_null_parser_with_invalid_default(self):
|
|
||||||
parser = NullTypeParser()
|
|
||||||
|
|
||||||
properties = {
|
|
||||||
"type": "null",
|
|
||||||
"default": "invalid",
|
|
||||||
}
|
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
parser.from_properties_impl("placeholder", properties)
|
|
||||||
|
|||||||
@@ -657,3 +657,23 @@ class TestSchemaConverter(TestCase):
|
|||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
Model(name="Canada")
|
Model(name="Canada")
|
||||||
|
|
||||||
|
def test_null_type_parser(self):
|
||||||
|
schema = {
|
||||||
|
"title": "Test",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"a_thing": {"type": "null"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
Model = SchemaConverter.build(schema)
|
||||||
|
|
||||||
|
obj = Model()
|
||||||
|
self.assertIsNone(obj.a_thing)
|
||||||
|
|
||||||
|
obj = Model(a_thing=None)
|
||||||
|
self.assertIsNone(obj.a_thing)
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
Model(a_thing="not none")
|
||||||
|
|||||||
Reference in New Issue
Block a user