Fix jsonschema compliance: use number instead of float (#25049)
Signed-off-by: Yongtao Huang<yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -258,5 +258,5 @@ def convert_input_form_to_parameters(
|
|||||||
parameters[item.variable]["type"] = "string"
|
parameters[item.variable]["type"] = "string"
|
||||||
parameters[item.variable]["enum"] = item.options
|
parameters[item.variable]["enum"] = item.options
|
||||||
elif item.type == VariableEntityType.NUMBER:
|
elif item.type == VariableEntityType.NUMBER:
|
||||||
parameters[item.variable]["type"] = "float"
|
parameters[item.variable]["type"] = "number"
|
||||||
return parameters, required
|
return parameters, required
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
|
import jsonschema
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from core.app.app_config.entities import VariableEntity, VariableEntityType
|
from core.app.app_config.entities import VariableEntity, VariableEntityType
|
||||||
@@ -434,7 +435,7 @@ class TestUtilityFunctions:
|
|||||||
assert parameters["category"]["enum"] == ["A", "B", "C"]
|
assert parameters["category"]["enum"] == ["A", "B", "C"]
|
||||||
|
|
||||||
assert "count" in parameters
|
assert "count" in parameters
|
||||||
assert parameters["count"]["type"] == "float"
|
assert parameters["count"]["type"] == "number"
|
||||||
|
|
||||||
# FILE type should be skipped - it creates empty dict but gets filtered later
|
# FILE type should be skipped - it creates empty dict but gets filtered later
|
||||||
# Check that it doesn't have any meaningful content
|
# Check that it doesn't have any meaningful content
|
||||||
@@ -447,3 +448,65 @@ class TestUtilityFunctions:
|
|||||||
assert "category" not in required
|
assert "category" not in required
|
||||||
|
|
||||||
# Note: _get_request_id function has been removed as request_id is now passed as parameter
|
# Note: _get_request_id function has been removed as request_id is now passed as parameter
|
||||||
|
|
||||||
|
def test_convert_input_form_to_parameters_jsonschema_validation_ok(self):
|
||||||
|
"""Current schema uses 'number' for numeric fields; it should be a valid JSON Schema."""
|
||||||
|
user_input_form = [
|
||||||
|
VariableEntity(
|
||||||
|
type=VariableEntityType.NUMBER,
|
||||||
|
variable="count",
|
||||||
|
description="Count",
|
||||||
|
label="Count",
|
||||||
|
required=True,
|
||||||
|
),
|
||||||
|
VariableEntity(
|
||||||
|
type=VariableEntityType.TEXT_INPUT,
|
||||||
|
variable="name",
|
||||||
|
description="User name",
|
||||||
|
label="Name",
|
||||||
|
required=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
parameters_dict = {
|
||||||
|
"count": "Enter count",
|
||||||
|
"name": "Enter your name",
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters, required = convert_input_form_to_parameters(user_input_form, parameters_dict)
|
||||||
|
|
||||||
|
# Build a complete JSON Schema
|
||||||
|
schema = {
|
||||||
|
"type": "object",
|
||||||
|
"properties": parameters,
|
||||||
|
"required": required,
|
||||||
|
}
|
||||||
|
|
||||||
|
# 1) The schema itself must be valid
|
||||||
|
jsonschema.Draft202012Validator.check_schema(schema)
|
||||||
|
|
||||||
|
# 2) Both float and integer instances should pass validation
|
||||||
|
jsonschema.validate(instance={"count": 3.14, "name": "alice"}, schema=schema)
|
||||||
|
jsonschema.validate(instance={"count": 2, "name": "bob"}, schema=schema)
|
||||||
|
|
||||||
|
def test_legacy_float_type_schema_is_invalid(self):
|
||||||
|
"""Legacy/buggy behavior: using 'float' should produce an invalid JSON Schema."""
|
||||||
|
# Manually construct a legacy/incorrect schema (simulating old behavior)
|
||||||
|
bad_schema = {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"count": {
|
||||||
|
"type": "float", # Invalid type: JSON Schema does not support 'float'
|
||||||
|
"description": "Enter count",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["count"],
|
||||||
|
}
|
||||||
|
|
||||||
|
# The schema itself should raise a SchemaError
|
||||||
|
with pytest.raises(jsonschema.exceptions.SchemaError):
|
||||||
|
jsonschema.Draft202012Validator.check_schema(bad_schema)
|
||||||
|
|
||||||
|
# Or validation should also raise SchemaError
|
||||||
|
with pytest.raises(jsonschema.exceptions.SchemaError):
|
||||||
|
jsonschema.validate(instance={"count": 1.23}, schema=bad_schema)
|
||||||
|
Reference in New Issue
Block a user