| 18 | type SchemaOrRef = SchemaObject | ReferenceObject; |
| 19 | |
| 20 | export class DefaultSpecProcessor implements ISpecProcessor { |
| 21 | async process(spec: OpenAPIV3.Document): Promise<OpenAPIV3.Document> { |
| 22 | // First dereference all $refs |
| 23 | const dereferencedSpec = (await $RefParser.dereference(spec, { |
| 24 | continueOnError: true, |
| 25 | })) as OpenAPIV3.Document; |
| 26 | |
| 27 | // Then merge all allOf schemas |
| 28 | return this.mergeAllOfSchemas(dereferencedSpec); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Recursively traverses the OpenAPI spec and merges any allOf schemas found |
| 33 | * @param spec The OpenAPI specification to process |
| 34 | * @returns The processed specification with merged allOf schemas |
| 35 | */ |
| 36 | private mergeAllOfSchemas(spec: OpenAPIV3.Document): OpenAPIV3.Document { |
| 37 | // Deep clone the spec to avoid modifying the input |
| 38 | const processedSpec = structuredClone(spec); |
| 39 | |
| 40 | // Process components schemas if they exist |
| 41 | if (processedSpec.components?.schemas) { |
| 42 | for (const [key, schema] of Object.entries( |
| 43 | processedSpec.components.schemas |
| 44 | )) { |
| 45 | processedSpec.components.schemas[key] = this.processSchema( |
| 46 | schema as SchemaOrRef |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Process schemas in paths |
| 52 | for (const path of Object.values(processedSpec.paths || {})) { |
| 53 | this.processPathItem(path as OpenAPIV3.PathItemObject); |
| 54 | } |
| 55 | |
| 56 | return processedSpec; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Processes a path item object, handling all nested schemas |
| 61 | * @param pathItem The path item to process |
| 62 | */ |
| 63 | private processPathItem(pathItem: OpenAPIV3.PathItemObject): void { |
| 64 | const operations = [ |
| 65 | "get", |
| 66 | "put", |
| 67 | "post", |
| 68 | "delete", |
| 69 | "options", |
| 70 | "head", |
| 71 | "patch", |
| 72 | "trace", |
| 73 | ]; |
| 74 | |
| 75 | for (const op of operations) { |
| 76 | const operation = pathItem[ |
| 77 | op as keyof OpenAPIV3.PathItemObject |
nothing calls this directly
no outgoing calls
no test coverage detected