From 8c9661ae5611fbfa5d04e70e3bb179448ebd8c5f Mon Sep 17 00:00:00 2001 From: kelvin Date: Sun, 29 Sep 2024 16:17:34 +0800 Subject: [PATCH 1/3] fix(swagger): fix the parsing of enum types with array (#4078) --- .../src/decorators/api-property.decorator.ts | 2 +- packages/swagger/test/parser.test.ts | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/swagger/src/decorators/api-property.decorator.ts b/packages/swagger/src/decorators/api-property.decorator.ts index 4dce026e5be3..2808f14f3122 100644 --- a/packages/swagger/src/decorators/api-property.decorator.ts +++ b/packages/swagger/src/decorators/api-property.decorator.ts @@ -45,7 +45,7 @@ export function createApiPropertyDecorator( options.type = getEnumType(enumValues); } - if (isArray) { + if (options.type !== 'array' && isArray) { options.type = 'array'; options.items = { type: type as any, diff --git a/packages/swagger/test/parser.test.ts b/packages/swagger/test/parser.test.ts index 1c81acbe1a99..25f2822cc23c 100644 --- a/packages/swagger/test/parser.test.ts +++ b/packages/swagger/test/parser.test.ts @@ -2208,6 +2208,26 @@ describe('test property metadata parse', () => { additionalProperties: true }); }); + + it('should format enum type with array', () => { + enum Animal { + Cat = 0, + Dog = 1, + Pig = 2, + } + + const result = swaggerExplorer.formatType({ + type: 'enum', + enum: Animal, + isArray: true, + }); + + expect(result).toEqual({ + type: 'enum', + isArray: true, + enum: [0, 1, 2], + }); + }) }); describe('test @ApiOperation', () => { From ed29679466e7f8124be8a98f573bc081f93948bf Mon Sep 17 00:00:00 2001 From: kelvin Date: Sun, 29 Sep 2024 18:05:15 +0800 Subject: [PATCH 2/3] fix(swagger): test --- .../test/__snapshots__/parser.test.ts.snap | 20 ++++++++++ packages/swagger/test/parser.test.ts | 40 +++++++++---------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/packages/swagger/test/__snapshots__/parser.test.ts.snap b/packages/swagger/test/__snapshots__/parser.test.ts.snap index 7cb4676488db..9361c7148c26 100644 --- a/packages/swagger/test/__snapshots__/parser.test.ts.snap +++ b/packages/swagger/test/__snapshots__/parser.test.ts.snap @@ -1350,6 +1350,26 @@ exports[`test @ApiParam should test with @Param decorator 1`] = ` } `; +exports[`test @ApiProperty should format enum type with array 1`] = ` +{ + "properties": { + "animal": { + "isArray": true, + "items": { + "enum": [ + 0, + 1, + 2, + ], + "type": "number", + }, + "type": "array", + }, + }, + "type": "object", +} +`; + exports[`test @ApiProperty should parse base type 1`] = ` { "properties": { diff --git a/packages/swagger/test/parser.test.ts b/packages/swagger/test/parser.test.ts index 25f2822cc23c..48bd4d813a04 100644 --- a/packages/swagger/test/parser.test.ts +++ b/packages/swagger/test/parser.test.ts @@ -1776,6 +1776,26 @@ describe('test @ApiProperty', () => { const explorer = new CustomSwaggerExplorer(); expect(explorer.parse(Cat)).toMatchSnapshot(); }); + + it('should format enum type with array', () => { + enum Animal { + Cat = 0, + Dog = 1, + Pig = 2, + } + + class Dto { + @ApiProperty({ + type: 'enum', + enum: Animal, + isArray: true, + }) + animal: Animal[]; + } + + const explorer = new CustomSwaggerExplorer(); + expect(explorer.parse(Dto)).toMatchSnapshot(); + }) }); describe('test property metadata parse', () => { @@ -2208,26 +2228,6 @@ describe('test property metadata parse', () => { additionalProperties: true }); }); - - it('should format enum type with array', () => { - enum Animal { - Cat = 0, - Dog = 1, - Pig = 2, - } - - const result = swaggerExplorer.formatType({ - type: 'enum', - enum: Animal, - isArray: true, - }); - - expect(result).toEqual({ - type: 'enum', - isArray: true, - enum: [0, 1, 2], - }); - }) }); describe('test @ApiOperation', () => { From 2366c9472489383aa5b60c170a50922a3ea04535 Mon Sep 17 00:00:00 2001 From: kelvin Date: Sun, 29 Sep 2024 18:24:48 +0800 Subject: [PATCH 3/3] fix(swagger): test --- packages/swagger/test/parser.test.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/swagger/test/parser.test.ts b/packages/swagger/test/parser.test.ts index 48bd4d813a04..89d034d29e4f 100644 --- a/packages/swagger/test/parser.test.ts +++ b/packages/swagger/test/parser.test.ts @@ -1794,7 +1794,25 @@ describe('test @ApiProperty', () => { } const explorer = new CustomSwaggerExplorer(); - expect(explorer.parse(Dto)).toMatchSnapshot(); + const result = explorer.parse(Dto) + // expect(explorer.parse(Dto)).toMatchSnapshot(); + expect(result).toEqual({ + properties: { + animal: { + isArray: true, + items: { + enum: [ + 0, + 1, + 2, + ], + type: 'number', + }, + type: 'array', + }, + }, + type: 'object', + }) }) });