From db53656a45fdb13447a410b9b4609d991013d89d Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Wed, 3 Sep 2025 22:27:41 +0800 Subject: [PATCH] Fix jsonschema compliance: use number instead of float (#25049) Signed-off-by: Yongtao Huang Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- api/core/mcp/server/streamable_http.py | 2 +- .../core/mcp/server/test_streamable_http.py | 65 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/api/core/mcp/server/streamable_http.py b/api/core/mcp/server/streamable_http.py index 5851c6d40..3d51ac233 100644 --- a/api/core/mcp/server/streamable_http.py +++ b/api/core/mcp/server/streamable_http.py @@ -258,5 +258,5 @@ def convert_input_form_to_parameters( parameters[item.variable]["type"] = "string" parameters[item.variable]["enum"] = item.options elif item.type == VariableEntityType.NUMBER: - parameters[item.variable]["type"] = "float" + parameters[item.variable]["type"] = "number" return parameters, required diff --git a/api/tests/unit_tests/core/mcp/server/test_streamable_http.py b/api/tests/unit_tests/core/mcp/server/test_streamable_http.py index ccc5d42bc..f1d741602 100644 --- a/api/tests/unit_tests/core/mcp/server/test_streamable_http.py +++ b/api/tests/unit_tests/core/mcp/server/test_streamable_http.py @@ -1,6 +1,7 @@ import json from unittest.mock import Mock, patch +import jsonschema import pytest from core.app.app_config.entities import VariableEntity, VariableEntityType @@ -434,7 +435,7 @@ class TestUtilityFunctions: assert parameters["category"]["enum"] == ["A", "B", "C"] 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 # Check that it doesn't have any meaningful content @@ -447,3 +448,65 @@ class TestUtilityFunctions: assert "category" not in required # 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)