Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/evil-ghosts-find.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix memory leak
20 changes: 20 additions & 0 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,23 @@ export class OpenAPIHono<
configureObject: OpenAPIObjectConfigure<E, P>,
configureGenerator?: OpenAPIGeneratorConfigure<E, P>
): OpenAPIHono<E, S & ToSchema<'get', MergePath<BasePath, P>, {}, {}>, 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)
Expand All @@ -635,13 +645,23 @@ export class OpenAPIHono<
configureObject: OpenAPIObjectConfigure<E, P>,
configureGenerator?: OpenAPIGeneratorConfigure<E, P>
): OpenAPIHono<E, S & ToSchema<'get', MergePath<BasePath, P>, {}, {}>, 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)
Expand Down
Loading