Skip to content

Commit

Permalink
Do not append customized api-version if url already has one e.g LRO/p… (
Browse files Browse the repository at this point in the history
Azure#22423)

* 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
  • Loading branch information
MaryGao authored Jul 15, 2022
1 parent 06b2f31 commit ff143a1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 24 deletions.
30 changes: 23 additions & 7 deletions common/config/rush/pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions sdk/core/core-client-rest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/core-client-rest/src/apiVersionPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
53 changes: 38 additions & 15 deletions sdk/core/core-client-rest/test/getClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit ff143a1

Please sign in to comment.