diff --git a/sdk/core/core-client-rest/CHANGELOG.md b/sdk/core/core-client-rest/CHANGELOG.md index f2e624066861..e9ff74054b20 100644 --- a/sdk/core/core-client-rest/CHANGELOG.md +++ b/sdk/core/core-client-rest/CHANGELOG.md @@ -8,6 +8,8 @@ ### Bugs Fixed +- Allow `number` path parameters. PR [#31352](https://github.com/Azure/azure-sdk-for-js/pull/31352/files) + ### Other Changes ## 2.3.0 (2024-10-03) diff --git a/sdk/core/core-client-rest/review/core-client.api.md b/sdk/core/core-client-rest/review/core-client.api.md index a117fd8f6dd8..ff4a18c9f71f 100644 --- a/sdk/core/core-client-rest/review/core-client.api.md +++ b/sdk/core/core-client-rest/review/core-client.api.md @@ -138,7 +138,7 @@ export interface OperationRequestOptions { // @public export type PathParameters = TRoute extends `${infer _Head}/{${infer _Param}}${infer Tail}` ? [ -pathParameter: string | PathParameterWithOptions, +pathParameter: string | number | PathParameterWithOptions, ...pathParameters: PathParameters ] : [ ]; @@ -146,7 +146,7 @@ pathParameter: string | PathParameterWithOptions, // @public export interface PathParameterWithOptions { allowReserved?: boolean; - value: string; + value: string | number; } // @public diff --git a/sdk/core/core-client-rest/src/common.ts b/sdk/core/core-client-rest/src/common.ts index 21938d67af42..916c4d500fd9 100644 --- a/sdk/core/core-client-rest/src/common.ts +++ b/sdk/core/core-client-rest/src/common.ts @@ -398,7 +398,10 @@ export type PathParameters< // additional parameters we can call RouteParameters recursively on the Tail to match the remaining parts, // in case the Tail has more parameters, it will return a tuple with the parameters found in tail. // We spread the second path params to end up with a single dimension tuple at the end. - [pathParameter: string | PathParameterWithOptions, ...pathParameters: PathParameters] + [ + pathParameter: string | number | PathParameterWithOptions, + ...pathParameters: PathParameters, + ] : // When the path doesn't match the template, it means that we have no path parameters so we return // an empty tuple. []; @@ -438,7 +441,7 @@ export interface PathParameterWithOptions { /** * The value of the parameter. */ - value: string; + value: string | number; /** * Whether to allow for reserved characters in the value. If set to true, special characters such as '/' in the parameter's value will not be URL encoded. diff --git a/sdk/core/core-client-rest/src/urlHelpers.ts b/sdk/core/core-client-rest/src/urlHelpers.ts index 56f98d0bb4b3..e052e7eb02f3 100644 --- a/sdk/core/core-client-rest/src/urlHelpers.ts +++ b/sdk/core/core-client-rest/src/urlHelpers.ts @@ -54,7 +54,7 @@ function isQueryParameterWithOptions(x: unknown): x is QueryParameterWithOptions export function buildRequestUrl( endpoint: string, routePath: string, - pathParameters: (string | PathParameterWithOptions)[], + pathParameters: (string | number | PathParameterWithOptions)[], options: RequestParameters = {}, ): string { if (routePath.startsWith("https://") || routePath.startsWith("http://")) { @@ -187,19 +187,18 @@ export function buildBaseUrl(endpoint: string, options: RequestParameters): stri function buildRoutePath( routePath: string, - pathParameters: (string | PathParameterWithOptions)[], + pathParameters: (string | number | PathParameterWithOptions)[], options: RequestParameters = {}, ): string { for (const pathParam of pathParameters) { - const allowReserved = - typeof pathParam === "string" ? false : (pathParam?.allowReserved ?? false); - let value = typeof pathParam === "string" ? pathParam : pathParam?.value; + const allowReserved = typeof pathParam === "object" && (pathParam.allowReserved ?? false); + let value = typeof pathParam === "object" ? pathParam.value : pathParam; if (!options.skipUrlEncoding && !allowReserved) { value = encodeURIComponent(value); } - routePath = routePath.replace(/\{\w+\}/, value); + routePath = routePath.replace(/\{\w+\}/, String(value)); } return routePath; } diff --git a/sdk/core/core-client-rest/test/urlHelpers.spec.ts b/sdk/core/core-client-rest/test/urlHelpers.spec.ts index 29d5e37b5152..1337542a2271 100644 --- a/sdk/core/core-client-rest/test/urlHelpers.spec.ts +++ b/sdk/core/core-client-rest/test/urlHelpers.spec.ts @@ -19,6 +19,12 @@ describe("urlHelpers", () => { assert.equal(result, `https://example.org/foo/one`); }); + it("should append number path parameters", () => { + const result = buildRequestUrl(mockBaseUrl, "/foo/{id}", [12345]); + + assert.equal(result, "https://example.org/foo/12345"); + }); + it("should append path, fill path parameters and append query parameters", () => { const result = buildRequestUrl(mockBaseUrl, "/foo/{id}", ["one"], { queryParameters: { foo: "1", bar: "two" },