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: enum array when using enumName #2929

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 21 additions & 10 deletions lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,29 @@ export class SchemaObjectFactory {
const $ref = getSchemaPath(enumName);

if (!(enumName in schemas)) {
const _enum = param.enum
? param.enum
: param.schema
? param.schema['items']
? param.schema['items']['enum']
: param.schema['enum']
: param.isArray && param.items
? param.items.enum
: undefined;
let _enum;
let _type;
if (param.enum) {
_enum = param.enum;
_type = param.type;
} else if (param.schema) {
if (param.schema['items']) {
_enum = param.schema['items']['enum'];
_type = param.schema['items']['type'];
} else {
_enum = param.schema['enum'];
_type = param.schema['type'];
}
} else if (param.isArray && param.items) {
_enum = param.items.enum;
_type = param.items.type;
} else {
_enum = undefined;
_type = 'string';
}

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

it('should properly define enum as schema with lazy function', () => {
const explorer = new SwaggerExplorer(schemaObjectFactory);
const schema = explorer.getSchemas();
const routes = explorer.exploreController(
{
instance: new BarController(),
Expand All @@ -1097,6 +1098,12 @@ describe('SwaggerExplorer', () => {
'globalPrefix'
);

expect(schema.QueryEnum).toEqual({ type: 'number', enum: [1, 2, 3] });
expect(schema.ParamEnum).toEqual({
type: 'string',
enum: ['a', 'b', 'c']
});

expect(routes[0].root!.parameters).toEqual([
{
in: 'query',
Expand Down