Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/python-optional_path-2025-3-18-10-47-35.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/http-client-python"
---

Support optional path parameter.
14 changes: 12 additions & 2 deletions packages/http-client-python/emitter/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,18 @@ function emitFlattenedParameter(
};
}

function emitHttpPathParameter(context: PythonSdkContext, parameter: SdkPathParameter) {
function emitHttpPathParameter(
context: PythonSdkContext,
parameter: SdkPathParameter,
operation: SdkHttpOperation,
): Record<string, any> {
const base = emitParamBase(context, parameter);
if (parameter.optional && operation.path.includes(`/{${parameter.serializedName}}`)) {
operation.path = operation.path.replace(
`/{${parameter.serializedName}}`,
`{${parameter.serializedName}}`,
);
}
return {
...base,
wireName: parameter.serializedName,
Expand Down Expand Up @@ -410,7 +420,7 @@ function emitHttpParameters(
parameters.push(emitHttpQueryParameter(context, parameter, method));
break;
case "path":
parameters.push(emitHttpPathParameter(context, parameter));
parameters.push(emitHttpPathParameter(context, parameter, operation));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def method_location( # pylint: disable=too-many-return-statements
)
if self.code_model.options["only_path_and_body_params_positional"] and query_or_header:
return ParameterMethodLocation.KEYWORD_ONLY
# for optional path parameter, we need to use keyword only
if self.location == ParameterLocation.PATH and self.optional:
return ParameterMethodLocation.KEYWORD_ONLY
return ParameterMethodLocation.POSITIONAL

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def serialize_parameter(self, parameter: ParameterType, serializer_name: str) ->
return f"[{serialize_line} if q is not None else '' for q in {origin_name}]"
return serialize_line

# pylint: disable=line-too-long
def serialize_path(
self,
parameters: Union[
Expand All @@ -124,7 +125,11 @@ def serialize_path(
[
' "{}": {},'.format(
path_parameter.wire_name,
self.serialize_parameter(path_parameter, serializer_name),
(
f'"" if {path_parameter.full_client_name} is None else "/" + {self.serialize_parameter(path_parameter, serializer_name)}'
if path_parameter.optional and isinstance(path_parameter, RequestBuilderParameter)
else self.serialize_parameter(path_parameter, serializer_name)
),
)
for path_parameter in parameters
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ azure-mgmt-core==1.3.2
-e ./generated/encode-numeric
-e ./generated/parameters-basic
-e ./generated/parameters-collection-format
-e ./generated/parameters-path
-e ./generated/parameters-spread
-e ./generated/serialization-encoded-name-json
-e ./generated/server-endpoint-not-defined
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
from parameters.path.aio import PathClient


@pytest.fixture
async def client():
async with PathClient() as client:
yield client


@pytest.mark.asyncio
async def test_normal(client: PathClient):
await client.normal("foo")


@pytest.mark.asyncio
async def test_optional(client: PathClient):
await client.optional()
await client.optional(name="foo")
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def test_create_resource(client: JsonMergePatchClient):
map={"key": inner_madge},
array=[inner_madge],
int_value=1,
float_value=1.1,
float_value=1.25,
inner_model=inner_madge,
int_array=[1, 2, 3],
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
from parameters.path import PathClient


@pytest.fixture
def client():
with PathClient() as client:
yield client


def test_normal(client: PathClient):
client.normal("foo")


def test_optional(client: PathClient):
client.optional()
client.optional(name="foo")
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_create_resource(client: JsonMergePatchClient):
map={"key": inner_madge},
array=[inner_madge],
int_value=1,
float_value=1.1,
float_value=1.25,
inner_model=inner_madge,
int_array=[1, 2, 3],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-e ./generated/encode-numeric
-e ./generated/parameters-basic
-e ./generated/parameters-collection-format
-e ./generated/parameters-path
-e ./generated/parameters-spread
-e ./generated/serialization-encoded-name-json
-e ./generated/server-endpoint-not-defined
Expand Down
22 changes: 11 additions & 11 deletions packages/http-client-python/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/http-client-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@azure-tools/typespec-azure-core": ">=0.54.0 <1.0.0",
"@azure-tools/typespec-azure-resource-manager": ">=0.54.0 <1.0.0",
"@azure-tools/typespec-azure-rulesets": ">=0.54.0 <1.0.0",
"@azure-tools/typespec-client-generator-core": ">=0.54.0 <1.0.0",
"@azure-tools/typespec-client-generator-core": ">=0.54.1 <1.0.0",
"@typespec/compiler": "^1.0.0-0",
"@typespec/http": "^1.0.0-0",
"@typespec/openapi": "^1.0.0-0",
Expand All @@ -81,7 +81,7 @@
"@azure-tools/typespec-azure-core": "~0.54.0",
"@azure-tools/typespec-azure-resource-manager": "~0.54.0",
"@azure-tools/typespec-azure-rulesets": "~0.54.0",
"@azure-tools/typespec-client-generator-core": "~0.54.0",
"@azure-tools/typespec-client-generator-core": "~0.54.1",
"@azure-tools/azure-http-specs": "0.1.0-alpha.13",
"@typespec/compiler": "^1.0.0-0",
"@typespec/http": "^1.0.0-0",
Expand All @@ -92,7 +92,7 @@
"@typespec/sse": "~0.68.0",
"@typespec/streams": "~0.68.0",
"@typespec/xml": "~0.68.0",
"@typespec/http-specs": "0.1.0-alpha.17",
"@typespec/http-specs": "0.1.0-alpha.19",
"@types/js-yaml": "~4.0.5",
"@types/node": "~22.13.14",
"@types/semver": "7.5.8",
Expand Down
Loading