feat: Add examples #54
@@ -45,8 +45,7 @@ class OneOfTypeParser(GenericTypeParser):
|
||||
# they were added by OpenAPI and not all implementations may support them,
|
||||
# and they do not always generate a model one-to-one to the Pydantic model
|
||||
# TL;DR: Discriminators were added by OpenAPI and not a Official JSON Schema feature
|
||||
discriminator = properties.get("discriminator")
|
||||
if discriminator is not None:
|
||||
if (discriminator := properties.get("discriminator")) is not None:
|
||||
validated_type = self._build_type_one_of_with_discriminator(
|
||||
subfield_types, discriminator
|
||||
)
|
||||
|
||||
@@ -99,7 +99,33 @@ class TestAnyOfTypeParser(TestCase):
|
||||
with self.assertRaises(InvalidSchemaException):
|
||||
AnyOfTypeParser().from_properties("placeholder", properties)
|
||||
|
||||
def test_any_of_with_examples(self):
|
||||
def test_anyof_with_examples(self):
|
||||
"""
|
||||
Tests the AnyOfTypeParser with a string or int type and examples.
|
||||
"""
|
||||
|
||||
properties = {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"examples": ["example string"],
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"examples": [123],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
parsed_type, _ = AnyOfTypeParser().from_properties("placeholder", properties)
|
||||
|
||||
type_1, type_2 = get_args(parsed_type)
|
||||
|
||||
self.assertEqual(get_args(type_1)[1].examples, ["example string"])
|
||||
|
||||
self.assertEqual(get_args(type_2)[1].examples, [123])
|
||||
|
||||
def test_any_of_with_root_examples(self):
|
||||
"""
|
||||
Tests the AnyOfTypeParser with a string or int type and examples.
|
||||
"""
|
||||
|
||||
@@ -532,3 +532,71 @@ class TestOneOfTypeParser(TestCase):
|
||||
# Invalid: Wrong properties for the type
|
||||
with self.assertRaises(ValidationError):
|
||||
Model(shape={"type": "circle", "width": 10})
|
||||
|
||||
def test_oneof_with_examples(self):
|
||||
schema = {
|
||||
"title": "ExampleTest",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"examples": ["example1", "example2"],
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"examples": [1, 2, 3],
|
||||
},
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": ["value"],
|
||||
}
|
||||
|
||||
Model = SchemaConverter.build(schema)
|
||||
|
||||
# Since Pydantic does not natively support oneOf and the validation
|
||||
# is done via a custom validator, the `value` is represented using `anyOf`
|
||||
model_schema = Model.model_json_schema()
|
||||
|
||||
self.assertEqual(
|
||||
model_schema["properties"]["value"]["anyOf"][0]["examples"],
|
||||
["example1", "example2"],
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
model_schema["properties"]["value"]["anyOf"][1]["examples"],
|
||||
[1, 2, 3],
|
||||
)
|
||||
|
||||
def test_oneof_with_root_examples(self):
|
||||
schema = {
|
||||
"title": "ExampleTest",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "string",
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
},
|
||||
],
|
||||
"examples": ["example1", 2],
|
||||
}
|
||||
},
|
||||
"required": ["value"],
|
||||
}
|
||||
|
||||
Model = SchemaConverter.build(schema)
|
||||
|
||||
# Since Pydantic does not natively support oneOf and the validation
|
||||
# is done via a custom validator, the `value` is represented using `anyOf`
|
||||
model_schema = Model.model_json_schema()
|
||||
|
||||
self.assertEqual(
|
||||
model_schema["properties"]["value"]["examples"],
|
||||
["example1", 2],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user