Skip to content

Commit

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

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Jan 29, 2020
1 parent 933b0b2 commit 32c5f59
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/openapi-v3/src/enhancers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * from './consolidate-schema.enhancer';
export * from './default-deprecate-schema.enhancer';
export * from './keys';
export * from './prune-schema.enhancer';
export * from './restructure-schema.enhancer';
export * from './spec-enhancer.service';
export * from './tags-schema.enhancer';
export * from './types';

59 changes: 59 additions & 0 deletions packages/openapi-v3/src/enhancers/restructure-schema.enhancer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {bind} from '@loopback/core';
import {OpenApiSpec} from '../types';
import {asSpecEnhancer, OASEnhancer} from './types';

/**
* A spec enhancer to restructure the OpenAPI spec in an opinionated manner.
* (dougal83)
*
*/
@bind(asSpecEnhancer)
export class RestructureEnhancer implements OASEnhancer {
name = 'restructure';

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

/**
* Structure OpenApiSpec in an opinionated manner
*
*/
private restructureSpec(spec: OpenApiSpec): OpenApiSpec {
spec = Object.assign(
{
openapi: undefined,
info: undefined,
servers: undefined,
paths: undefined,
components: undefined,
tags: undefined,
},
spec,
);

Object.keys(spec.paths).forEach(path =>
Object.keys(spec.paths[path]).forEach(op => {
spec.paths[path][op] = Object.assign(
{
tags: undefined,
summary: undefined,
description: undefined,
operationId: undefined,
parameters: undefined,
responses: undefined,
depreciated: undefined,
},
spec.paths[path][op],
);
}),
);

return spec;
}
}
3 changes: 3 additions & 0 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
OpenApiSpec,
OperationObject,
PruneEnhancer,
RestructureEnhancer,
ServerObject,
TagsEnhancer,
} from '@loopback/openapi-v3';
Expand Down Expand Up @@ -728,6 +729,8 @@ export class RestServer extends Context implements Server, HttpServerLike {
spec = tagsEnhancer.modifySpec(spec);
const defaultDeprecateEnhancer = new DefaultDeprecateEnhancer();
spec = defaultDeprecateEnhancer.modifySpec(spec);
const restructureEnhancer = new RestructureEnhancer();
spec = restructureEnhancer.modifySpec(spec);

return spec;
}
Expand Down

0 comments on commit 32c5f59

Please sign in to comment.