Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swagger): fix the parsing of enum types with array (#4078) #4098

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/swagger/src/decorators/api-property.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions packages/swagger/test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
38 changes: 38 additions & 0 deletions packages/swagger/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,44 @@ 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();
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',
})
})
});

describe('test property metadata parse', () => {
Expand Down
Loading