feat: fixes and validates that objects have parsed examples
This commit is contained in:
@@ -21,13 +21,18 @@ class ObjectTypeParser(GenericTypeParser):
|
|||||||
properties.get("required", []),
|
properties.get("required", []),
|
||||||
**kwargs,
|
**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(
|
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
|
return type_parsing, type_properties
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -22,6 +22,44 @@ class TestObjectTypeParser(TestCase):
|
|||||||
self.assertEqual(obj.name, "name")
|
self.assertEqual(obj.name, "name")
|
||||||
self.assertEqual(obj.age, 10)
|
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):
|
def test_object_type_parser_with_default(self):
|
||||||
parser = ObjectTypeParser()
|
parser = ObjectTypeParser()
|
||||||
|
|
||||||
|
|||||||
@@ -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")
|
@unittest.skip("Duration parsing not yet implemented")
|
||||||
def test_string_parser_with_timedelta_format(self):
|
def test_string_parser_with_timedelta_format(self):
|
||||||
parser = StringTypeParser()
|
parser = StringTypeParser()
|
||||||
@@ -310,14 +321,3 @@ class TestStringTypeParser(TestCase):
|
|||||||
timedelta(seconds=0.5),
|
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"],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user