Merge pull request #58 from HideyoshiNakazone/feature/oneof-unique-subtypes-naming
Feature/oneof unique subtypes naming
This commit was merged in pull request #58.
This commit is contained in:
@@ -30,8 +30,10 @@ class AnyOfTypeParser(GenericTypeParser):
|
|||||||
sub_properties = properties["anyOf"]
|
sub_properties = properties["anyOf"]
|
||||||
|
|
||||||
sub_types = [
|
sub_types = [
|
||||||
GenericTypeParser.type_from_properties(name, subProperty, **kwargs)
|
GenericTypeParser.type_from_properties(
|
||||||
for subProperty in sub_properties
|
f"{name}_sub{i}", subProperty, **kwargs
|
||||||
|
)
|
||||||
|
for i, subProperty in enumerate(sub_properties)
|
||||||
]
|
]
|
||||||
|
|
||||||
if not kwargs.get("required", False):
|
if not kwargs.get("required", False):
|
||||||
|
|||||||
@@ -29,11 +29,11 @@ class OneOfTypeParser(GenericTypeParser):
|
|||||||
|
|
||||||
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
mapped_properties = self.mappings_properties_builder(properties, **kwargs)
|
||||||
|
|
||||||
sub_properties = properties["oneOf"]
|
|
||||||
|
|
||||||
sub_types = [
|
sub_types = [
|
||||||
GenericTypeParser.type_from_properties(name, subProperty, **kwargs)
|
GenericTypeParser.type_from_properties(
|
||||||
for subProperty in sub_properties
|
f"{name}_sub{i}", subProperty, **kwargs
|
||||||
|
)
|
||||||
|
for i, subProperty in enumerate(properties["oneOf"])
|
||||||
]
|
]
|
||||||
|
|
||||||
if not kwargs.get("required", False):
|
if not kwargs.get("required", False):
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
from jambo import SchemaConverter
|
from jambo import SchemaConverter
|
||||||
from jambo.exceptions import InvalidSchemaException, UnsupportedSchemaException
|
from jambo.exceptions import InvalidSchemaException, UnsupportedSchemaException
|
||||||
|
from jambo.types import JSONSchema
|
||||||
|
|
||||||
from pydantic import AnyUrl, BaseModel, ValidationError
|
from pydantic import AnyUrl, BaseModel, ValidationError
|
||||||
|
from typing_extensions import get_args
|
||||||
|
|
||||||
from ipaddress import IPv4Address, IPv6Address
|
from ipaddress import IPv4Address, IPv6Address
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
@@ -791,3 +793,46 @@ class TestSchemaConverter(TestCase):
|
|||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
Model(a_thing="not none")
|
Model(a_thing="not none")
|
||||||
|
|
||||||
|
def test_scoped_ref_schema(self):
|
||||||
|
schema: JSONSchema = {
|
||||||
|
"title": "Example Schema",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"operating_system": {
|
||||||
|
"oneOf": [
|
||||||
|
{"$ref": "#/$defs/operating_system"},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"creation": {"$ref": "#/$defs/operating_system"},
|
||||||
|
"reinstallation": {"$ref": "#/$defs/operating_system"},
|
||||||
|
},
|
||||||
|
"required": ["creation", "reinstallation"],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"$defs": {
|
||||||
|
"operating_system": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string"},
|
||||||
|
"version": {"type": "string"},
|
||||||
|
},
|
||||||
|
"required": ["name", "version"],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
schema_type = SchemaConverter.build(schema)
|
||||||
|
|
||||||
|
# check for me that the types generated by the oneOf in the typing.Annotated have different names
|
||||||
|
operating_system_field = schema_type.model_fields["operating_system"]
|
||||||
|
|
||||||
|
arg1, arg2 = get_args(operating_system_field.annotation)
|
||||||
|
|
||||||
|
first_type = get_args(arg1)[0]
|
||||||
|
second_type = get_args(arg2)[0]
|
||||||
|
|
||||||
|
self.assertNotEqual(first_type.__name__, second_type.__name__)
|
||||||
|
|||||||
Reference in New Issue
Block a user