From 5c5483a7f0831150a70c092624e626843158a17d Mon Sep 17 00:00:00 2001 From: Wei Mingzhi Date: Fri, 15 Nov 2024 13:55:53 +0800 Subject: [PATCH] Fix issue with using an JSON object with single quote in a string when testing API tools. Simply replacing the single quote with double quote will cause issue if some string in the JSON object contains single quotes. This fixes the issue. --- api/core/tools/tool/api_tool.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/api/core/tools/tool/api_tool.py b/api/core/tools/tool/api_tool.py index c779d704c3..f96b16442a 100644 --- a/api/core/tools/tool/api_tool.py +++ b/api/core/tools/tool/api_tool.py @@ -263,12 +263,14 @@ class ApiTool(Tool): elif property["type"] == "object" or property["type"] == "array": if isinstance(value, str): try: - # an array str like '[1,2]' also can convert to list [1,2] through json.loads - # json not support single quote, but we can support it - value = value.replace("'", '"') - return json.loads(value) + # an array str like '[1,2]' can also be converted to list [1,2] through json.loads + # json does not support single quotes, but we can support it + return json.loads(value.replace("'", '"')) except ValueError: - return value + try: + return json.loads(value) + except ValueError: + return value elif isinstance(value, dict): return value else: