From cc62c629be091e6f3b7b08eedb89985957a872dc Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Mon, 20 May 2024 10:49:55 +0200 Subject: [PATCH] fix(openapi): handle same pattern for multiple endpoints --- packages/openapi/src/generator.ts | 2 +- packages/openapi/src/meta_store.ts | 9 ++++++--- playgrounds/openapi/start/routes.ts | 3 +++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/openapi/src/generator.ts b/packages/openapi/src/generator.ts index f93c6ba..1d11c20 100644 --- a/packages/openapi/src/generator.ts +++ b/packages/openapi/src/generator.ts @@ -209,7 +209,7 @@ export class OpenApiGenerator { return { method, path: openApiPath, - spec: defu(this.metaStore.getComputed(options.path), { + spec: defu(this.metaStore.getComputed({ method, path: options.path }), { ...parameters, responses: objectMap(responses, (status, schema) => { return [ diff --git a/packages/openapi/src/meta_store.ts b/packages/openapi/src/meta_store.ts index 488e6b6..aa4cbab 100644 --- a/packages/openapi/src/meta_store.ts +++ b/packages/openapi/src/meta_store.ts @@ -12,13 +12,16 @@ export class MetaStore { return this.#store.get(key) } - getComputed(key: string) { - return this.#computedStore.get(key) + getComputed(options: { method: string; path: string }) { + return this.#computedStore.get(`${options.method.toUpperCase()}:${options.path}`) } compute() { for (const [route, value] of this.#store) { - this.#computedStore.set(route.toJSON().pattern, value) + const serialized = route.toJSON() + for (const method of serialized.methods) { + this.#computedStore.set(`${method}:${serialized.pattern}`, value) + } } } } diff --git a/playgrounds/openapi/start/routes.ts b/playgrounds/openapi/start/routes.ts index 1641b5e..c254689 100644 --- a/playgrounds/openapi/start/routes.ts +++ b/playgrounds/openapi/start/routes.ts @@ -34,6 +34,9 @@ router router.get('/random/:id', [MiscController, 'show']).openapi({ description: 'Get a random thing by id', }) + router.post('/random', [MiscController, 'store']).openapi({ + description: 'Create a random thing', + }) }) .prefix('/misc') .openapi({ tags: ['misc'] })