From 3c748a2c7f2ea099c28f53ff223b47c7c78d9763 Mon Sep 17 00:00:00 2001 From: linchen1987 Date: Tue, 26 Sep 2023 11:49:10 +0800 Subject: [PATCH] fix: gql request crashs when formatting scalar args (#173) --- CHANGELOG.md | 3 +++ packages/sdk-util/src/util.js | 2 +- packages/sdk-util/test/util.spec.js | 15 +++++++++++++++ version | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6dacc..98543ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.34.3 (September 26, 2023) +- fix: gql request crashes when formatting scalar args + ## 0.34.2 (September 26, 2023) - fix: gql request crashes when formatting scalar args diff --git a/packages/sdk-util/src/util.js b/packages/sdk-util/src/util.js index 5d1ba40..18bb0fa 100644 --- a/packages/sdk-util/src/util.js +++ b/packages/sdk-util/src/util.js @@ -190,7 +190,7 @@ const formatArgs = (values, specs = {}) => { // escape slash(\) and double quotes (") if ('String' === type) { - const container = isNull(value) ? null : (value || '').includes('\n') ? '"""' : '"'; + const container = isNull(value) ? null : String(value).includes('\n') ? '"""' : '"'; return isNull(value) ? null : `${container}${value .toString() diff --git a/packages/sdk-util/test/util.spec.js b/packages/sdk-util/test/util.spec.js index ead0920..48f26ea 100644 --- a/packages/sdk-util/test/util.spec.js +++ b/packages/sdk-util/test/util.spec.js @@ -107,6 +107,21 @@ describe('#formatArgs', () => { expect(args).toEqual('address: "xxx", height: 123'); }); + test('should process scalar types correctly (has break line)', () => { + const args = formatArgs({ address: 'ab\ncd', height: 123 }, specs); + expect(args).toEqual('address: """ab\ncd""", height: 123'); + }); + + test('should process scalar types correctly (object in string)', () => { + const args = formatArgs({ address: { name: 'abc' }, height: 123 }, specs); + expect(args).toEqual('address: "[object Object]", height: 123'); + }); + + test('should process scalar types correctly (null)', () => { + const args = formatArgs({ address: null, height: 123 }, specs); + expect(args).toEqual('address: null, height: 123'); + }); + test('should process null scalar values correctly', () => { const args = formatArgs({ address: null, height: 123 }, specs); expect(args).toEqual('address: null, height: 123'); diff --git a/version b/version index 3f8003c..0aeaf41 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.34.2 +0.34.3