Merge pull request #40 from HideyoshiNakazone/fix/adds-check-for-discriminator-type
(fix): Adds check for discriminator type
This commit was merged in pull request #40.
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
from jambo.parser._type_parser import GenericTypeParser
|
from jambo.parser._type_parser import GenericTypeParser
|
||||||
from jambo.types.type_parser_options import TypeParserOptions
|
from jambo.types.type_parser_options import TypeParserOptions
|
||||||
|
|
||||||
from pydantic import BeforeValidator, Field, TypeAdapter, ValidationError
|
from pydantic import BaseModel, BeforeValidator, Field, TypeAdapter, ValidationError
|
||||||
from typing_extensions import Annotated, Any, Union, Unpack
|
from typing_extensions import Annotated, Any, Union, Unpack, get_args
|
||||||
|
|
||||||
|
|
||||||
class OneOfTypeParser(GenericTypeParser):
|
class OneOfTypeParser(GenericTypeParser):
|
||||||
@@ -16,7 +16,7 @@ class OneOfTypeParser(GenericTypeParser):
|
|||||||
if "oneOf" not in properties:
|
if "oneOf" not in properties:
|
||||||
raise ValueError(f"Invalid JSON Schema: {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']}")
|
raise ValueError(f"Invalid JSON Schema: {properties['oneOf']}")
|
||||||
|
|
||||||
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
||||||
@@ -57,6 +57,16 @@ class OneOfTypeParser(GenericTypeParser):
|
|||||||
if not isinstance(discriminator_prop, dict):
|
if not isinstance(discriminator_prop, dict):
|
||||||
raise ValueError("Discriminator must be a dictionary")
|
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")
|
property_name = discriminator_prop.get("propertyName")
|
||||||
if property_name is None or not isinstance(property_name, str):
|
if property_name is None or not isinstance(property_name, str):
|
||||||
raise ValueError("Discriminator must have a 'propertyName' key")
|
raise ValueError("Discriminator must have a 'propertyName' key")
|
||||||
|
|||||||
@@ -196,6 +196,29 @@ class TestOneOfTypeParser(TestCase):
|
|||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
Model(pet={"type": "bird", "flies": True})
|
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):
|
def test_oneof_with_discriminator_mapping(self):
|
||||||
schema = {
|
schema = {
|
||||||
"title": "Vehicle",
|
"title": "Vehicle",
|
||||||
|
|||||||
Reference in New Issue
Block a user