diff --git a/.changeset/evil-ghosts-find.md b/.changeset/evil-ghosts-find.md new file mode 100644 index 000000000..34d3fc373 --- /dev/null +++ b/.changeset/evil-ghosts-find.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix memory leak diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 866b62416..f78a0427b 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -616,13 +616,23 @@ export class OpenAPIHono< configureObject: OpenAPIObjectConfigure, configureGenerator?: OpenAPIGeneratorConfigure ): OpenAPIHono, {}, {}>, BasePath> => { + const isStatic = + typeof configureObject !== 'function' && typeof configureGenerator !== 'function' + let cachedDocument: OpenAPIObject | null = null + return this.get(path, (c) => { + if (isStatic && cachedDocument !== null) { + return c.json(cachedDocument) + } const objectConfig = typeof configureObject === 'function' ? configureObject(c) : configureObject const generatorConfig = typeof configureGenerator === 'function' ? configureGenerator(c) : configureGenerator try { const document = this.getOpenAPIDocument(objectConfig, generatorConfig) + if (isStatic) { + cachedDocument = document + } return c.json(document) } catch (e: any) { return c.json(e, 500) @@ -635,13 +645,23 @@ export class OpenAPIHono< configureObject: OpenAPIObjectConfigure, configureGenerator?: OpenAPIGeneratorConfigure ): OpenAPIHono, {}, {}>, BasePath> => { + const isStatic = + typeof configureObject !== 'function' && typeof configureGenerator !== 'function' + let cachedDocument: OpenAPIV31bject | null = null return this.get(path, (c) => { + // Return cached document if available and config is static + if (isStatic && cachedDocument !== null) { + return c.json(cachedDocument) + } const objectConfig = typeof configureObject === 'function' ? configureObject(c) : configureObject const generatorConfig = typeof configureGenerator === 'function' ? configureGenerator(c) : configureGenerator try { const document = this.getOpenAPI31Document(objectConfig, generatorConfig) + if (isStatic) { + cachedDocument = document + } return c.json(document) } catch (e: any) { return c.json(e, 500)