feat: Add examples #54
@@ -21,13 +21,18 @@ class ObjectTypeParser(GenericTypeParser):
|
||||
properties.get("required", []),
|
||||
**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(
|
||||
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
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -22,6 +22,44 @@ class TestObjectTypeParser(TestCase):
|
||||
self.assertEqual(obj.name, "name")
|
||||
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):
|
||||
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")
|
||||
def test_string_parser_with_timedelta_format(self):
|
||||
parser = StringTypeParser()
|
||||
@@ -310,14 +321,3 @@ class TestStringTypeParser(TestCase):
|
||||
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