Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved aliased parameter parsing #3084

Merged
2 commits merged into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions packages/sp/spqueryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,11 @@ export class _SPQueryable<GetType = any> extends Queryable<GetType> {

const aliasedParams = new URLSearchParams(<any>this.query);

// this regex is designed to locate aliased parameters within url paths. These may have the form:
// /something(!@p1::value)
// /something(!@p1::value, param=value)
// /something(param=value,!@p1::value)
// /something(param=value,!@p1::value,param=value)
// /something(param=!@p1::value)
// there could be spaces or not around the boundaries
let url = this.toUrl().replace(/([( *| *, *| *= *])'!(@.*?)::(.*?)'([ *)| *, *])/ig, (match, frontBoundary, labelName, value, endBoundary) => {
// this regex is designed to locate aliased parameters within url paths
let url = this.toUrl().replace(/'!(@.+?)::((?:[^']|'')+)'/ig, (match, labelName, value) => {
this.log(`Rewriting aliased parameter from match ${match} to label: ${labelName} value: ${value}`, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to leave this logging in the final solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the this.log(`Rewriting aliased parameter from match... line?
It was already there, so I kept it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh ok, probably an oversight, we'll remove it.

aliasedParams.set(labelName, `'${value}'`);
return `${frontBoundary}${labelName}${endBoundary}`;
return labelName;
});

const query = aliasedParams.toString();
Expand Down
44 changes: 44 additions & 0 deletions test/sp/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "@pnp/sp/files/web";
import "@pnp/sp/files/folder";
import "@pnp/sp/lists/web";
import { combine } from "@pnp/core";
import { SPQueryable } from "@pnp/sp";

describe("Alias Parameters", function () {

Expand All @@ -29,6 +30,49 @@ describe("Alias Parameters", function () {
await list.rootFolder.files.addUsingPath("text.txt", "Some file content!");
});

it("Parameter parsing", function() {
/** Values to test */
const values = [
"value",
"value's",
"value with space",
"value with space' and apostrophe",
"ending with apostrophe'",
"'staring with apostrophe",
"'staring and ending with apostrophe'",
"with,' comma",
];
/** Aliased parameters to test */
const tests = values.reduce<Record<string, Record<string, string>>>((obj, value)=>{
// Escape apostrophe in value
value = value.replace(/'/g, "''");

obj[`something('!@p1::${value}')`] = {"@p1": `'${value}'`};
obj[`something('!@p1::${value}','!@p2::${value}2')`] = {"@p1": `'${value}'`, "@p2": `'${value}2'`};
obj[`something('!@p1::${value}', param=value)`] = {"@p1": `'${value}'`};
obj[`something('!@p1::${value}', param=value, '!@p2::${value}2')`] = {"@p1": `'${value}'`, "@p2": `'${value}2'`};
obj[`something(param=value,'!@p1::${value}')`] = {"@p1": `'${value}'`};
obj[`something(param=value,'!@p1::${value}','!@p2::${value}2')`] = {"@p1": `'${value}'`, "@p2": `'${value}2'`};
obj[`something(param=value,'!@p1::${value}',param=value)`] = {"@p1": `'${value}'`};
obj[`something(param=value,'!@p1::${value}',param=value,'!@p2::${value}2')`] = {"@p1": `'${value}'`, "@p2": `'${value}2'`};
obj[`something(param='!@p1::${value}')`] = {"@p1": `'${value}'`};
obj[`something(param='!@p1::${value}',param2='!@p2::${value}2')`] = {"@p1": `'${value}'`, "@p2": `'${value}2'`};
return obj;
}, {});

// Test all aliased parameters
for(const [alias, params] of Object.entries(tests)) {
const requestUrl = SPQueryable(this.pnp.sp.web, alias).toRequestUrl();
const searchParams = Object.fromEntries(new URL(requestUrl).searchParams.entries());

// eslint-disable-next-line guard-for-in
for(const param in params) {
expect(searchParams, `Failed to parse "${alias}"`).to.have.property(param);
expect(searchParams[param], `Failed to parse "${alias}"`).to.equal(params[param]);
}
}
});

it("Folders", function () {

return expect(this.pnp.sp.web.getFolderByServerRelativePath(`!@p1::/${combine(webRelativeUrl, "AliasTestLib/MyTestFolder")}`)()).to.eventually.be.fulfilled;
Expand Down