Skip to content

Commit

Permalink
fix: some strings in request body cast to another type (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba authored Nov 9, 2016
1 parent dea0835 commit edb4635
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/packages/server/request/parser/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
export const INT = /^\d+$/;
export const BOOL = /^(true|false)$/;
export const NULL = /^null$/;
export const BOOL = /^(?:true|false)$/;
export const DATE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(Z|\+\d{4})$/;
export const TRUE = /^true$/;
export const BRACKETS = /(\[])/g;
export const BRACKETS = /(?:\[])/g;
22 changes: 14 additions & 8 deletions src/packages/server/request/parser/utils/format.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import { camelize } from 'inflection';

import { INT, BOOL, DATE, TRUE, BRACKETS } from '../constants';
import { INT, NULL, BOOL, DATE, TRUE, BRACKETS } from '../constants';
import isNull from '../../../../../utils/is-null';
import entries from '../../../../../utils/entries';
import underscore from '../../../../../utils/underscore';
Expand All @@ -23,13 +23,19 @@ function makeArray(source: string | Array<string>): Array<string> {
* @private
*/
function formatString(source: string, method: Request$method): mixed {
if (method === 'GET' && source.indexOf(',') >= 0) {
return source.split(',').map(str => camelize(underscore(str), true));
} else if (INT.test(source)) {
return Number.parseInt(source, 10);
} else if (BOOL.test(source)) {
return TRUE.test(source);
} else if (DATE.test(source)) {
if (method === 'GET') {
if (source.indexOf(',') >= 0) {
return source.split(',').map(str => camelize(underscore(str), true));
} else if (INT.test(source)) {
return Number.parseInt(source, 10);
} else if (BOOL.test(source)) {
return TRUE.test(source);
} else if (NULL.test(source)) {
return null;
}
}

if (DATE.test(source)) {
return new Date(source);
}

Expand Down
50 changes: 44 additions & 6 deletions src/packages/server/request/test/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ describe('module "server/request"', () => {

describe('#parseRequest()', () => {
it('can parse params from a GET request', () => {
const now = new Date().toISOString();
const url = '/posts?'
+ 'fields[posts]=body,title'
+ '&fields[users]=name'
+ '&include=user';
+ '&include=user'
+ '&filter[is-public]=true'
+ '&filter[title]=123'
+ '&filter[body]=null'
+ `&filter[created-at]=${now}`;

return test(url, {
method: 'GET'
Expand All @@ -101,20 +106,39 @@ describe('module "server/request"', () => {

expect(params).to.deep.equal({
fields: ['body', 'title'],
include: ['user']
include: ['user'],
filter: {
body: null,
title: 123,
isPublic: true
}
});

expect(params)
.to.have.deep.property('filter.createdAt')
.and.be.an.instanceOf(Date);

expect(
params.filter.createdAt.valueOf()
).to.equal(new Date(now).valueOf());
});
});

it('can parse params from a POST request', () => {
const now = new Date().toISOString();

return test('/posts?include=user', {
method: 'POST',
body: {
data: {
type: 'posts',
attributes: {
title: 'New Post 1',
'is-public': true
'is-public': true,
intString: '123',
nullString: 'null',
boolString: 'true',
dateString: now
},
relationships: {
user: {
Expand Down Expand Up @@ -153,7 +177,11 @@ describe('module "server/request"', () => {
type: 'posts',
attributes: {
title: 'New Post 1',
isPublic: true
isPublic: true,
intString: '123',
nullString: 'null',
boolString: 'true',
dateString: now
},
relationships: {
user: {
Expand Down Expand Up @@ -186,14 +214,20 @@ describe('module "server/request"', () => {
});

it('can parse params from a PATCH request', () => {
const now = new Date().toISOString();

return test('/posts/1?include=user', {
method: 'PATCH',
data: {
id: 1,
type: 'posts',
attributes: {
title: 'New Post 1',
'is-public': true
'is-public': true,
intString: '123',
nullString: 'null',
boolString: 'true',
dateString: now
},
relationships: {
user: {
Expand Down Expand Up @@ -231,7 +265,11 @@ describe('module "server/request"', () => {
type: 'posts',
attributes: {
title: 'New Post 1',
isPublic: true
isPublic: true,
intString: '123',
nullString: 'null',
boolString: 'true',
dateString: now
},
relationships: {
user: {
Expand Down

0 comments on commit edb4635

Please sign in to comment.