Skip to content

Commit 669ede1

Browse files
committed
fix(rest): only return matched trie nodes with values
1 parent e5e5fc4 commit 669ede1

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/rest/src/__tests__/unit/router/routing-table.unit.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,44 @@ function runTestsWithRouter(router: RestRouter) {
214214
expect(route.pathParams).to.containEql({userId: '1', format: 'json'});
215215
});
216216

217+
it('finds "GET /orders, /orders/{id}, /orders/{orderId}/shipments" endpoints', () => {
218+
class TestController {
219+
@get('/orders/{id}')
220+
async getOrderById(@param.path.number('id') id: number): Promise<object> {
221+
return {id};
222+
}
223+
@get('/orders')
224+
async findOrders(): Promise<object[]> {
225+
return [];
226+
}
227+
// A path that overlaps with `/orders/{id}`. Please note a different var
228+
// name is used - `{orderId}`
229+
@get('/orders/{orderId}/shipments')
230+
async getShipmentsForOrder(
231+
@param.path.number('orderId') id: number,
232+
): Promise<object> {
233+
return [];
234+
}
235+
}
236+
237+
const table = givenRoutingTable();
238+
const spec = getControllerSpec(TestController);
239+
table.registerController(spec, TestController);
240+
241+
const findAndCheckRoute = (url: string, expectedPath: string) => {
242+
let request = givenRequest({
243+
method: 'get',
244+
url,
245+
});
246+
const route = table.find(request);
247+
expect(route.path).to.eql(expectedPath);
248+
};
249+
250+
findAndCheckRoute('/orders/1', '/orders/{id}');
251+
findAndCheckRoute('/orders/1/shipments', '/orders/{orderId}/shipments');
252+
findAndCheckRoute('/orders', '/orders');
253+
});
254+
217255
it('throws if router is not found', () => {
218256
const table = givenRoutingTable();
219257

packages/rest/src/router/trie.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function search<T>(
161161
// There might be multiple matches, such as `/users/{id}` and `/users/{userId}`
162162
for (const child of children) {
163163
const result = search(keys, index + 1, params, child.node);
164-
if (result) {
164+
if (result && isNodeWithValue(result.node)) {
165165
Object.assign(params, child.params);
166166
return result;
167167
}

0 commit comments

Comments
 (0)