Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/rest/src/__tests__/unit/router/routing-table.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,44 @@ function runTestsWithRouter(router: RestRouter) {
expect(route.pathParams).to.containEql({userId: '1', format: 'json'});
});

it('finds "GET /orders, /orders/{id}, /orders/{orderId}/shipments" endpoints', () => {
class TestController {
@get('/orders/{id}')
async getOrderById(@param.path.number('id') id: number): Promise<object> {
return {id};
}
@get('/orders')
async findOrders(): Promise<object[]> {
return [];
}
// A path that overlaps with `/orders/{id}`. Please note a different var
// name is used - `{orderId}`
@get('/orders/{orderId}/shipments')
async getShipmentsForOrder(
@param.path.number('orderId') id: number,
): Promise<object> {
return [];
}
}

const table = givenRoutingTable();
const spec = getControllerSpec(TestController);
table.registerController(spec, TestController);

const findAndCheckRoute = (url: string, expectedPath: string) => {
let request = givenRequest({
method: 'get',
url,
});
const route = table.find(request);
expect(route.path).to.eql(expectedPath);
};

findAndCheckRoute('/orders/1', '/orders/{id}');
findAndCheckRoute('/orders/1/shipments', '/orders/{orderId}/shipments');
findAndCheckRoute('/orders', '/orders');
});

it('throws if router is not found', () => {
const table = givenRoutingTable();

Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/router/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function search<T>(
// There might be multiple matches, such as `/users/{id}` and `/users/{userId}`
for (const child of children) {
const result = search(keys, index + 1, params, child.node);
if (result) {
if (result && isNodeWithValue(result.node)) {
Object.assign(params, child.params);
return result;
}
Expand Down