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(success response handler): parse unsafe integers as strings #834

Merged
merged 2 commits into from
Jun 20, 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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
}
},
"dependencies": {
"core-js": "^3.37.1",
"exponential-backoff": "^3.1.0",
"query-string-cjs": "npm:query-string@^7.0.0",
"query-string-esm": "npm:query-string@^9.0.0"
Expand Down
20 changes: 19 additions & 1 deletion src/handlers/response/ResponseHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO CDX-1574: remove this polyfill when we bump the minimal supported Node.js version to 22
import 'core-js/actual/json/parse.js';
import {Predicate} from '../../utils/types.js';
import {ResponseBodyFormat, ResponseHandler} from './ResponseHandlerInterfaces.js';

Expand All @@ -14,7 +16,23 @@ const noContent: ResponseHandler = {

const success: ResponseHandler = {
canProcess: isAnyOkStatus,
process: async (response, responseBodyFormat = 'json') => response[responseBodyFormat](),
process: async (response, responseBodyFormat = 'json') => {
toofff marked this conversation as resolved.
Show resolved Hide resolved
if (responseBodyFormat !== 'json') {
return response[responseBodyFormat]();
}
const content = await response.text();
return (
JSON.parse as (
text: string,
reviver?: (key: string, value: any, context: {source: any}) => any | undefined,
) => any
)(content, (_key, value, context) => {
if (typeof value === 'number' && !Number.isSafeInteger(Math.floor(value))) {
return context.source;
fbeaudoincoveo marked this conversation as resolved.
Show resolved Hide resolved
}
return value;
});
},
};

/**
Expand Down
40 changes: 35 additions & 5 deletions src/handlers/response/tests/ResponseHandlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,41 @@ describe('ResponseHandlers', () => {
});

describe.each([200, 299])('when the response status code is between 200 and 299 (success)', (status) => {
it(`${status} returns a promise resolved with the response body in JSON format`, async () => {
const data = {someData: 'thank you!'};
const okResponse = new Response(JSON.stringify(data), {status});
const result = await handleResponse(okResponse);
expect(result).toEqual(data);
describe('if responseBodyFormat = "json", or by default', () => {
it(`${status} returns a promise resolved with the response body in JSON format`, async () => {
const data = {someData: 'thank you!'};
const okResponse = new Response(JSON.stringify(data), {status});
const result = await handleResponse(okResponse);
expect(result).toEqual(data);
});

it(`${status} parses unsafe integer as a string`, async () => {
const data = '{"unsafeInteger": 9999999999999999}';
const okResponse = new Response(data, {status});
const result = await handleResponse(okResponse);
expect(result).toEqual({unsafeInteger: '9999999999999999'});
});

it(`${status} parses safe integer as an integer`, async () => {
const data = '{"safeInteger": 999999999999999}';
const okResponse = new Response(data, {status});
const result = await handleResponse(okResponse);
expect(result).toEqual({safeInteger: 999999999999999});
});

it(`${status} parses float whose integer part is unsafe as a string`, async () => {
const data = '{"float": 9999999999999999.9}';
const okResponse = new Response(data, {status});
const result = await handleResponse(okResponse);
expect(result).toEqual({float: '9999999999999999.9'});
});

it(`${status} parses float whose integer part is safe as a number`, async () => {
const data = '{"float": 999999999999999.9}';
const okResponse = new Response(data, {status});
const result = await handleResponse(okResponse);
expect(result).toEqual({float: 999999999999999.9});
});
});

it(`${status} returns a promise resolved with the response body in text format if responseBodyFormat = "text"`, async () => {
Expand Down