(fix): Adds check for discriminator type #40
@@ -1,8 +1,8 @@
|
||||
from jambo.parser._type_parser import GenericTypeParser
|
||||
from jambo.types.type_parser_options import TypeParserOptions
|
||||
|
||||
from pydantic import BeforeValidator, Field, TypeAdapter, ValidationError
|
||||
from typing_extensions import Annotated, Any, Union, Unpack
|
||||
from pydantic import BaseModel, BeforeValidator, Field, TypeAdapter, ValidationError
|
||||
from typing_extensions import Annotated, Any, Union, Unpack, get_args
|
||||
|
||||
|
||||
class OneOfTypeParser(GenericTypeParser):
|
||||
@@ -16,7 +16,7 @@ class OneOfTypeParser(GenericTypeParser):
|
||||
if "oneOf" not in properties:
|
||||
raise ValueError(f"Invalid JSON Schema: {properties}")
|
||||
|
||||
if not isinstance(properties["oneOf"], list):
|
||||
if not isinstance(properties["oneOf"], list) or len(properties["oneOf"]) == 0:
|
||||
raise ValueError(f"Invalid JSON Schema: {properties['oneOf']}")
|
||||
|
||||
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
||||
@@ -57,6 +57,16 @@ class OneOfTypeParser(GenericTypeParser):
|
||||
if not isinstance(discriminator_prop, dict):
|
||||
raise ValueError("Discriminator must be a dictionary")
|
||||
|
||||
for field in subfield_types:
|
||||
field_type, field_info = get_args(field)
|
||||
|
||||
if issubclass(field_type, BaseModel):
|
||||
continue
|
||||
|
||||
raise ValueError(
|
||||
"When using a discriminator, all subfield types must be of type 'object'."
|
||||
)
|
||||
|
||||
property_name = discriminator_prop.get("propertyName")
|
||||
if property_name is None or not isinstance(property_name, str):
|
||||
raise ValueError("Discriminator must have a 'propertyName' key")
|
||||
|
||||
@@ -196,6 +196,29 @@ class TestOneOfTypeParser(TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
Model(pet={"type": "bird", "flies": True})
|
||||
|
||||
def test_oneof_with_invalid_types(self):
|
||||
with self.assertRaises(ValueError):
|
||||
SchemaConverter.build(
|
||||
{
|
||||
"title": "Pet",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"pet": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "number",
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
},
|
||||
],
|
||||
"discriminator": {"propertyName": "type"},
|
||||
}
|
||||
},
|
||||
"required": ["pet"],
|
||||
}
|
||||
)
|
||||
|
||||
def test_oneof_with_discriminator_mapping(self):
|
||||
schema = {
|
||||
"title": "Vehicle",
|
||||
|
||||
Reference in New Issue
Block a user