Skip to content

Commit

Permalink
Merge pull request #2238 from habiiev/fix-number-enum-schema-type
Browse files Browse the repository at this point in the history
fix: number enum generated schema type
  • Loading branch information
kamilmysliwiec committed Feb 6, 2023
2 parents daf477d + 05d420a commit 4f786b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class SchemaObjectFactory {
: param.schema['enum'];

schemas[enumName] = {
type: 'string',
type: param.schema?.['type'] ?? 'string',
enum: _enum
};
}
Expand Down
40 changes: 40 additions & 0 deletions test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,19 @@ describe('SwaggerExplorer', () => {
}
}

@Controller('')
class Bar2Controller {
@Get('bars/:objectId')
@ApiParam({
name: 'objectId',
enum: [1, 2, 3],
enumName: 'NumberEnum'
})
findBar(): Promise<Foo> {
return Promise.resolve(null);
}
}

it('should properly define enums', () => {
const explorer = new SwaggerExplorer(schemaObjectFactory);
const config = new ApplicationConfig();
Expand Down Expand Up @@ -1052,6 +1065,33 @@ describe('SwaggerExplorer', () => {
}
]);
});

it('should properly define number enum as schema', () => {
const explorer = new SwaggerExplorer(schemaObjectFactory);

const schema = explorer.getSchemas();
const routes = explorer.exploreController(
{
instance: new Bar2Controller(),
metatype: Bar2Controller
} as InstanceWrapper<Bar2Controller>,
new ApplicationConfig(),
'modulePath',
'globalPrefix'
);

expect(schema.NumberEnum).toEqual({ type: 'number', enum: [1, 2, 3] });
expect(routes[0].root.parameters).toEqual([
{
in: 'path',
name: 'objectId',
required: true,
schema: {
$ref: '#/components/schemas/NumberEnum'
}
}
]);
});
});

describe('when headers are defined', () => {
Expand Down

0 comments on commit 4f786b1

Please sign in to comment.