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

fix(util-dynamodb): state options.wrapNumbers on BigInt error in unmarshall #2015

Merged
merged 2 commits into from
Feb 10, 2021
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
8 changes: 8 additions & 0 deletions packages/util-dynamodb/src/convertToNative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ describe("convertToNative", () => {
);
});
});

[`${Number.MAX_SAFE_INTEGER + 2}.1`, `${Number.MIN_SAFE_INTEGER - 2}.1`].forEach((numString) => {
it(`throws if number cannot be converted into BigInt: ${numString}`, () => {
expect(() => {
convertToNative({ N: numString });
}).toThrowError(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
});
});
});

describe("binary", () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/util-dynamodb/src/convertToNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const convertNumber = (numString: string, options?: unmarshallOptions): number |
const infinityValues = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
if ((num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num)) {
if (typeof BigInt === "function") {
return BigInt(numString);
try {
return BigInt(numString);
} catch (error) {
throw new Error(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
}
} else {
throw new Error(`${numString} is outside SAFE_INTEGER bounds. Set options.wrapNumbers to get string value.`);
}
Expand Down