Order of routes declaration in python file

Hi,

We got an issue when we declare this 2 routes in this order into the app.py:

@app.get("/v1/<id:string>/<obj:str>")
async def get_one_object(request, id): 
    ...

@app.get("/v1/<id:string>/objects")
async def get_objects(request, id): 
        ...

In this case, if we call the route /v1/my_id/objects, the request goes into the method get_one_object() and not get_objects()

If we change the order of the two methods in the python file, everything works fine.

But I don’t see any information about the importance of the declaration order in the Sanic’s documentation.

Thank you for your help,

Yes, ordering is important. There is no real way to know the difference between those routes except trying one, checking for a match, then trying the next.

Hi, thank you for your answer :slight_smile:

A solution. could be to check all route and to select the one which match the most.
I will check on my side and make you a proposition.

That would be a huge hit in performance, so I doubt we will make that change. Routing is on the table and a place we are looking to optimize more. If you want to learn more take a look at this thread.

I will take a look :slight_smile:

Thank you

Or, to avoid performance issue, the routes could be sorted when the server starts, no ?