Implements: allOf, anyOf #11
@@ -19,9 +19,15 @@ class AllOfTypeParser(GenericTypeParser):
|
||||
if _mapped_type is None:
|
||||
raise ValueError("Invalid JSON Schema: 'type' is not specified.")
|
||||
|
||||
if not all(prop.get("type") == _mapped_type for prop in subProperties):
|
||||
if any(
|
||||
[prop.get("type", _mapped_type) != _mapped_type for prop in subProperties]
|
||||
):
|
||||
raise ValueError("Invalid JSON Schema: allOf types do not match.")
|
||||
|
||||
for subProperty in subProperties:
|
||||
# If a sub-property has not defined a type, we need to set it to the top-level type
|
||||
subProperty["type"] = _mapped_type
|
||||
|
||||
combined_properties = AllOfTypeParser._rebuild_properties_from_subproperties(
|
||||
subProperties
|
||||
)
|
||||
@@ -46,13 +52,6 @@ class AllOfTypeParser(GenericTypeParser):
|
||||
|
||||
@staticmethod
|
||||
def _validate_prop(prop_name, old_value, new_value):
|
||||
if prop_name == "type":
|
||||
if old_value != new_value:
|
||||
raise ValueError(
|
||||
f"Invalid JSON Schema: conflicting types for '{prop_name}'"
|
||||
)
|
||||
return old_value
|
||||
|
||||
if prop_name == "description":
|
||||
return f"{old_value} | {new_value}"
|
||||
|
||||
@@ -72,5 +71,20 @@ class AllOfTypeParser(GenericTypeParser):
|
||||
if prop_name in ("minLength", "minimum", "exclusiveMinimum"):
|
||||
return old_value if old_value < new_value else new_value
|
||||
|
||||
if prop_name == "properties":
|
||||
for key, value in new_value.items():
|
||||
if key not in old_value:
|
||||
old_value[key] = value
|
||||
continue
|
||||
|
||||
for sub_key, sub_value in value.items():
|
||||
if sub_key not in old_value[key]:
|
||||
old_value[key][sub_key] = sub_value
|
||||
else:
|
||||
# Merge properties if they exist in both sub-properties
|
||||
old_value[key][sub_key] = AllOfTypeParser._validate_prop(
|
||||
sub_key, old_value[key][sub_key], sub_value
|
||||
)
|
||||
|
||||
# Handle other properties by just returning the first valued
|
||||
return old_value
|
||||
|
||||
@@ -58,7 +58,11 @@ build-backend = "hatchling.build"
|
||||
|
||||
|
||||
# Linters
|
||||
[tool.ruff]
|
||||
extend-select = ["I"]
|
||||
|
||||
[tool.ruff.lint.isort]
|
||||
known-first-party = ["jambo"]
|
||||
section-order=[
|
||||
"future",
|
||||
"first-party",
|
||||
|
||||
@@ -290,6 +290,7 @@ class TestSchemaConverter(TestCase):
|
||||
"properties": {
|
||||
"name": {
|
||||
"allOf": [
|
||||
{"type": "string", "maxLength": 11},
|
||||
{"type": "string", "maxLength": 4},
|
||||
{"type": "string", "minLength": 1},
|
||||
{"type": "string", "minLength": 2},
|
||||
|
||||
Reference in New Issue
Block a user