Skip to content

Commit 563f374

Browse files
focusaurusnodkz
authored andcommitted
fix(ElasticApiParser): Valid number encoded as string gets parsed and retained
1 parent f3b2b3c commit 563f374

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/ElasticApiParser.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,15 @@ export default class ElasticApiParser {
323323

324324
if (paramCfg.default) {
325325
result.defaultValue = paramCfg.default;
326-
// Handle broken data where default is not valid for the given type
327-
// https://github.com/graphql-compose/graphql-compose-elasticsearch/issues/49
328-
if (result.type === 'Float' && Number.isNaN(Number(paramCfg.default))) {
329-
delete result.defaultValue;
326+
if (result.type === 'Float') {
327+
const defaultNumber = Number(result.defaultValue);
328+
if (Number.isNaN(defaultNumber)) {
329+
// Handle broken data where default is not valid for the given type
330+
// https://github.com/graphql-compose/graphql-compose-elasticsearch/issues/49
331+
delete result.defaultValue;
332+
} else {
333+
result.defaultValue = defaultNumber;
334+
}
330335
}
331336
} else if (fieldName === 'format') {
332337
result.defaultValue = 'json';

src/__tests__/ElasticApiParser-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ describe('ElasticApiParser', () => {
383383
parser.paramToGraphQLArgConfig({ type: 'number', default: 'ABC' }, 'someField')
384384
).not.toHaveProperty('defaultValue');
385385
});
386+
387+
it('should accept numeric default for float', () => {
388+
expect(
389+
parser.paramToGraphQLArgConfig({ type: 'number', default: '4.2' }, 'someField')
390+
).toHaveProperty('defaultValue', 4.2);
391+
});
386392
});
387393

388394
describe('settingsToArgMap()', () => {

0 commit comments

Comments
 (0)