Merge pull request #1364 from yunstanford/raise-exception-when-param-conflicts
Raise exception when param conflicts
This commit is contained in:
commit
0cad831eca
|
@ -38,6 +38,10 @@ class RouteDoesNotExist(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class ParameterNameConflicts(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Router:
|
||||
"""Router supports basic routing with parameters and method checks
|
||||
|
||||
|
@ -195,12 +199,19 @@ class Router:
|
|||
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)
|
||||
|
|
1
setup.py
1
setup.py
|
@ -48,6 +48,7 @@ setup_kwargs = {
|
|||
'License :: OSI Approved :: MIT License',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
],
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import pytest
|
|||
|
||||
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
|
||||
|
||||
|
||||
|
@ -935,3 +935,10 @@ def test_uri_with_different_method_and_different_params(app):
|
|||
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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user