-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(openapi-v3): add OAS3 visibility decorator #6414
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
raymondfeng
merged 1 commit into
loopbackio:master
from
achrinzafork:feat/adds-oas-visibility-decorator
Oct 6, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/openapi-v3/src/__tests__/unit/decorators/visibility.decorator.unit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/openapi-v3 | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {anOpenApiSpec} from '@loopback/openapi-spec-builder'; | ||
| import {expect} from '@loopback/testlab'; | ||
| import {api, get, getControllerSpec, oas, OperationVisibility} from '../../..'; | ||
|
|
||
| /** | ||
| * Verification of the Controller Spec uses string literals (e.g. | ||
| * 'documented', 'undocumented') instead of the `OperationVisibility` enum so | ||
| * as to verify that the enum itself did not change. | ||
| */ | ||
| describe('visibility decorator', () => { | ||
| it('Returns a spec with all the items decorated from the class level', () => { | ||
| const expectedSpec = anOpenApiSpec() | ||
| .withOperationReturningString('get', '/greet', 'greet') | ||
| .withOperationReturningString('get', '/echo', 'echo') | ||
| .build(); | ||
|
|
||
| @api(expectedSpec) | ||
| @oas.visibility(OperationVisibility.UNDOCUMENTED) | ||
| class MyController { | ||
| greet() { | ||
| return 'Hello world!'; | ||
| } | ||
| echo() { | ||
| return 'Hello world!'; | ||
| } | ||
| } | ||
|
|
||
| const actualSpec = getControllerSpec(MyController); | ||
| expect(actualSpec.paths['/greet'].get['x-visibility']).to.eql( | ||
| 'undocumented', | ||
| ); | ||
| expect(actualSpec.paths['/echo'].get['x-visibility']).to.eql( | ||
| 'undocumented', | ||
| ); | ||
| }); | ||
|
|
||
| it('Returns a spec where only one method is undocumented', () => { | ||
| class MyController { | ||
| @get('/greet') | ||
| greet() { | ||
| return 'Hello world!'; | ||
| } | ||
|
|
||
| @get('/echo') | ||
| @oas.visibility(OperationVisibility.UNDOCUMENTED) | ||
| echo() { | ||
| return 'Hello world!'; | ||
| } | ||
| } | ||
|
|
||
| const actualSpec = getControllerSpec(MyController); | ||
| expect(actualSpec.paths['/greet'].get['x-visibility']).to.be.undefined(); | ||
| expect(actualSpec.paths['/echo'].get['x-visibility']).to.eql( | ||
| 'undocumented', | ||
| ); | ||
| }); | ||
|
|
||
| it('Allows a method to override the visibility of a class', () => { | ||
| @oas.visibility(OperationVisibility.UNDOCUMENTED) | ||
| class MyController { | ||
| @get('/greet') | ||
| greet() { | ||
| return 'Hello world!'; | ||
| } | ||
|
|
||
| @get('/echo') | ||
| echo() { | ||
| return 'Hello world!'; | ||
| } | ||
|
|
||
| @get('/yell') | ||
| @oas.visibility(OperationVisibility.DOCUMENTED) | ||
| yell() { | ||
| return 'HELLO WORLD!'; | ||
| } | ||
| } | ||
| const actualSpec = getControllerSpec(MyController); | ||
| expect(actualSpec.paths['/greet'].get['x-visibility']).to.eql( | ||
| 'undocumented', | ||
| ); | ||
| expect(actualSpec.paths['/echo'].get['x-visibility']).to.eql( | ||
| 'undocumented', | ||
| ); | ||
| expect(actualSpec.paths['/yell'].get['x-visibility']).to.be.eql( | ||
| 'documented', | ||
| ); | ||
| }); | ||
|
|
||
| it('Allows a class to not be decorated with @oas.visibility at all', () => { | ||
| class MyController { | ||
| @get('/greet') | ||
| greet() { | ||
| return 'Hello world!'; | ||
| } | ||
|
|
||
| @get('/echo') | ||
| echo() { | ||
| return 'Hello world!'; | ||
| } | ||
| } | ||
|
|
||
| const actualSpec = getControllerSpec(MyController); | ||
| expect(actualSpec.paths['/greet'].get['x-visibility']).to.be.undefined(); | ||
| expect(actualSpec.paths['/echo'].get['x-visibility']).to.be.undefined(); | ||
| }); | ||
|
|
||
| it('Does not allow a member variable to be decorated', () => { | ||
| const shouldThrow = () => { | ||
| class MyController { | ||
| @oas.visibility(OperationVisibility.UNDOCUMENTED) | ||
| public foo: string; | ||
|
|
||
| @get('/greet') | ||
| greet() {} | ||
| } | ||
|
|
||
| return getControllerSpec(MyController); | ||
| }; | ||
|
|
||
| expect(shouldThrow).to.throw( | ||
| /^\@oas.visibility cannot be used on a property:/, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/openapi-v3/src/decorators/visibility.decorator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/openapi-v3 | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import { | ||
| ClassDecoratorFactory, | ||
| DecoratorFactory, | ||
| MethodDecoratorFactory, | ||
| } from '@loopback/core'; | ||
| import {OAI3Keys} from '../keys'; | ||
| import {OperationVisibility} from '../types'; | ||
|
|
||
| const debug = require('debug')( | ||
| 'loopback:openapi3:metadata:controller-spec:visibility', | ||
| ); | ||
|
|
||
| /** | ||
| * Marks an api path with the specfied visibility. When applied to a class, | ||
| * this decorator marks all paths with the specified visibility. | ||
| * | ||
| * You can optionally mark all controllers in a class with | ||
| * `@visibility('undocumented')`, but use `@visibility('documented')` | ||
| * on a specific method to ensure it is not marked as `undocumented`. | ||
| * | ||
| * @param visibilityTyoe - The visbility of the api path on the OAS3 spec. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * @oas.visibility('undocumented') | ||
| * class MyController { | ||
| * @get('/greet') | ||
| * async function greet() { | ||
| * return 'Hello, World!' | ||
| * } | ||
| * | ||
| * @get('/greet-v2') | ||
| * @oas.deprecated('documented') | ||
| * async function greetV2() { | ||
| * return 'Hello, World!' | ||
| * } | ||
| * } | ||
| * | ||
| * class MyOtherController { | ||
| * @get('/echo') | ||
| * async function echo() { | ||
| * return 'Echo!' | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function visibility(visibilityType: OperationVisibility) { | ||
| return function visibilityDecoratorForClassOrMethod( | ||
| // Class or a prototype | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| target: any, | ||
| method?: string, | ||
| // Use `any` to for `TypedPropertyDescriptor` | ||
| // See https://github.com/strongloop/loopback-next/pull/2704 | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| methodDescriptor?: TypedPropertyDescriptor<any>, | ||
| ) { | ||
| debug(target, method, methodDescriptor); | ||
|
|
||
| if (method && methodDescriptor) { | ||
| // Method | ||
| return MethodDecoratorFactory.createDecorator<OperationVisibility>( | ||
| OAI3Keys.VISIBILITY_METHOD_KEY, | ||
| visibilityType, | ||
| { | ||
| decoratorName: '@oas.visibility', | ||
| }, | ||
| )(target, method, methodDescriptor); | ||
| } else if (typeof target === 'function' && !method && !methodDescriptor) { | ||
| // Class | ||
| return ClassDecoratorFactory.createDecorator<OperationVisibility>( | ||
| OAI3Keys.VISIBILITY_CLASS_KEY, | ||
| visibilityType, | ||
| {decoratorName: '@oas.visibility'}, | ||
| )(target); | ||
| } else { | ||
| throw new Error( | ||
| '@oas.visibility cannot be used on a property: ' + | ||
| DecoratorFactory.getTargetName(target, method, methodDescriptor), | ||
| ); | ||
| } | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.