({
patchOptions,
spec: _spec,
}: {
patchOptions: Patch | undefined;
spec: unknown;
})
| 2 | import type { OpenApi } from '../../../openApi/types'; |
| 3 | |
| 4 | export async function patchOpenApiSpec({ |
| 5 | patchOptions, |
| 6 | spec: _spec, |
| 7 | }: { |
| 8 | patchOptions: Patch | undefined; |
| 9 | spec: unknown; |
| 10 | }) { |
| 11 | if (!patchOptions) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | const spec = _spec as OpenApi.V2_0_X | OpenApi.V3_0_X | OpenApi.V3_1_X; |
| 16 | |
| 17 | if (typeof patchOptions === 'function') { |
| 18 | await patchOptions(spec); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (patchOptions.input) { |
| 23 | await patchOptions.input(spec); |
| 24 | } |
| 25 | |
| 26 | if ('swagger' in spec) { |
| 27 | if (patchOptions.version && spec.swagger) { |
| 28 | spec.swagger = ( |
| 29 | typeof patchOptions.version === 'string' |
| 30 | ? patchOptions.version |
| 31 | : patchOptions.version(spec.swagger) |
| 32 | ) as typeof spec.swagger; |
| 33 | } |
| 34 | |
| 35 | if (patchOptions.meta && spec.info) { |
| 36 | patchOptions.meta(spec.info); |
| 37 | } |
| 38 | |
| 39 | if (patchOptions.schemas && spec.definitions) { |
| 40 | if (typeof patchOptions.schemas === 'function') { |
| 41 | for (const [key, schema] of Object.entries(spec.definitions)) { |
| 42 | if (schema && typeof schema === 'object') { |
| 43 | await patchOptions.schemas(key, schema); |
| 44 | } |
| 45 | } |
| 46 | } else { |
| 47 | for (const key in patchOptions.schemas) { |
| 48 | const schema = spec.definitions[key]; |
| 49 | if (!schema || typeof schema !== 'object') continue; |
| 50 | |
| 51 | const patchFn = patchOptions.schemas[key]!; |
| 52 | await patchFn(schema); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (patchOptions.operations && spec.paths) { |
| 58 | if (typeof patchOptions.operations === 'function') { |
| 59 | // Bulk callback: iterate all operations |
| 60 | for (const [path, pathItem] of Object.entries(spec.paths)) { |
| 61 | if (!pathItem || typeof pathItem !== 'object') continue; |
no test coverage detected