Skip to content

Add support for using @Hidden() decorator on controllers #498

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

Merged
merged 3 commits into from
Oct 7, 2019
Merged
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
29 changes: 29 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,35 @@ public async find(): Promise<any> {
}
```

### Hidden

Excludes this endpoint from the generated swagger document.

```ts
@Get()
@Hidden()
public async find(): Promise<any> {

}
```

It can also be set at the controller level to exclude all of its endpoints from the swagger document.

```ts
@Hidden()
export class HiddenController {
@Get()
public async find(): Promise<any> {

}

@Post()
public async create(): Promise<any> {

}
}
```

## Command Line Interface

For information on the configuration object (tsoa.json), check out the following:
Expand Down
16 changes: 15 additions & 1 deletion src/metadataGeneration/controllerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export class ControllerGenerator {
private readonly path?: string;
private readonly tags?: string[];
private readonly security?: Tsoa.Security[];
private readonly isHidden?: boolean;

constructor(private readonly node: ts.ClassDeclaration, private readonly current: MetadataGenerator) {
this.path = this.getPath();
this.tags = this.getTags();
this.security = this.getSecurity();
this.isHidden = this.getIsHidden();
}

public IsValid() {
Expand Down Expand Up @@ -42,7 +44,7 @@ export class ControllerGenerator {
private buildMethods() {
return this.node.members
.filter(m => m.kind === ts.SyntaxKind.MethodDeclaration)
.map((m: ts.MethodDeclaration) => new MethodGenerator(m, this.current, this.tags, this.security))
.map((m: ts.MethodDeclaration) => new MethodGenerator(m, this.current, this.tags, this.security, this.isHidden))
.filter(generator => generator.IsValid())
.map(generator => generator.Generate());
}
Expand Down Expand Up @@ -85,4 +87,16 @@ export class ControllerGenerator {

return getSecurities(securityDecorators);
}

private getIsHidden(): boolean {
const hiddenDecorators = getDecorators(this.node, identifier => identifier.text === 'Hidden');
if (!hiddenDecorators || !hiddenDecorators.length) {
return false;
}
if (hiddenDecorators.length > 1) {
throw new GenerateMetadataError(`Only one Hidden decorator allowed in '${this.node.name!.text}' class.`);
}

return true;
}
}
17 changes: 14 additions & 3 deletions src/metadataGeneration/methodGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';
import { getDecorators } from './../utils/decoratorUtils';
import {getJSDocComment, getJSDocDescription, isExistJSDocTag} from './../utils/jsDocUtils';
import { getJSDocComment, getJSDocDescription, isExistJSDocTag } from './../utils/jsDocUtils';
import { GenerateMetadataError } from './exceptions';
import { getInitializerValue } from './initializer-value';
import { MetadataGenerator } from './metadataGenerator';
Expand All @@ -13,7 +13,13 @@ export class MethodGenerator {
private method: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head';
private path: string;

constructor(private readonly node: ts.MethodDeclaration, private readonly current: MetadataGenerator, private readonly parentTags?: string[], private readonly parentSecurity?: Tsoa.Security[]) {
constructor(
private readonly node: ts.MethodDeclaration,
private readonly current: MetadataGenerator,
private readonly parentTags?: string[],
private readonly parentSecurity?: Tsoa.Security[],
private readonly isParentHidden?: boolean,
) {
this.processMethodDecorators();
}

Expand Down Expand Up @@ -251,8 +257,13 @@ export class MethodGenerator {
private getIsHidden() {
const hiddenDecorators = this.getDecoratorsByIdentifier(this.node, 'Hidden');
if (!hiddenDecorators || !hiddenDecorators.length) {
return false;
return !!this.isParentHidden;
}

if (this.isParentHidden) {
throw new GenerateMetadataError(`Hidden decorator cannot be set on '${this.getCurrentLocation()}' it is already defined on the controller`);
}

if (hiddenDecorators.length > 1) {
throw new GenerateMetadataError(`Only one Hidden decorator allowed in '${this.getCurrentLocation}' method.`);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/controllers/hiddenController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller, Get, Hidden, Post, Route } from '../../../src';
import { ModelService } from '../services/modelService';
import { TestModel } from '../testModel';

@Route('Controller')
@Hidden()
export class HiddenMethodController extends Controller {
@Get('hiddenGetMethod')
public async hiddenGetMethod(): Promise<TestModel> {
return Promise.resolve(new ModelService().getModel());
}
@Post('hiddenPostMethod')
public async hiddenPostMethod(): Promise<TestModel> {
return Promise.resolve(new ModelService().getModel());
}
}
16 changes: 14 additions & 2 deletions tests/unit/swagger/definitionsGeneration/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ describe('Metadata generation', () => {
const parameterMetadata = new MetadataGenerator('./tests/fixtures/controllers/hiddenMethodController.ts').Generate();
const controller = parameterMetadata.controllers[0];

it('should generate methods visible by default', () => {
it('should mark methods as visible by default', () => {
const method = controller.methods.find(m => m.name === 'normalGetMethod');
if (!method) {
throw new Error('Method normalGetMethod not defined!');
Expand All @@ -491,7 +491,7 @@ describe('Metadata generation', () => {
expect(method.isHidden).to.equal(false);
});

it('should generate hidden methods', () => {
it('should mark methods as hidden', () => {
const method = controller.methods.find(m => m.name === 'hiddenGetMethod');
if (!method) {
throw new Error('Method hiddenGetMethod not defined!');
Expand All @@ -503,6 +503,18 @@ describe('Metadata generation', () => {
});
});

describe('HiddenControllerGenerator', () => {
const parameterMetadata = new MetadataGenerator('./tests/fixtures/controllers/hiddenController.ts').Generate();
const controller = parameterMetadata.controllers[0];

it('should mark all methods as hidden', () => {
expect(controller.methods).to.have.lengthOf(2);
controller.methods.forEach(method => {
expect(method.isHidden).to.equal(true);
});
});
});

describe('DeprecatedMethodGenerator', () => {
const parameterMetadata = new MetadataGenerator('./tests/fixtures/controllers/deprecatedController.ts').Generate();
const controller = parameterMetadata.controllers[0];
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/swagger/schemaDetails.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai';
import 'mocha';

import { expect } from 'chai';

import { MetadataGenerator } from '../../../src/metadataGeneration/metadataGenerator';
import { SpecGenerator2 } from '../../../src/swagger/specGenerator2';
import { getDefaultOptions } from '../../fixtures/defaultOptions';
Expand Down Expand Up @@ -52,4 +54,22 @@ describe('Schema details generation', () => {
}

it('should set API license if provided', () => expect(licenseName).to.equal(getDefaultOptions().license));

describe('paths', () => {
describe('hidden paths', () => {
it('should not contain hidden paths', () => {
const metadataHiddenMethod = new MetadataGenerator('./tests/fixtures/controllers/hiddenMethodController.ts').Generate();
const specHiddenMethod = new SpecGenerator2(metadataHiddenMethod, getDefaultOptions()).GetSpec();

expect(specHiddenMethod.paths).to.have.keys(['/Controller/normalGetMethod']);
});

it('should not contain paths for hidden controller', () => {
const metadataHiddenController = new MetadataGenerator('./tests/fixtures/controllers/hiddenController.ts').Generate();
const specHiddenController = new SpecGenerator2(metadataHiddenController, getDefaultOptions()).GetSpec();

expect(specHiddenController.paths).to.be.empty;
});
});
});
});
15 changes: 15 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
});
});
});
describe('hidden paths', () => {
it('should not contain hidden paths', () => {
const metadataHiddenMethod = new MetadataGenerator('./tests/fixtures/controllers/hiddenMethodController.ts').Generate();
const specHiddenMethod = new SpecGenerator3(metadataHiddenMethod, getDefaultOptions()).GetSpec();

expect(specHiddenMethod.paths).to.have.keys(['/Controller/normalGetMethod']);
});

it('should not contain paths for hidden controller', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So basically the same thing you did here but with SpecGenerator2 instead. And just put it anywhere other than this file.

const metadataHiddenController = new MetadataGenerator('./tests/fixtures/controllers/hiddenController.ts').Generate();
const specHiddenController = new SpecGenerator3(metadataHiddenController, getDefaultOptions()).GetSpec();

expect(specHiddenController.paths).to.be.empty;
});
});
});

describe('components', () => {
Expand Down