Skip to content

Commit

Permalink
Merge pull request #1364 from yunstanford/raise-exception-when-param-…
Browse files Browse the repository at this point in the history
…conflicts

Raise exception when param conflicts
  • Loading branch information
yunstanford committed Oct 13, 2018
2 parents 4175924 + f15a7fb commit 0cad831
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions sanic/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class RouteDoesNotExist(Exception):
pass


class ParameterNameConflicts(Exception):
pass


class Router:
"""Router supports basic routing with parameters and method checks
Expand Down Expand Up @@ -195,12 +199,19 @@ def _add(self, uri, methods, handler, host=None, name=None):
methods = frozenset(methods)

parameters = []
parameter_names = set()
properties = {"unhashable": None}

def add_parameter(match):
name = match.group(1)
name, _type, pattern = self.parse_parameter_string(name)

if name in parameter_names:
raise ParameterNameConflicts(
"Multiple parameter named <{name}> "
"in route uri {uri}".format(name=name, uri=uri))
parameter_names.add(name)

parameter = Parameter(
name=name, cast=_type)
parameters.append(parameter)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def open_local(paths, mode='r', encoding='utf8'):
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
}

Expand Down
9 changes: 8 additions & 1 deletion tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from sanic import Sanic
from sanic.response import text, json
from sanic.router import RouteExists, RouteDoesNotExist
from sanic.router import RouteExists, RouteDoesNotExist, ParameterNameConflicts
from sanic.constants import HTTP_METHODS


Expand Down Expand Up @@ -935,3 +935,10 @@ async def ad_post(request, action):
assert response.json == {
'action': 'post'
}


def test_route_raise_ParameterNameConflicts(app):
with pytest.raises(ParameterNameConflicts):
@app.get('/api/v1/<user>/<user>/')
def handler(request, user):
return text('OK')

0 comments on commit 0cad831

Please sign in to comment.