feat: adds example to allOf

This commit is contained in:
2025-11-23 18:59:47 -03:00
parent cfbe1f38c8
commit ffbd124cf9
3 changed files with 67 additions and 0 deletions

View File

@@ -27,6 +27,9 @@ class AllOfTypeParser(GenericTypeParser):
sub_properties sub_properties
) )
if (examples := properties.get("examples")) is not None:
combined_properties["examples"] = examples
return parser().from_properties_impl(name, combined_properties, **kwargs) return parser().from_properties_impl(name, combined_properties, **kwargs)
@staticmethod @staticmethod

View File

@@ -309,3 +309,50 @@ class TestAllOfTypeParser(TestCase):
with self.assertRaises(InvalidSchemaException): with self.assertRaises(InvalidSchemaException):
AllOfTypeParser().from_properties("placeholder", properties) AllOfTypeParser().from_properties("placeholder", properties)
def test_all_of_with_root_examples(self):
"""
Tests the AllOfTypeParser with examples.
"""
properties = {
"type": "object",
"allOf": [
{
"properties": {
"name": {
"type": "string",
"minLength": 1,
}
},
},
{
"properties": {
"name": {
"type": "string",
"maxLength": 4,
}
},
},
],
"examples": [
{"name": "John"},
{"name": "Jane"},
{"name": "Doe"},
{"name": "Jack"},
],
}
type_parsed, type_properties = AllOfTypeParser().from_properties(
"placeholder", properties
)
self.assertEqual(
type_properties["examples"],
[
type_parsed(name="John"),
type_parsed(name="Jane"),
type_parsed(name="Doe"),
type_parsed(name="Jack"),
],
)

View File

@@ -98,3 +98,20 @@ class TestAnyOfTypeParser(TestCase):
with self.assertRaises(InvalidSchemaException): with self.assertRaises(InvalidSchemaException):
AnyOfTypeParser().from_properties("placeholder", properties) AnyOfTypeParser().from_properties("placeholder", properties)
def test_any_of_with_examples(self):
"""
Tests the AnyOfTypeParser with a string or int type and examples.
"""
properties = {
"anyOf": [
{"type": "string"},
{"type": "integer"},
],
"examples": ["100", 100],
}
_, type_validator = AnyOfTypeParser().from_properties("placeholder", properties)
self.assertEqual(type_validator["examples"], ["100", 100])