Skip to content

Commit ac02bdf

Browse files
author
carmine
committed
caches pre-processed resolved schemas
1 parent 0b50688 commit ac02bdf

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/middlewares/parsers/schema.preprocessor.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export class SchemaPreprocessor {
9595
private apiDocRes: OpenAPIV3.DocumentV3 | OpenAPIV3.DocumentV3_1;
9696
private serDesMap: SerDesMap;
9797
private responseOpts: ValidateResponseOpts;
98+
private resolvedSchemaCache = new Map<string, SchemaObject>();
99+
98100
constructor(
99101
apiDoc: OpenAPIV3.DocumentV3 | OpenAPIV3.DocumentV3_1,
100102
ajvOptions: Options,
@@ -470,7 +472,7 @@ export class SchemaPreprocessor {
470472
}
471473
}
472474

473-
private handleReadonly(
475+
private handleReadonly(
474476
parent: OpenAPIV3.SchemaObject,
475477
schema: OpenAPIV3.SchemaObject,
476478
opts,
@@ -606,12 +608,18 @@ export class SchemaPreprocessor {
606608
private resolveSchema<T>(schema): T {
607609
if (!schema) return null;
608610
const ref = schema?.['$ref'];
611+
if (ref && this.resolvedSchemaCache.has(ref)) {
612+
return this.resolvedSchemaCache.get(ref) as T;
613+
}
609614
let res = (ref ? this.ajv.getSchema(ref)?.schema : schema) as T;
610615
if (ref && !res) {
611616
const path = ref.split('/').join('.');
612617
const p = path.substring(path.indexOf('.') + 1);
613618
res = _get(this.apiDoc, p);
614619
}
620+
if (ref) {
621+
this.resolvedSchemaCache.set(ref, res);
622+
}
615623
return res;
616624
}
617625
/**

0 commit comments

Comments
 (0)