From a804bf45092d2c57f015d2ffd98bc49b65865cda Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Mon, 27 Feb 2023 14:07:11 +0100 Subject: [PATCH 1/2] Add test case for ParseArrayPipe that doesn't work as expected --- packages/common/test/pipes/parse-array.pipe.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/test/pipes/parse-array.pipe.spec.ts b/packages/common/test/pipes/parse-array.pipe.spec.ts index ad5aca0fdab..092f1945f90 100644 --- a/packages/common/test/pipes/parse-array.pipe.spec.ts +++ b/packages/common/test/pipes/parse-array.pipe.spec.ts @@ -176,8 +176,8 @@ describe('ParseArrayPipe', () => { target = new ParseArrayPipe({ items: String }); expect( - await target.transform('1,2,3', {} as ArgumentMetadata), - ).to.deep.equal(['1', '2', '3']); + await target.transform('1,2.0,3', {} as ArgumentMetadata), + ).to.deep.equal(['1', '2.0', '3']); target = new ParseArrayPipe({ items: Boolean }); expect( From 96afa8e650647c18b7a7285f2eb42e1784e5a60c Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Thu, 9 Mar 2023 10:40:21 +0100 Subject: [PATCH 2/2] fix: disable JSON parse for strings in ParseArrayPipe --- packages/common/pipes/parse-array.pipe.ts | 9 +++++---- .../common/test/pipes/parse-array.pipe.spec.ts | 16 +++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/common/pipes/parse-array.pipe.ts b/packages/common/pipes/parse-array.pipe.ts index 60d6e09eb24..a46acc8fdf0 100644 --- a/packages/common/pipes/parse-array.pipe.ts +++ b/packages/common/pipes/parse-array.pipe.ts @@ -88,10 +88,11 @@ export class ParseArrayPipe implements PipeTransform { const isExpectedTypePrimitive = this.isExpectedTypePrimitive(); const toClassInstance = (item: any, index?: number) => { - try { - item = JSON.parse(item); - } catch {} - + if (this.options.items !== String) { + try { + item = JSON.parse(item); + } catch {} + } if (isExpectedTypePrimitive) { return this.validatePrimitive(item, index); } diff --git a/packages/common/test/pipes/parse-array.pipe.spec.ts b/packages/common/test/pipes/parse-array.pipe.spec.ts index 092f1945f90..096ab3f9bca 100644 --- a/packages/common/test/pipes/parse-array.pipe.spec.ts +++ b/packages/common/test/pipes/parse-array.pipe.spec.ts @@ -78,8 +78,11 @@ describe('ParseArrayPipe', () => { target = new ParseArrayPipe(); expect( - await target.transform('1,2,3', {} as ArgumentMetadata), - ).to.be.deep.equal(['1', '2', '3']); + await target.transform( + '1,2.0,3,{},true,null,,', + {} as ArgumentMetadata, + ), + ).to.be.deep.equal(['1', '2.0', '3', '{}', 'true', 'null', '', '']); target = new ParseArrayPipe({ separator: '/' }); @@ -171,13 +174,16 @@ describe('ParseArrayPipe', () => { target = new ParseArrayPipe({ items: Number }); expect( - await target.transform('1,2,3', {} as ArgumentMetadata), + await target.transform('1,2.0,3', {} as ArgumentMetadata), ).to.deep.equal([1, 2, 3]); target = new ParseArrayPipe({ items: String }); expect( - await target.transform('1,2.0,3', {} as ArgumentMetadata), - ).to.deep.equal(['1', '2.0', '3']); + await target.transform( + '1,2.0,3,{},true,null,,', + {} as ArgumentMetadata, + ), + ).to.deep.equal(['1', '2.0', '3', '{}', 'true', 'null', '', '']); target = new ParseArrayPipe({ items: Boolean }); expect(