Skip to content

Commit

Permalink
Merge pull request #1632 from micalevisk/hotfix/controller-default-ve…
Browse files Browse the repository at this point in the history
…rsion

fix: use `defaultVersion` as a fallback to controller version
  • Loading branch information
kamilmysliwiec committed Oct 22, 2021
2 parents c4492d4 + 1e85ce6 commit 0762f74
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/swagger-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,11 @@ export class SwaggerExplorer {
metatype: Type<unknown> | Function,
versioningOptions: VersioningOptions | undefined
): VersionValue | undefined {
return versioningOptions?.type === VersioningType.URI
? Reflect.getMetadata(VERSION_METADATA, metatype)
: undefined;
if (versioningOptions?.type === VersioningType.URI) {
return (
Reflect.getMetadata(VERSION_METADATA, metatype) ??
versioningOptions.defaultVersion
);
}
}
}
122 changes: 122 additions & 0 deletions test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Version,
VersioningType
} from '@nestjs/common';
import { VersionValue } from '@nestjs/common/interfaces';
import { ApplicationConfig } from '@nestjs/core';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import {
Expand Down Expand Up @@ -1221,4 +1222,125 @@ describe('SwaggerExplorer', () => {
expect(routes).toHaveLength(0);
});
});

describe('when defaultVersion is defined', () => {
let explorer: SwaggerExplorer;
let config: ApplicationConfig;

describe('and controller/route versions are defined', () => {
const CONTROLLER_VERSION: VersionValue = '1';
const METHOD_VERSION: VersionValue = '2';
const CONTROLLER_MULTIPLE_VERSIONS: VersionValue = ['3', '4'];

@Controller({ path:'with-version', version: CONTROLLER_VERSION })
class WithVersionController {
@Get()
foo(): void {}

@Version(METHOD_VERSION)
@Get()
bar(): void {}
}

@Controller({ path:'with-multiple-version', version: CONTROLLER_MULTIPLE_VERSIONS })
class WithMultipleVersionsController {
@Get()
foo(): void {}
}

beforeAll(() => {
explorer = new SwaggerExplorer(schemaObjectFactory);

config = new ApplicationConfig();
config.enableVersioning({
type: VersioningType.URI,
defaultVersion: 'THIS_SHOULD_NOT_APPEAR_ANYWHERE'
});
})

it('should use controller version defined', () => {
const routes = explorer.exploreController(
{
instance: new WithVersionController(),
metatype: WithVersionController
} as InstanceWrapper<WithVersionController>,
config,
'modulePath',
'globalPrefix'
);

expect(routes[0].root.path).toEqual(
`/globalPrefix/v${CONTROLLER_VERSION}/modulePath/with-version`
);
});

it('should use route version defined', () => {
const routes = explorer.exploreController(
{
instance: new WithVersionController(),
metatype: WithVersionController
} as InstanceWrapper<WithVersionController>,
config,
'modulePath',
'globalPrefix'
);

expect(routes[1].root.path).toEqual(
`/globalPrefix/v${METHOD_VERSION}/modulePath/with-version`
);
});

it('should use multiple versions defined', () => {
const routes = explorer.exploreController(
{
instance: new WithMultipleVersionsController(),
metatype: WithMultipleVersionsController
} as InstanceWrapper<WithMultipleVersionsController>,
config,
'modulePath',
'globalPrefix'
);

expect(routes[0].root.path).toEqual(
`/globalPrefix/v${CONTROLLER_MULTIPLE_VERSIONS[0]}/modulePath/with-multiple-version`
);
expect(routes[1].root.path).toEqual(
`/globalPrefix/v${CONTROLLER_MULTIPLE_VERSIONS[1]}/modulePath/with-multiple-version`
);
});
});

describe('and controller/route versions are not defined', () => {
const DEFAULT_VERSION: VersionValue = '1';

@Controller('with-multiple-version')
class WithoutVersionsController {
@Get()
foo(): void {}
}

it('should use the global default version ', () => {
const explorer = new SwaggerExplorer(schemaObjectFactory);
const config = new ApplicationConfig();
config.enableVersioning({
type: VersioningType.URI,
defaultVersion: DEFAULT_VERSION
});
const routes = explorer.exploreController(
{
instance: new WithoutVersionsController(),
metatype: WithoutVersionsController
} as InstanceWrapper<WithoutVersionsController>,
config,
'modulePath',
'globalPrefix'
);

expect(routes[0].root.path).toEqual(
`/globalPrefix/v${DEFAULT_VERSION}/modulePath/with-multiple-version`
);
});
})

});
});

0 comments on commit 0762f74

Please sign in to comment.