diff --git a/python/semantic_kernel/connectors/openapi_plugin/models/rest_api_operation.py b/python/semantic_kernel/connectors/openapi_plugin/models/rest_api_operation.py index cc137083b4da..a9af303a2e10 100644 --- a/python/semantic_kernel/connectors/openapi_plugin/models/rest_api_operation.py +++ b/python/semantic_kernel/connectors/openapi_plugin/models/rest_api_operation.py @@ -215,7 +215,10 @@ def build_headers(self, arguments: dict[str, Any]) -> dict[str, str]: ) continue - headers[parameter.name] = str(argument) + if isinstance(argument, (list, tuple)): + headers[parameter.name] = ",".join(self._format_parameter_value(item) for item in argument) + else: + headers[parameter.name] = self._format_parameter_value(argument) return headers @@ -328,7 +331,13 @@ def build_path(self, path_template: str, arguments: dict[str, Any]) -> str: f"required parameter of the operation - `{self.id}`." ) continue - path_template = path_template.replace(f"{{{parameter.name}}}", quote(str(argument), safe="")) + if isinstance(argument, (list, tuple)): + encoded_value = ",".join( + quote(self._format_parameter_value(item), safe="") for item in argument + ) + else: + encoded_value = quote(self._format_parameter_value(argument), safe="") + path_template = path_template.replace(f"{{{parameter.name}}}", encoded_value) self._validate_path_segments(path_template) return path_template @@ -383,6 +392,13 @@ def _is_non_relative_path(path: str) -> bool: parsed = urlparse(path) return bool(parsed.scheme or parsed.netloc) + @staticmethod + def _format_parameter_value(value: Any) -> str: + """Format a parameter value for serialization according to OpenAPI conventions.""" + if isinstance(value, bool): + return "true" if value else "false" + return str(value) + def build_query_string(self, arguments: dict[str, Any]) -> str: """Build the query string for the operation.""" segments = [] @@ -396,7 +412,11 @@ def build_query_string(self, arguments: dict[str, Any]) -> str: f"required parameter of the operation - `{self.id}`." ) continue - segments.append((parameter.name, argument)) + if isinstance(argument, (list, tuple)): + for item in argument: + segments.append((parameter.name, self._format_parameter_value(item))) + else: + segments.append((parameter.name, self._format_parameter_value(argument))) return urlencode(segments) def replace_invalid_symbols(self, parameter_name): diff --git a/python/tests/unit/connectors/openapi_plugin/test_sk_openapi.py b/python/tests/unit/connectors/openapi_plugin/test_sk_openapi.py index 58f79f9850eb..b377e84f8023 100644 --- a/python/tests/unit/connectors/openapi_plugin/test_sk_openapi.py +++ b/python/tests/unit/connectors/openapi_plugin/test_sk_openapi.py @@ -685,6 +685,50 @@ def test_build_query_string_with_optional_and_required_parameters(): assert operation.build_query_string(arguments) == expected_query_string +def test_build_query_string_with_array_and_boolean_parameters(): + parameters = [ + RestApiParameter(name="tags", type="array", location=RestApiParameterLocation.QUERY, is_required=False), + RestApiParameter(name="active", type="boolean", location=RestApiParameterLocation.QUERY, is_required=False), + RestApiParameter(name="archived", type="boolean", location=RestApiParameterLocation.QUERY, is_required=False), + ] + operation = RestApiOperation( + id="test", method="GET", servers=["https://example.com/"], path="/resource", params=parameters + ) + arguments = {"tags": ["red", "blue"], "active": True, "archived": False} + expected_query_string = "tags=red&tags=blue&active=true&archived=false" + assert operation.build_query_string(arguments) == expected_query_string + + +def test_build_headers_with_array_and_boolean_parameters(): + parameters = [ + RestApiParameter(name="X-Flags", type="array", location=RestApiParameterLocation.HEADER, is_required=False), + RestApiParameter(name="X-Active", type="boolean", location=RestApiParameterLocation.HEADER, is_required=False), + ] + operation = RestApiOperation( + id="test", method="GET", servers=["https://example.com/"], path="/resource", params=parameters + ) + arguments = {"X-Flags": ["tag1", "tag2"], "X-Active": True} + expected_headers = {"X-Flags": "tag1,tag2", "X-Active": "true"} + assert operation.build_headers(arguments) == expected_headers + + +def test_build_path_with_boolean_and_array_parameters(): + parameters = [ + RestApiParameter(name="flag", type="boolean", location=RestApiParameterLocation.PATH, is_required=True), + RestApiParameter(name="items", type="array", location=RestApiParameterLocation.PATH, is_required=True), + ] + operation = RestApiOperation( + id="test", method="GET", servers=["https://example.com/"], path="/resource/{flag}/{items}", params=parameters + ) + arguments = {"flag": True, "items": ["a", "b"]} + expected_path = "/resource/true/a,b" + assert operation.build_path(operation.path, arguments) == expected_path + + # Verify that commas within individual array items are properly percent-encoded + arguments_with_inner_comma = {"flag": False, "items": ["a,b", "c"]} + assert operation.build_path(operation.path, arguments_with_inner_comma) == "/resource/false/a%2Cb,c" + + def test_create_payload_artificial_parameter_with_text_plain(): properties = [ RestApiPayloadProperty(