-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add restructure enhancer Signed-off-by: Douglas McConnachie <[email protected]>
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
59 changes: 59 additions & 0 deletions
59
packages/openapi-v3/src/enhancers/restructure-schema.enhancer.ts
This file contains 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,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; | ||
} | ||
} |
This file contains 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