Skip to content

Commit

Permalink
Fix itty router nested routers appending the wrong prefix to routes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Jan 16, 2025
1 parent 523259b commit 6f4fb5c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/ittyRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function fromIttyRouter<M>(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({
Expand Down
58 changes: 58 additions & 0 deletions tests/integration/nested-routers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
});
});
});

0 comments on commit 6f4fb5c

Please sign in to comment.