Skip to content

Commit 6e890cc

Browse files
committed
Changed parsepath and query functions to split query params on first equals sign considering nested queries. Added comments to query functions
1 parent 870477b commit 6e890cc

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

spec/core/urlGeneration.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ cases.push({
9191
.query("$search=senior"),
9292
});
9393

94+
cases.push({
95+
url: "https://graph.microsoft.com/beta/me/people?$select=displayName,title,id&$count=false&$expand=a($expand=a,b)",
96+
request: client
97+
.api("/me/people")
98+
.version("beta")
99+
.select(["displayName", "title"])
100+
.count(true)
101+
.expand("a($expand=a,b)")
102+
.query("$select=id")
103+
.query("$count=false"),
104+
});
105+
106+
cases.push({
107+
url: "https://graph.microsoft.com/v1.0/me/people?$select=displayName,title,id&select=value",
108+
request: client
109+
.api("/me/people")
110+
.version("v1.0")
111+
.select(["displayName", "title"])
112+
.query({ select: "value" })
113+
.query({ $select: "id" }),
114+
});
115+
94116
cases.push({
95117
url: "https://graph.microsoft.com/v1.0/me/drive/root?$expand=children($select=name),permissions",
96118
request: client

spec/core/urlParsing.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const testCases = {
2828
"me?$select=displayName": "https://graph.microsoft.com/v1.0/me?$select=displayName",
2929
"me?select=displayName": "https://graph.microsoft.com/v1.0/me?select=displayName",
3030
"https://graph.microsoft.com/beta/me?select=displayName": "https://graph.microsoft.com/beta/me?select=displayName",
31+
32+
// test for nested query parameters
33+
"https://graph.microsoft.com/beta/identityGovernance/entitlementManagement/accessPackages/?$expand=accessPackageAssignmentPolicies,accessPackageResourceRoleScopes($expand=accessPackageResourceRole,accessPackageResourceScope)": "https://graph.microsoft.com/beta/identityGovernance/entitlementManagement/accessPackages/?$expand=accessPackageAssignmentPolicies,accessPackageResourceRoleScopes($expand=accessPackageResourceRole,accessPackageResourceScope)",
34+
"me?$select=displayName&$select=id": "https://graph.microsoft.com/v1.0/me?$select=displayName,id",
35+
"/me?$filter=b&$filter=a": "https://graph.microsoft.com/v1.0/me?$filter=a",
36+
"https://graph.microsoft.com/v1.0/me?$top=4&$expand=4&$iscount=true&$top=2": "https://graph.microsoft.com/v1.0/me?$top=2&$expand=4&$iscount=true",
3137
};
3238

3339
describe("urlParsing.ts", () => {
@@ -42,5 +48,5 @@ describe("urlParsing.ts", () => {
4248
}
4349
}
4450
});
45-
/* tslint:enable: no-string-literal */
4651
});
52+
/* tslint:enable: no-string-literal */

src/GraphRequest.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,7 @@ export class GraphRequest {
170170
// Capture query string into oDataQueryParams and otherURLQueryParams
171171
const queryParams = path.substring(queryStrPos + 1, path.length).split("&");
172172
for (const queryParam of queryParams) {
173-
const qParams = queryParam.split("=");
174-
const key = qParams[0];
175-
const value = qParams[1];
176-
if (oDataQueryNames.indexOf(key) !== -1) {
177-
this.urlComponents.oDataQueryParams[key] = value;
178-
} else {
179-
this.urlComponents.otherURLQueryParams[key] = value;
180-
}
173+
this.query(queryParam);
181174
}
182175
}
183176
};
@@ -430,7 +423,7 @@ export class GraphRequest {
430423

431424
/**
432425
* @public
433-
* To add query string for filter OData Query param
426+
* To add query string for filter OData Query param. The request URL accepts only one $filter Odata Query option and it's value is set to the most recently passed filter query string.
434427
* @param {string} filterStr - The filter query string
435428
* @returns The same GraphRequest instance that is being called with
436429
*/
@@ -441,7 +434,7 @@ export class GraphRequest {
441434

442435
/**
443436
* @public
444-
* To add criterion for search OData Query param
437+
* To add criterion for search OData Query param. The request URL accepts only one $search Odata Query option and it's value is set to the most recently passed search criterion string.
445438
* @param {string} searchStr - The search criterion string
446439
* @returns The same GraphRequest instance that is being called with
447440
*/
@@ -452,7 +445,7 @@ export class GraphRequest {
452445

453446
/**
454447
* @public
455-
* To add number for top OData Query param
448+
* To add number for top OData Query param. The request URL accepts only one $top Odata Query option and it's value is set to the most recently passed number value.
456449
* @param {number} n - The number value
457450
* @returns The same GraphRequest instance that is being called with
458451
*/
@@ -463,7 +456,7 @@ export class GraphRequest {
463456

464457
/**
465458
* @public
466-
* To add number for skip OData Query param
459+
* To add number for skip OData Query param. The request URL accepts only one $skip Odata Query option and it's value is set to the most recently passed number value.
467460
* @param {number} n - The number value
468461
* @returns The same GraphRequest instance that is being called with
469462
*/
@@ -474,7 +467,7 @@ export class GraphRequest {
474467

475468
/**
476469
* @public
477-
* To add token string for skipToken OData Query param
470+
* To add token string for skipToken OData Query param. The request URL accepts only one $skipToken Odata Query option and it's value is set to the most recently passed token value.
478471
* @param {string} token - The token value
479472
* @returns The same GraphRequest instance that is being called with
480473
*/
@@ -485,7 +478,7 @@ export class GraphRequest {
485478

486479
/**
487480
* @public
488-
* To add boolean for count OData Query param
481+
* To add boolean for count OData Query param. The URL accepts only one $count Odata Query option and it's value is set to the most recently passed boolean value.
489482
* @param {boolean} isCount - The count boolean
490483
* @returns The same GraphRequest instance that is being called with
491484
*/
@@ -501,19 +494,26 @@ export class GraphRequest {
501494
* @returns The same GraphRequest instance that is being called with
502495
*/
503496
public query(queryDictionaryOrString: string | KeyValuePairObjectStringNumber): GraphRequest {
504-
const otherURLQueryParams = this.urlComponents.otherURLQueryParams;
497+
let paramKey: string;
498+
let paramValue: string | number;
505499
if (typeof queryDictionaryOrString === "string") {
506-
const querySplit = queryDictionaryOrString.split("=");
507-
const queryKey = querySplit[0];
508-
const queryValue = querySplit[1];
509-
otherURLQueryParams[queryKey] = queryValue;
500+
const indexOfFirstEquals = queryDictionaryOrString.indexOf("="); // The query key-value pair must be split on the first equals sign to avoid error while parsing nested query parameters
501+
const qParams = [queryDictionaryOrString.substring(0, indexOfFirstEquals), queryDictionaryOrString.substring(indexOfFirstEquals + 1, queryDictionaryOrString.length)];
502+
paramKey = qParams[0];
503+
paramValue = qParams[1];
510504
} else {
511505
for (const key in queryDictionaryOrString) {
512506
if (queryDictionaryOrString.hasOwnProperty(key)) {
513-
otherURLQueryParams[key] = queryDictionaryOrString[key];
507+
paramKey = key;
508+
paramValue = queryDictionaryOrString[key];
514509
}
515510
}
516511
}
512+
if (oDataQueryNames.indexOf(paramKey) !== -1) {
513+
this.urlComponents.oDataQueryParams[paramKey] = paramKey === "$expand" || paramKey === "$select" || paramKey === "$orderby" ? (this.urlComponents.oDataQueryParams[paramKey] ? this.urlComponents.oDataQueryParams[paramKey] + "," + paramValue : paramValue) : paramValue;
514+
} else {
515+
this.urlComponents.otherURLQueryParams[paramKey] = paramValue;
516+
}
517517
return this;
518518
}
519519

0 commit comments

Comments
 (0)