Ignore parameters with pre-filled JSON values

Summary:
https://fb.workplace.com/groups/flippersupport/permalink/1513232162490770/

Apparently, some apps use JSONs as values for their params

Reviewed By: lblasa

Differential Revision: D41684474

fbshipit-source-id: 4ab19313ede06b226715a5c398728f0be7feddaf
This commit is contained in:
Andrey Goncharov
2022-12-05 04:14:34 -08:00
committed by Facebook GitHub Bot
parent 904e530f74
commit 73e7298525
2 changed files with 18 additions and 2 deletions

View File

@@ -28,6 +28,14 @@ test('parse required numeric parameters from uri', () => {
expect(getRequiredParameters(testURI)).toEqual(expectedResult);
});
// https://fb.workplace.com/groups/flippersupport/permalink/1513232162490770/
test('ignore params with JSON values', () => {
const testURI =
'fb://test_uri/?parameter1={"test":"value"}&parameter2="{\\"test\\":\\"value\\"}"';
const expectedResult: string[] = [];
expect(getRequiredParameters(testURI)).toEqual(expectedResult);
});
test('replace required parameters with values', () => {
const testURI =
'fb://test_uri/?parameter1={parameter1}&parameter2={parameter2}';

View File

@@ -60,12 +60,20 @@ export const replaceRequiredParametersWithValues = (
};
export const getRequiredParameters = (uri: string) => {
const parameterRegExp = /{[^?]*?}/g;
// Add = to the matching group to filter out stringified JSON parameters
const parameterRegExp = /={[^?]*?}/g;
const matches: Array<string> = [];
let match = parameterRegExp.exec(uri);
while (match != null) {
if (match[0]) {
matches.push(match[0]);
// Remove = from the match
const target = match[0].substring(1);
try {
// If the value could be parsed asa valid JSON, ignore it
JSON.parse(target);
} catch {
matches.push(target);
}
}
match = parameterRegExp.exec(uri);
}