Skip to content

Commit

Permalink
fix(success response handler): parse unsafe integers as strings (#834)
Browse files Browse the repository at this point in the history
* fix(success response handler): parse unsafe integers as strings

* chore: add issue number to todo comment
  • Loading branch information
fbeaudoincoveo authored Jun 20, 2024
1 parent 2d5e19f commit 4727150
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
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') => {
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;
}
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

0 comments on commit 4727150

Please sign in to comment.