noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 507 forks source link

Dot in namespace name and blueprint fails with AssertionError #571

Open jamielennox opened 5 years ago

jamielennox commented 5 years ago

I have a Namespace defined with:

ns = api.namespace('api/1.0', description="Version 1")

and i'm trying to register the API with a blueprint like:

api = Api(blueprint,
          version='1.0', ...)

When restplus registers the resource it has to give it an endpoint name that it calculates like: https://github.com/noirbizarre/flask-restplus/blob/a8f35823fe40b2c7385632a2ad6b35b26467402c/flask_restplus/api.py#L388-L390 which has the period in the endpoint name. Because a . is a special character in Flask blueprints this fails, where it works just fine when not using a blueprint. I don't think resource.__name__ would ever have a ..

We should simply replace the . with an _ or something that is valid.

jamielennox commented 5 years ago
  File "/Users/jamielennox/.local/share/virtualenvs/presentation-CXDwjwgb/lib/python3.7/site-packages/flask_restplus/namespace.py", line 92, in wrapper
    self.add_resource(cls, *urls, **kwargs)
  File "/Users/jamielennox/.local/share/virtualenvs/presentation-CXDwjwgb/lib/python3.7/site-packages/flask_restplus/namespace.py", line 82, in add_resource
    api.register_resource(self, resource, *ns_urls, **kwargs)
  File "/Users/jamielennox/.local/share/virtualenvs/presentation-CXDwjwgb/lib/python3.7/site-packages/flask_restplus/api.py", line 266, in register_resource
    self._register_view(self.app, resource, namespace, *urls, **kwargs)
  File "/Users/jamielennox/.local/share/virtualenvs/presentation-CXDwjwgb/lib/python3.7/site-packages/flask_restplus/api.py", line 316, in _register_view
    app.add_url_rule(rule, view_func=resource_func, **kwargs)
  File "/Users/jamielennox/.local/share/virtualenvs/presentation-CXDwjwgb/lib/python3.7/site-packages/flask/blueprints.py", line 207, in add_url_rule
    assert '.' not in view_func.__name__, "Blueprint view function name should not contain dots"
AssertionError: Blueprint view function name should not contain dots
jamielennox commented 5 years ago

Change was made in 1.0: https://github.com/pallets/flask/pull/2450