Skip to content

Commit e3577ab

Browse files
committed
fix(rest): make the route binding key friendly for find
1 parent 871ddef commit e3577ab

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/rest/src/rest-server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,11 @@ export class RestServer extends Context implements Server {
382382
): Binding {
383383
if (typeof routeOrVerb === 'object') {
384384
const r = routeOrVerb;
385-
return this.bind(`routes.${r.verb} ${r.path}`).to(r);
385+
// Encode the path to escape special chars
386+
const encodedPath = encodeURIComponent(r.path).replace(/\./g, '%2E');
387+
return this.bind(`routes.${r.verb} ${encodedPath}`)
388+
.to(r)
389+
.tag('route');
386390
}
387391

388392
if (!path) {

packages/rest/test/unit/rest-server/rest-server.open-api-spec.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ describe('RestServer.getApiSpec()', () => {
4646
});
4747
});
4848

49+
it('binds a route via app.route(route)', () => {
50+
function greet() {}
51+
const binding = server.route(
52+
new Route('get', '/greet', {responses: {}}, greet),
53+
);
54+
expect(binding.key).to.eql('routes.get %2Fgreet');
55+
expect(binding.tags.has('route')).to.be.true();
56+
});
57+
58+
it('binds a route via app.route(..., Controller, method)', () => {
59+
class MyController {
60+
greet() {}
61+
}
62+
63+
const binding = server.route(
64+
'get',
65+
'/greet.json',
66+
{responses: {}},
67+
MyController,
68+
'greet',
69+
);
70+
expect(binding.key).to.eql('routes.get %2Fgreet%2Ejson');
71+
expect(binding.tags.has('route')).to.be.true();
72+
});
73+
4974
it('returns routes registered via app.route(route)', () => {
5075
function greet() {}
5176
server.route(new Route('get', '/greet', {responses: {}}, greet));

0 commit comments

Comments
 (0)