Core: Overwrites previous Route in group() method

Created on 11 Dec 2017  路  13Comments  路  Source: adonisjs/core

Structuring The Issue

The order of routes does not work in the Route.group() method.

Brief Description

Sample Code

_ProductController.js_
``` js ProductController
async category({ response }) {
console.log('category');
}

async show({ response }) {
console.log('show');
}

**case 1**
_routes.js_
``` js routes.js
Route.group(() => {
  Route.get('/product/category', 'ProductController.category');
  Route.get('/product/:id', 'ProductController.show');
})
.prefix('api');

You can see 'show' in console.

case 2
_routes.js_
``` js routes.js
Route.get('/product/category', 'ProductController.category');

Route.group(() => {
Route.get('/product/:id', 'ProductController.show');
})
.prefix('api');
```
Now 'category', 'show' in console.

Error Stack (if any)

I wonder if the order in Route.group() method is irrelevant to the route, or is it a bug?

Screenshots (if any)

All 13 comments

Can you share the URL you hit from the browser?

screen shot 2017-12-11 at 11 10 38 am

Using your routes, this is output I get, which is the same order as routes get matched, which seems fine to me

sample code

// ProductController.js
async categories() {
  console.log('categories')
}

async detail() {
  console.log('show')
}

case 1

// routes.js
Route.group(() => {
  Route.get('/product/category/:id?', 'ProductController.categories');
  Route.get('/product/:id', 'ProductController.detail');
})
.prefix('api/v1')
.formats(['json']);

case 2

// routes.js
Route.get('/api/v1/product/category/:id?', 'ProductController.categories');
Route.group(() => {
  Route.get('/product/:id', 'ProductController.detail');
})
.prefix('api/v1')
.formats(['json']);

screenshots

API calls in postman
case 1
image

GET /api/v1/product/category
image

case 2
image

GET /api/v1/product/category
image

versions

image

As per your routes definition in case1 you have registered 2 routes, but screenshot of case1 shows 3 on the terminal.

Can u create a sample app which reproduces the issue

sample app

case 1
image

case 2
image

I mean share the repo of this app, screenshots are not something I can use to debug

Looks to me the root of the issue is actually .formats() and the normally optional trailing slash.
If you request /api/product/category/ or /api/product/category/.json it does resolve categories while without the slash it resolves show. Same thing with case2 if you add .formats(['json']) to the outside route it will behave the same.

Going to look into it in a while

Created an issue on the internal library Adonis uses, let's see what best can be done here https://github.com/pillarjs/path-to-regexp/issues/131

I am afraid, I don't think I can fix it anyway unless the internal library path-to-regexp fixes it. The only option is to re-write the functionality of path-to-regexp, which I have no plans to do it right now.

@thetutlage appreciate your time

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanc picture seanc  路  4Comments

umaams picture umaams  路  3Comments

amrayoub picture amrayoub  路  4Comments

milosdakic picture milosdakic  路  3Comments

GianCastle picture GianCastle  路  3Comments