Skip to content

Commit

Permalink
fix(rest): only return matched trie nodes with values
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 12, 2019
1 parent e5e5fc4 commit 669ede1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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

0 comments on commit 669ede1

Please sign in to comment.