Feature/oneof unique subtypes naming #58

Merged
HideyoshiNakazone merged 2 commits from feature/oneof-unique-subtypes-naming into main 2025-11-24 01:11:01 +00:00
2 changed files with 51 additions and 4 deletions
Showing only changes of commit f80a1bbda3 - Show all commits

View File

@@ -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):

View File

@@ -1,5 +1,6 @@
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
@@ -791,3 +792,49 @@ 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)
_ = schema_type.model_validate(
{"operating_system": {"name": "Ubuntu", "version": "20.04"}}
)
_ = schema_type.model_validate(
{
"operating_system": {
"creation": {"name": "Ubuntu", "version": "20.04"},
"reinstallation": {"name": "Ubuntu", "version": "22.04"},
}
}
)