Skip to content

Commit

Permalink
fix(ElasticApiParser): Valid number encoded as string gets parsed and…
Browse files Browse the repository at this point in the history
… retained
  • Loading branch information
focusaurus authored and nodkz committed Dec 11, 2018
1 parent f3b2b3c commit 563f374
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ElasticApiParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,15 @@ export default class ElasticApiParser {

if (paramCfg.default) {
result.defaultValue = paramCfg.default;
// Handle broken data where default is not valid for the given type
// https://github.com/graphql-compose/graphql-compose-elasticsearch/issues/49
if (result.type === 'Float' && Number.isNaN(Number(paramCfg.default))) {
delete result.defaultValue;
if (result.type === 'Float') {
const defaultNumber = Number(result.defaultValue);
if (Number.isNaN(defaultNumber)) {
// Handle broken data where default is not valid for the given type
// https://github.com/graphql-compose/graphql-compose-elasticsearch/issues/49
delete result.defaultValue;
} else {
result.defaultValue = defaultNumber;
}
}
} else if (fieldName === 'format') {
result.defaultValue = 'json';
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/ElasticApiParser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ describe('ElasticApiParser', () => {
parser.paramToGraphQLArgConfig({ type: 'number', default: 'ABC' }, 'someField')
).not.toHaveProperty('defaultValue');
});

it('should accept numeric default for float', () => {
expect(
parser.paramToGraphQLArgConfig({ type: 'number', default: '4.2' }, 'someField')
).toHaveProperty('defaultValue', 4.2);
});
});

describe('settingsToArgMap()', () => {
Expand Down

0 comments on commit 563f374

Please sign in to comment.