Skip to content

Commit

Permalink
feat: add function for enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
slukes committed Apr 29, 2024
1 parent d5591ec commit afc47a7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/decorators/api-property.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createPropertyDecorator, getTypeIsArrayTuple } from './helpers';
export interface ApiPropertyOptions
extends Omit<SchemaObjectMetadata, 'name' | 'enum'> {
name?: string;
enum?: any[] | Record<string, any>;
enum?: any[] | Record<string, any> | (() => (any[] | Record<string, any>));
enumName?: string;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/utils/enum.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { SchemaObject } from '../interfaces/open-api-spec.interface';
import { SchemaObjectMetadata } from '../interfaces/schema-object-metadata.interface';
import { SwaggerEnumType } from '../types/swagger-enum.type';

export function getEnumValues(enumType: SwaggerEnumType): string[] | number[] {
export function getEnumValues(enumType: SwaggerEnumType | (() => SwaggerEnumType)): string[] | number[] {
if (typeof enumType === 'function') {
return getEnumValues(enumType());
}

if (Array.isArray(enumType)) {
return enumType as string[];
}
Expand Down
16 changes: 15 additions & 1 deletion test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ describe('SwaggerExplorer', () => {
isArray: true
})
enumArr: LettersEnum;

@ApiProperty({
enum: () => LettersEnum,
enumName: 'LettersEnum',
})
enumFunction: LettersEnum;
}

@Controller('')
Expand Down Expand Up @@ -229,7 +235,7 @@ describe('SwaggerExplorer', () => {
expect(routes[0].root!.method).toEqual('post');
expect(routes[0].root!.path).toEqual('/globalPrefix/modulePath/foos');
expect(routes[0].root!.summary).toEqual('Create foo');
expect(routes[0].root!.parameters.length).toEqual(5);
expect(routes[0].root!.parameters.length).toEqual(6);
expect(routes[0].root!.parameters).toEqual([
{
in: 'query',
Expand Down Expand Up @@ -278,6 +284,14 @@ describe('SwaggerExplorer', () => {
},
type: 'array'
}
},
{
in: 'query',
name: 'enumFunction',
required: true,
schema: {
$ref: '#/components/schemas/LettersEnum'
}
}
]);
expect(routes[0].root!.requestBody).toEqual({
Expand Down
31 changes: 28 additions & 3 deletions test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ describe('SchemaObjectFactory', () => {
Third = 3
}

enum HairColour {
Brown = 'Brown',
Blond = 'Blond',
Ginger = 'Ginger',
}

class CreatePersonDto {
@ApiProperty()
name: string;
Expand All @@ -57,13 +63,19 @@ describe('SchemaObjectFactory', () => {

@ApiProperty({ enum: Ranking, enumName: 'Ranking', isArray: true })
rankings: Ranking[];

@ApiProperty({ enum: () => HairColour, enumName: 'HairColour' })
hairColour: HairColour;

@ApiProperty({ enum: () => ['Pizza', 'Burger', 'Salad'], enumName: 'Food', isArray: true })
favouriteFoods: string[];
}

it('should explore enum', () => {
const schemas: Record<string, SchemasObject> = {};
schemaObjectFactory.exploreModelSchema(Person, schemas);

expect(Object.keys(schemas)).toHaveLength(4);
expect(Object.keys(schemas)).toHaveLength(6);

expect(schemas).toHaveProperty('Role');
expect(schemas.Role).toEqual({
Expand All @@ -78,6 +90,10 @@ describe('SchemaObjectFactory', () => {
type: 'number',
enum: [1, 2, 3]
});
expect(schemas.HairColour).toEqual({
type: 'string',
enum: ['Brown', 'Blond', 'Ginger']
});
expect(schemas).toHaveProperty('Person');
expect(schemas.Person).toEqual({
type: 'object',
Expand All @@ -102,13 +118,22 @@ describe('SchemaObjectFactory', () => {
items: {
$ref: '#/components/schemas/Ranking'
}
},
'favouriteFoods': {
'items': {
'$ref': '#/components/schemas/Food'
},
'type': 'array'
},
'hairColour': {
'$ref': '#/components/schemas/HairColour'
}
},
required: ['role', 'roles', 'groups', 'rankings']
required: ['role', 'roles', 'groups', 'rankings', 'hairColour', 'favouriteFoods']
});
schemaObjectFactory.exploreModelSchema(CreatePersonDto, schemas);

expect(Object.keys(schemas)).toHaveLength(5);
expect(Object.keys(schemas)).toHaveLength(7);
expect(schemas).toHaveProperty('CreatePersonDto');
expect(schemas.CreatePersonDto).toEqual({
type: 'object',
Expand Down

0 comments on commit afc47a7

Please sign in to comment.