cloudflare / chanfana

OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!
https://chanfana.pages.dev
MIT License
302 stars 40 forks source link

Path parameters not unescaped correctly #88

Open gimenete opened 1 year ago

gimenete commented 1 year ago

Not really sure if it's a bug with itty-router or itty-router-openapi but it affects itty-router-openapi users.

Given an endpoint like this (using .original for simplicity):

router.original.get(
  "/something/:id",
  (request) => new Response(`id: ${request.params.id}`)
);

Making a query to /something/user%3A1234 returns the following response: id: user%3A1234. While it should be id: user:1234.

Just to make sure my assumption is correct I made an express application with a similar endpoint and the response is correct (id: user:1234):

app.get("/something/:id", (req, res) => {
  res.send(`id: ${req.params.id}`);
});

My workaround is to decode the full URL before passing it to the library. Like this:

export async function handleRequest(
  request: Request,
  env: Bindings,
  context: ExecutionContext
) {
  const req = new Request(decodeURIComponent(request.url), request);
  return router.handle(req, env, context);
}

Not sure if it's the best approach.

G4brym commented 1 year ago

Hey @gimenete, i've just published the new v1.0.0 release that fixes this issue, i'm now using the decodeURIComponent in the url before parsing the parameters Let me know if you are still having this issue Migration guide to 1.0.0 available here

gimenete commented 1 year ago

Hey @G4brym

I think this is still an issue for path params. I see that decodeURIComponent is being used inside extractQueryParameters but I guess that only affects query parameters and not path parameters, right?

G4brym commented 1 year ago

@gimenete path parameters are parsed on itty-router, i've openned a pull request there is the required changes https://github.com/kwhitley/itty-router/pull/184

kwhitley commented 1 year ago

Thanks @G4brym - taking a look now!