-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: decodePointer does not handle percent character (#83)
- Loading branch information
Showing
3 changed files
with
23 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
import { replaceInString } from './_utils'; | ||
|
||
function safeDecodeURIComponent(value: string): string { | ||
try { | ||
return decodeURIComponent(value); | ||
} catch { | ||
return value; | ||
} | ||
} | ||
|
||
const PERCENT_ENCODING_OCTET = /%[0-9a-f]+/gi; | ||
|
||
/** | ||
* Removes special json pointer characters in a value. Example: | ||
* | ||
* decodePointer('#/paths/~1users) => '#/paths//users' | ||
*/ | ||
export const decodePointer = (value: string): string => { | ||
return replaceInString(replaceInString(decodeURIComponent('' + value), '~1', '/'), '~0', '~'); | ||
let decoded; | ||
try { | ||
decoded = decodeURIComponent(value); | ||
} catch { | ||
decoded = value.replace(PERCENT_ENCODING_OCTET, safeDecodeURIComponent); | ||
} | ||
|
||
return replaceInString(replaceInString(decoded, '~1', '/'), '~0', '~'); | ||
}; |