Skip to content

Commit

Permalink
feat: add default deprecate enhancer
Browse files Browse the repository at this point in the history
add deprecate enhancer

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Jan 29, 2020
1 parent 0b769a5 commit 5271b8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {bind} from '@loopback/core';
import {OperationObject} from 'openapi3-ts';
import {OpenApiSpec} from '../types';
import {asSpecEnhancer, OASEnhancer} from './types';

/**
* A spec enhancer to add verbose deprecation status to OperationObjects
*
*/
@bind(asSpecEnhancer)
export class DefaultDeprecateEnhancer implements OASEnhancer {
name = 'defaultdeprecate';

modifySpec(spec: OpenApiSpec): OpenApiSpec {
try {
return this.defaultDeprecate(spec);
} catch {
console.log('Default Deprecate Enhancer failed, returned original spec.');
return spec;
}
}

/**
* add verbose deprecation status to OperationObjects if not set
*
*/
private defaultDeprecate(spec: OpenApiSpec): OpenApiSpec {
Object.keys(spec.paths).forEach(path =>
Object.keys(spec.paths[path]).forEach(op => {
const OpObj = spec.paths[path][op] as OperationObject;
if (!OpObj.deprecated) OpObj.deprecated = false;
}),
);

return spec;
}
}
1 change: 1 addition & 0 deletions packages/openapi-v3/src/enhancers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

export * from './consolidate-schema.enhancer';
export * from './default-deprecate-schema.enhancer';
export * from './keys';
export * from './prune-schema.enhancer';
export * from './spec-enhancer.service';
Expand Down
3 changes: 3 additions & 0 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Application, CoreBindings, Server} from '@loopback/core';
import {HttpServer, HttpServerOptions} from '@loopback/http-server';
import {
ConsolidationEnhancer,
DefaultDeprecateEnhancer,
getControllerSpec,
OpenAPIObject,
OpenApiSpec,
Expand Down Expand Up @@ -722,6 +723,8 @@ export class RestServer extends Context implements Server, HttpServerLike {
spec = consolidationEnhancer.modifySpec(spec);
const pruneEnhancer = new PruneEnhancer();
spec = pruneEnhancer.modifySpec(spec);
const defaultDeprecateEnhancer = new DefaultDeprecateEnhancer();
spec = defaultDeprecateEnhancer.modifySpec(spec);

return spec;
}
Expand Down

0 comments on commit 5271b8c

Please sign in to comment.