From ff143a1844fe230e086762a02c2b07dfb42983ab Mon Sep 17 00:00:00 2001 From: Mary Gao Date: Fri, 15 Jul 2022 18:17:44 +0800 Subject: [PATCH] =?UTF-8?q?Do=20not=20append=20customized=20api-version=20?= =?UTF-8?q?if=20url=20already=20has=20one=20e.g=20LRO/p=E2=80=A6=20(#22423?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Do not append customized api-version if url already has one e.g LRO/paging URL returned by the service * Update format issue * Fix test cases * Bump a new version * Update beta 10 version * Update lock file * Resolve conflict * Update apiVersionPolicy.ts * Merge into main branch * Update test cases * Fix lint errors * Add feature to allow keep url apiVersion in policy * Revert change * Update pnpm lock file * Update lints error * Update file * Update pnpm-lock --- common/config/rush/pnpm-lock.yaml | 30 ++++++++--- sdk/core/core-client-rest/CHANGELOG.md | 6 +++ sdk/core/core-client-rest/package.json | 2 +- .../core-client-rest/src/apiVersionPolicy.ts | 3 +- .../core-client-rest/test/getClient.spec.ts | 53 +++++++++++++------ 5 files changed, 70 insertions(+), 24 deletions(-) diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index 45cb45779237..1327c7376653 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -594,6 +594,18 @@ packages: '@jridgewell/trace-mapping': 0.3.14 dev: false + /@azure-rest/core-client/1.0.0-beta.10: + resolution: {integrity: sha512-qsiepIEskvu+J815SFZ+u0wh0pPHtQVGtyT+yKbLN1YCIFtfM0L5XdaegfwMBWDG/NIXwF2fmqjM75Sqz8dAwA==} + engines: {node: '>=12.0.0'} + dependencies: + '@azure/core-auth': 1.3.2 + '@azure/core-rest-pipeline': 1.9.0 + '@azure/core-util': 1.0.0 + tslib: 2.4.0 + transitivePeerDependencies: + - supports-color + dev: false + /@azure-rest/core-client/1.0.0-beta.9: resolution: {integrity: sha512-zerHAy25S9T8+4DcLKdea/olxGceb9tZMBlw7KWmeasHk1IEdlutVeckKzSt4nQaX4Fm1gnX47TaDEt4VGBTbw==} engines: {node: '>=12.0.0'} @@ -3859,7 +3871,7 @@ packages: dependencies: semver: 7.3.7 shelljs: 0.8.5 - typescript: 4.8.0-dev.20220714 + typescript: 4.8.0-dev.20220715 dev: false /downlevel-dts/0.4.0: @@ -8669,8 +8681,8 @@ packages: hasBin: true dev: false - /typescript/4.8.0-dev.20220714: - resolution: {integrity: sha512-wKK9FMpdvwI68PZiQdNTNmX4rpVXJBDOG9aylV9O6nJUO5YX8yv3bQNcyLc5tbI7J3+u7AU48LVY9SF9lYwy5g==} + /typescript/4.8.0-dev.20220715: + resolution: {integrity: sha512-N2t+SrXdUM14ZwEM2jxxLqq9nSurmXjxOY/1TSoJlp2oHjHr42cBdThZ3X1kFpBj7oXAyDM9q0yrNtzg67Vhlg==} engines: {node: '>=4.2.0'} hasBin: true dev: false @@ -10586,10 +10598,11 @@ packages: dev: false file:projects/arm-containerservice.tgz: - resolution: {integrity: sha512-aiKGYdSpsvlZKEXgm2tA2jYCid5hXUZ7bkfy0J0L5bGwvkVF8pS89QciATkUzH8N8ATJ0u1yPoztf78b+uhVqg==, tarball: file:projects/arm-containerservice.tgz} + resolution: {integrity: sha512-5MDHWMGL9R8LXOQe7EDIS8Z4E7odmDmFPOiF3guRtULnbEf4a9l19C07svZX4wR83ybt1Dkmec31Oml15yt0CA==, tarball: file:projects/arm-containerservice.tgz} name: '@rush-temp/arm-containerservice' version: 0.0.0 dependencies: + '@azure-rest/core-client': 1.0.0-beta.10 '@azure/core-lro': 2.2.4 '@microsoft/api-extractor': 7.18.11 '@rollup/plugin-commonjs': 21.1.0_rollup@2.76.0 @@ -13639,10 +13652,11 @@ packages: dev: false file:projects/arm-servicefabric.tgz: - resolution: {integrity: sha512-a15RkpMr7Lb140sm9+NZNydpMg5UShBzntVUBhUtQRX/z9fvSxQIYPhysZ7KeW70Wd4uV7iqCC6ohItUuNSFyQ==, tarball: file:projects/arm-servicefabric.tgz} + resolution: {integrity: sha512-0MwSVX6/2nbCas25IFzj37me3kGGUSm6K8FNV3lLz1Kh2UHSn14e5X5/kyR72Pwy25pioxEXvGVNB15MgpdPdA==, tarball: file:projects/arm-servicefabric.tgz} name: '@rush-temp/arm-servicefabric' version: 0.0.0 dependencies: + '@azure-rest/core-client': 1.0.0-beta.10 '@azure-tools/test-recorder': 1.0.2 '@azure/core-lro': 2.2.4 '@microsoft/api-extractor': 7.18.11 @@ -14859,10 +14873,11 @@ packages: dev: false file:projects/confidential-ledger.tgz: - resolution: {integrity: sha512-M3cBGZu+X4CafnlQleQ72gzKVhqC1fVNyrEtaSu7xlvQ345Se+ndmk/KV0fDUgZolnrK+Ago8Finzdkw9uDrUw==, tarball: file:projects/confidential-ledger.tgz} + resolution: {integrity: sha512-Q7qvqmIGihTBMTPIpCoSkpCCT0ot0IMSe28GtM5vMBu85WKgG1vMxd7wjcJHsUiuvxXMu9UCTDHZBIQ/7rd0xw==, tarball: file:projects/confidential-ledger.tgz} name: '@rush-temp/confidential-ledger' version: 0.0.0 dependencies: + '@azure-rest/core-client': 1.0.0-beta.10 '@azure-tools/test-recorder': 1.0.2 '@microsoft/api-extractor': 7.18.11 '@types/chai': 4.3.1 @@ -16072,10 +16087,11 @@ packages: dev: false file:projects/iot-device-update.tgz: - resolution: {integrity: sha512-YCfYc9EnUrtWmFFZjvasdYvGM2DSxE4smvj/eFSdBJVntn5mO2Ob6UgcAGD1W7g1DMSsuBIIcGy2Kqh5ZLB2eQ==, tarball: file:projects/iot-device-update.tgz} + resolution: {integrity: sha512-Own8y0R5dXpqe+cV79i9HZycHUveewzFKo0SH21ioVDnXkF1AsmofHi+cuQlA/b0HLsH8lHz7ZkdV0/mK3tNeQ==, tarball: file:projects/iot-device-update.tgz} name: '@rush-temp/iot-device-update' version: 0.0.0 dependencies: + '@azure-rest/core-client': 1.0.0-beta.10 '@azure/core-lro': 2.2.4 '@microsoft/api-extractor': 7.18.11 '@types/chai': 4.3.1 diff --git a/sdk/core/core-client-rest/CHANGELOG.md b/sdk/core/core-client-rest/CHANGELOG.md index 5b49e5b02c70..6dac335d14e2 100644 --- a/sdk/core/core-client-rest/CHANGELOG.md +++ b/sdk/core/core-client-rest/CHANGELOG.md @@ -1,5 +1,11 @@ # Release History +## 1.0.0-beta.11 (2022-07-07) + +### Fixes + +- Fix the duplicate `api-version` issue in apiVersionPolicy + ## 1.0.0-beta.10 (2022-06-07) ### Fixes diff --git a/sdk/core/core-client-rest/package.json b/sdk/core/core-client-rest/package.json index 921c508924bb..0660c2928fd2 100644 --- a/sdk/core/core-client-rest/package.json +++ b/sdk/core/core-client-rest/package.json @@ -1,6 +1,6 @@ { "name": "@azure-rest/core-client", - "version": "1.0.0-beta.10", + "version": "1.0.0-beta.11", "description": "Core library for interfacing with AutoRest rest level generated code", "sdk-type": "client", "main": "dist/index.js", diff --git a/sdk/core/core-client-rest/src/apiVersionPolicy.ts b/sdk/core/core-client-rest/src/apiVersionPolicy.ts index c64b493e31d4..455ba5245fb3 100644 --- a/sdk/core/core-client-rest/src/apiVersionPolicy.ts +++ b/sdk/core/core-client-rest/src/apiVersionPolicy.ts @@ -17,7 +17,8 @@ export function apiVersionPolicy(options: ClientOptions): PipelinePolicy { sendRequest: (req, next) => { if (options.apiVersion) { const url = new URL(req.url); - url.searchParams.append("api-version", options.apiVersion); + // add or replace the exiting api-version with client one + url.searchParams.set("api-version", options.apiVersion); req.url = url.toString(); } diff --git a/sdk/core/core-client-rest/test/getClient.spec.ts b/sdk/core/core-client-rest/test/getClient.spec.ts index d6113b78ba43..3cd16d9f3bb7 100644 --- a/sdk/core/core-client-rest/test/getClient.spec.ts +++ b/sdk/core/core-client-rest/test/getClient.spec.ts @@ -18,25 +18,48 @@ describe("getClient", () => { sinon.restore(); }); - it("should add apiVersion to requests", async () => { - const defaultHttpClient = getCachedDefaultHttpsClient(); - sinon.stub(defaultHttpClient, "sendRequest").callsFake(async (req) => { - return { headers: createHttpHeaders(), status: 200, request: req } as PipelineResponse; + describe("#apiVersionPolicy", () => { + it("should add apiVersion to requests if apiVersion is absent", async () => { + const defaultHttpClient = getCachedDefaultHttpsClient(); + sinon.stub(defaultHttpClient, "sendRequest").callsFake(async (req) => { + return { headers: createHttpHeaders(), status: 200, request: req } as PipelineResponse; + }); + + const apiVersion = "2021-11-18"; + const client = getClient("https://example.org", { apiVersion }); + const validationPolicy: PipelinePolicy = { + name: "validationPolicy", + sendRequest: (req, next) => { + assert.include(req.url, `api-version=${apiVersion}`); + return next(req); + }, + }; + + client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" }); + await client.pathUnchecked("/foo").get(); }); - const apiVersion = "2021-11-18"; - const client = getClient("https://example.org?api-version=1233321", { apiVersion }); - const validationPolicy: PipelinePolicy = { - name: "validationPolicy", - sendRequest: (req, next) => { - assert.include(req.url, `api-version=${apiVersion}`); - return next(req); - }, - }; + it("should replace existing apiVersion to requests with client api-version", async () => { + const defaultHttpClient = getCachedDefaultHttpsClient(); + sinon.stub(defaultHttpClient, "sendRequest").callsFake(async (req) => { + return { headers: createHttpHeaders(), status: 200, request: req } as PipelineResponse; + }); - client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" }); + const apiVersion = "2021-11-18"; + const client = getClient("https://example.org?api-version=1233321", { apiVersion }); + const validationPolicy: PipelinePolicy = { + name: "validationPolicy", + sendRequest: (req, next) => { + assert.include(req.url, `api-version=${apiVersion}`); + assert.notInclude(req.url, "api-version=1233321"); + return next(req); + }, + }; - await client.pathUnchecked("/foo").get(); + client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" }); + + await client.pathUnchecked("/foo").get(); + }); }); it("should insert policies in the correct pipeline position", async function () {