Skip to content

Commit dd026f3

Browse files
Encode OpenAPI server variable values (#14146)
## Summary - validate enum-constrained OpenAPI server variables before substitution - percent-encode substituted server variable values, including defaults - add regression tests for invalid enum values and reserved-character encoding ## Testing - uv run pytest tests/unit/connectors/openapi_plugin/test_sk_openapi.py -q --------- Co-authored-by: Copilot <[email protected]>
1 parent 008d40a commit dd026f3

2 files changed

Lines changed: 87 additions & 4 deletions

File tree

‎python/semantic_kernel/connectors/openapi_plugin/models/rest_api_operation.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,22 @@ def get_server_url(self, server_url_override=None, api_host_url=None, arguments=
287287
# Substitute server variables if available
288288
for variable_name, variable_def in server_variables.items():
289289
argument_name = variable_def.get("argument_name", variable_name)
290+
allowed_values = variable_def.get("enum")
290291
if argument_name in arguments:
291-
value = arguments[argument_name]
292-
server_url_string = server_url_string.replace(f"{{{variable_name}}}", str(value))
292+
value = str(arguments[argument_name])
293293
elif "default" in variable_def and variable_def["default"] is not None:
294294
# Use the default value if no argument is provided
295-
value = variable_def["default"]
296-
server_url_string = server_url_string.replace(f"{{{variable_name}}}", str(value))
295+
value = str(variable_def["default"])
297296
else:
298297
# Raise an exception if no value is available
299298
raise FunctionExecutionException(
300299
f"No argument provided for the '{variable_name}' server variable of the operation '{self.id}'."
301300
)
301+
if allowed_values is not None and value not in allowed_values:
302+
raise FunctionExecutionException(
303+
f"Value '{value}' for server variable '{variable_name}' is not one of the allowed values."
304+
)
305+
server_url_string = server_url_string.replace(f"{{{variable_name}}}", quote(value, safe=""))
302306
elif self.server_url:
303307
server_url_string = self.server_url
304308
elif api_host_url is not None:

‎python/tests/unit/connectors/openapi_plugin/test_sk_openapi.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,74 @@ def test_get_server_url_with_servers_coerces_variable_argument_to_string():
359359
assert operation.get_server_url(arguments=arguments) == expected_url
360360

361361

362+
def test_get_server_url_with_server_variable_enum_rejects_invalid_argument():
363+
operation = RestApiOperation(
364+
id="test",
365+
method="GET",
366+
servers=[
367+
{
368+
"url": "https://{region}.api.vendor.example/v1",
369+
"variables": {"region": {"default": "us", "enum": ["us", "eu"]}},
370+
}
371+
],
372+
path="/resource/{id}",
373+
)
374+
arguments = {"region": "external.example/"}
375+
with pytest.raises(FunctionExecutionException, match="server variable 'region' is not one of the allowed values"):
376+
operation.get_server_url(arguments=arguments)
377+
378+
379+
def test_get_server_url_with_server_variable_enum_allows_valid_argument():
380+
operation = RestApiOperation(
381+
id="test",
382+
method="GET",
383+
servers=[
384+
{
385+
"url": "https://{region}.api.vendor.example/v1",
386+
"variables": {"region": {"default": "us", "enum": ["us", "eu"]}},
387+
}
388+
],
389+
path="/resource/{id}",
390+
)
391+
arguments = {"region": "eu"}
392+
expected_url = "https://eu.api.vendor.example/v1/"
393+
assert operation.get_server_url(arguments=arguments) == expected_url
394+
395+
396+
def test_get_server_url_with_server_variable_enum_rejects_invalid_default():
397+
operation = RestApiOperation(
398+
id="test",
399+
method="GET",
400+
servers=[
401+
{
402+
"url": "https://{region}.api.vendor.example/v1",
403+
"variables": {"region": {"default": "external.example/", "enum": ["us", "eu"]}},
404+
}
405+
],
406+
path="/resource/{id}",
407+
)
408+
with pytest.raises(FunctionExecutionException, match="server variable 'region' is not one of the allowed values"):
409+
operation.get_server_url()
410+
411+
412+
def test_get_server_url_with_server_variable_encodes_reserved_characters():
413+
operation = RestApiOperation(
414+
id="test",
415+
method="GET",
416+
servers=[
417+
{
418+
"url": "https://{region}.api.vendor.example/v1",
419+
"variables": {"region": {"default": "us"}},
420+
}
421+
],
422+
path="/resource/{id}",
423+
)
424+
arguments = {"region": "external.example/"}
425+
result = operation.get_server_url(arguments=arguments)
426+
assert result == "https://external.example%2F.api.vendor.example/v1/"
427+
assert urlparse(result).hostname != "external.example"
428+
429+
362430
def test_get_server_url_with_servers_and_default_variable():
363431
operation = RestApiOperation(
364432
id="test",
@@ -381,6 +449,17 @@ def test_get_server_url_with_servers_coerces_default_variable_to_string():
381449
assert operation.get_server_url() == expected_url
382450

383451

452+
def test_get_server_url_with_servers_encodes_default_variable():
453+
operation = RestApiOperation(
454+
id="test",
455+
method="GET",
456+
servers=[{"url": "https://example.com/{version}", "variables": {"version": {"default": "v1/beta"}}}],
457+
path="/resource/{id}",
458+
)
459+
expected_url = "https://example.com/v1%2Fbeta/"
460+
assert operation.get_server_url() == expected_url
461+
462+
384463
def test_get_server_url_with_override():
385464
operation = RestApiOperation(
386465
id="test",

0 commit comments

Comments
 (0)