From 6f4fb5c45f2c3c062730470a46c383f3d0048f74 Mon Sep 17 00:00:00 2001 From: Gabriel Massadas <5445926+G4brym@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:31:34 +0000 Subject: [PATCH] Fix itty router nested routers appending the wrong prefix to routes (#231) --- package-lock.json | 4 +- package.json | 2 +- src/adapters/ittyRouter.ts | 2 +- tests/integration/nested-routers.test.ts | 58 ++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5097575..1d7d770 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "chanfana", - "version": "2.6.1", + "version": "2.6.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "chanfana", - "version": "2.6.1", + "version": "2.6.2", "license": "MIT", "dependencies": { "@asteasolutions/zod-to-openapi": "^7.2.0", diff --git a/package.json b/package.json index 1d8d4db..f52f89e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chanfana", - "version": "2.6.1", + "version": "2.6.2", "description": "OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/src/adapters/ittyRouter.ts b/src/adapters/ittyRouter.ts index b482a79..60a1cf0 100644 --- a/src/adapters/ittyRouter.ts +++ b/src/adapters/ittyRouter.ts @@ -49,7 +49,7 @@ export function fromIttyRouter(router: M, options?: RouterOptions): M & IttyR handlers = openapiRouter.registerNestedRouter({ method: prop, nestedRouter: handlers[0], - path: route, + path: undefined, }); } else if (openapiRouter.allowedMethods.includes(prop)) { handlers = openapiRouter.registerRoute({ diff --git a/tests/integration/nested-routers.test.ts b/tests/integration/nested-routers.test.ts index 1a59ef1..fe02fda 100644 --- a/tests/integration/nested-routers.test.ts +++ b/tests/integration/nested-routers.test.ts @@ -90,4 +90,62 @@ describe("innerRouter", () => { expect(request.status).toEqual(404); expect(resp).toEqual("Not Found."); }); + + it("nested router with base path", async () => { + const innerRouter = fromIttyRouter(AutoRouter(), { + base: "/api/v1", + }); + innerRouter.get("/todo/:id", ToDoGet); + + const router = fromIttyRouter(AutoRouter()); + router.all("/api/v1/*", innerRouter); + + const request = await router.fetch(new Request("http://localhost:8080/openapi.json")); + const resp = await request.json(); + + expect(request.status).toEqual(200); + expect(resp).toEqual({ + components: { + parameters: {}, + schemas: {}, + }, + info: { + title: "OpenAPI", + version: "1.0.0", + }, + openapi: "3.1.0", + paths: { + "/api/v1/todo/{id}": { + get: { + operationId: "get_ToDoGet", + parameters: [ + { + in: "path", + name: "id", + required: true, + schema: { + type: "number", + }, + }, + ], + responses: { + "200": { + content: { + "application/json": { + schema: { + todo: {}, + }, + }, + }, + description: "example", + }, + }, + summary: "Get a single ToDo", + tags: ["ToDo"], + }, + }, + }, + webhooks: {}, + }); + }); });