Implements: allOf, anyOf #11
@@ -1,6 +1,7 @@
|
||||
# Exports generic type parser
|
||||
from ._type_parser import GenericTypeParser as GenericTypeParser
|
||||
from .allof_type_parser import AllOfTypeParser as AllOfTypeParser
|
||||
from .anyof_type_parser import AnyOfTypeParser as AnyOfTypeParser
|
||||
from .array_type_parser import ArrayTypeParser as ArrayTypeParser
|
||||
from .boolean_type_parser import BooleanTypeParser as BooleanTypeParser
|
||||
from .float_type_parser import FloatTypeParser as FloatTypeParser
|
||||
|
||||
28
jambo/parser/anyof_type_parser.py
Normal file
28
jambo/parser/anyof_type_parser.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from jambo.parser._type_parser import GenericTypeParser
|
||||
|
||||
from typing import Union
|
||||
|
||||
|
||||
class AnyOfTypeParser(GenericTypeParser):
|
||||
mapped_type = Union
|
||||
|
||||
json_schema_type = "anyOf"
|
||||
|
||||
@staticmethod
|
||||
def from_properties(name, properties):
|
||||
if "anyOf" not in properties:
|
||||
raise ValueError(f"Invalid JSON Schema: {properties}")
|
||||
|
||||
if not isinstance(properties["anyOf"], list):
|
||||
raise ValueError(f"Invalid JSON Schema: {properties['anyOf']}")
|
||||
|
||||
subProperties = properties["anyOf"]
|
||||
|
||||
types = [
|
||||
GenericTypeParser.get_impl(subProperty["type"]).from_properties(
|
||||
name, subProperty
|
||||
)
|
||||
for subProperty in subProperties
|
||||
]
|
||||
|
||||
return Union[*(t for t, v in types)], {}
|
||||
@@ -84,8 +84,6 @@ class SchemaConverter:
|
||||
_field_type = "anyOf"
|
||||
case {"allOf": _}:
|
||||
_field_type = "allOf"
|
||||
case {"oneOf": _}:
|
||||
_field_type = "oneOf"
|
||||
case {"type": _}:
|
||||
_field_type = properties["type"]
|
||||
case _:
|
||||
|
||||
25
tests/parser/test_anyof_type_parser.py
Normal file
25
tests/parser/test_anyof_type_parser.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from jambo.parser.anyof_type_parser import AnyOfTypeParser
|
||||
|
||||
from typing import Union, get_args, get_origin
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class TestAnyOfTypeParser(TestCase):
|
||||
def test_any_of_string_or_int(self):
|
||||
"""
|
||||
Tests the AnyOfTypeParser with a string or int type.
|
||||
"""
|
||||
|
||||
properties = {
|
||||
"anyOf": [
|
||||
{"type": "string"},
|
||||
{"type": "integer"},
|
||||
],
|
||||
}
|
||||
|
||||
type_parsing, _ = AnyOfTypeParser.from_properties("placeholder", properties)
|
||||
|
||||
# check union type has string and int
|
||||
self.assertEqual(get_origin(type_parsing), Union)
|
||||
self.assertIn(str, get_args(type_parsing))
|
||||
self.assertIn(int, get_args(type_parsing))
|
||||
Reference in New Issue
Block a user