Final Implementation of AllOf Keyword
This commit is contained in:
@@ -19,9 +19,15 @@ class AllOfTypeParser(GenericTypeParser):
|
|||||||
if _mapped_type is None:
|
if _mapped_type is None:
|
||||||
raise ValueError("Invalid JSON Schema: 'type' is not specified.")
|
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.")
|
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(
|
combined_properties = AllOfTypeParser._rebuild_properties_from_subproperties(
|
||||||
subProperties
|
subProperties
|
||||||
)
|
)
|
||||||
@@ -46,13 +52,6 @@ class AllOfTypeParser(GenericTypeParser):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _validate_prop(prop_name, old_value, new_value):
|
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":
|
if prop_name == "description":
|
||||||
return f"{old_value} | {new_value}"
|
return f"{old_value} | {new_value}"
|
||||||
|
|
||||||
@@ -72,5 +71,20 @@ class AllOfTypeParser(GenericTypeParser):
|
|||||||
if prop_name in ("minLength", "minimum", "exclusiveMinimum"):
|
if prop_name in ("minLength", "minimum", "exclusiveMinimum"):
|
||||||
return old_value if old_value < new_value else new_value
|
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
|
# Handle other properties by just returning the first valued
|
||||||
return old_value
|
return old_value
|
||||||
|
|||||||
@@ -58,7 +58,11 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
|
|
||||||
# Linters
|
# Linters
|
||||||
|
[tool.ruff]
|
||||||
|
extend-select = ["I"]
|
||||||
|
|
||||||
[tool.ruff.lint.isort]
|
[tool.ruff.lint.isort]
|
||||||
|
known-first-party = ["jambo"]
|
||||||
section-order=[
|
section-order=[
|
||||||
"future",
|
"future",
|
||||||
"first-party",
|
"first-party",
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ class TestSchemaConverter(TestCase):
|
|||||||
"properties": {
|
"properties": {
|
||||||
"name": {
|
"name": {
|
||||||
"allOf": [
|
"allOf": [
|
||||||
|
{"type": "string", "maxLength": 11},
|
||||||
{"type": "string", "maxLength": 4},
|
{"type": "string", "maxLength": 4},
|
||||||
{"type": "string", "minLength": 1},
|
{"type": "string", "minLength": 1},
|
||||||
{"type": "string", "minLength": 2},
|
{"type": "string", "minLength": 2},
|
||||||
|
|||||||
Reference in New Issue
Block a user