From 086486e40ab818b0dd68d27a2bba3da67617c749 Mon Sep 17 00:00:00 2001 From: Tobias Watzek Date: Tue, 10 Sep 2024 20:11:32 +0200 Subject: [PATCH] Allow falsy values as path parameters in URL This makes it possible to set falsy values as path parameters except `null` or `undefined`. Before this change, it was not possible to set `0` or `false` as path parameters because they were filtered out from the data passed to `StdUriTemplate.expand`. --- .../abstractions/src/requestInformation.ts | 2 +- .../test/common/requestInformation.ts | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/abstractions/src/requestInformation.ts b/packages/abstractions/src/requestInformation.ts index a153a4951..c093a78ad 100644 --- a/packages/abstractions/src/requestInformation.ts +++ b/packages/abstractions/src/requestInformation.ts @@ -68,7 +68,7 @@ export class RequestInformation implements RequestInformationSetContent { } } for (const key in this.pathParameters) { - if (this.pathParameters[key]) { + if (this.pathParameters[key] !== null && this.pathParameters[key] !== undefined) { data[key] = this.normalizeValue(this.pathParameters[key]); } } diff --git a/packages/abstractions/test/common/requestInformation.ts b/packages/abstractions/test/common/requestInformation.ts index 11fd901a4..7326d9c81 100644 --- a/packages/abstractions/test/common/requestInformation.ts +++ b/packages/abstractions/test/common/requestInformation.ts @@ -96,6 +96,30 @@ describe("RequestInformation", () => { assert.equal(requestInformation.URL, "http://localhost/me?datasets=1,2"); }); + it.each([ + { someNumber: -1, expected: "http://localhost/-1" }, + { someNumber: 0, expected: "http://localhost/0" }, + { someNumber: 1, expected: "http://localhost/1" }, + { someNumber: NaN, expected: "http://localhost/NaN" }, + ])("Sets number $someNumber in path parameters", () => { + const requestInformation = new RequestInformation(); + requestInformation.pathParameters["baseurl"] = baseUrl; + requestInformation.pathParameters["someNumber"] = 0; + requestInformation.urlTemplate = "http://localhost/{someNumber}"; + assert.equal(requestInformation.URL, "http://localhost/0"); + }); + + it.each([ + { someBoolean: true, expected: "http://localhost/true" }, + { someBoolean: false, expected: "http://localhost/false" }, + ])("Sets false in path parameters", () => { + const requestInformation = new RequestInformation(); + requestInformation.pathParameters["baseurl"] = baseUrl; + requestInformation.pathParameters["someBoolean"] = false; + requestInformation.urlTemplate = "http://localhost/{someBoolean}"; + assert.equal(requestInformation.URL, "http://localhost/false"); + }); + it("Sets enum value in path parameters", () => { const requestInformation = new RequestInformation(); requestInformation.pathParameters["baseurl"] = baseUrl;