Deprecate url_for in FastAPI routes

This generalizes the error handling when trying to call `url_for` in ASGI mode.
This commit is contained in:
davelopez
2021-08-24 10:46:28 +02:00
parent b6881385d8
commit 4b75be4e8e
3 changed files with 23 additions and 4 deletions
+2 -2
View File
@@ -39,7 +39,6 @@ from typing import (
Type,
)
import routes
import sqlalchemy
from sqlalchemy.orm.scoping import scoped_session
@@ -51,6 +50,7 @@ from galaxy.schema.fields import EncodedDatabaseIdField
from galaxy.security.idencoding import IdEncodingHelper
from galaxy.structured_app import BasicApp, MinimalManagerApp
from galaxy.util import namedtuple
from galaxy.web import url_for as gx_url_for
log = logging.getLogger(__name__)
@@ -554,7 +554,7 @@ class ModelSerializer(HasAModelManager):
item_dict = MySerializer.serialize( my_item, keys_to_serialize )
"""
#: 'service' to use for getting urls - use class var to allow overriding when testing
url_for = staticmethod(routes.url_for)
url_for = staticmethod(gx_url_for)
default_view: Optional[str]
views: Dict[str, List[str]]
+16 -1
View File
@@ -4,4 +4,19 @@ Galaxy web application framework
from . import base
url_for = base.routes.url_for
DEPRECATED_URL_ATTRIBUTE_MESSAGE = "*deprecated attribute, URL not filled in by server*"
def handle_url_for(*args, **kargs) -> str:
"""Tries to resolve the URL using the `routes` module.
This only works in a WSGI app so a deprecation message is returned
when running an ASGI app.
"""
try:
return base.routes.url_for(*args, **kargs)
except AttributeError:
return DEPRECATED_URL_ATTRIBUTE_MESSAGE
url_for = handle_url_for
+5 -1
View File
@@ -1,5 +1,6 @@
from typing import List
from galaxy.web.framework import DEPRECATED_URL_ATTRIBUTE_MESSAGE
from galaxy_test.base.populators import DatasetPopulator
from ._framework import ApiTestCase
@@ -67,7 +68,10 @@ class GroupRolesApiTestCase(ApiTestCase):
self._assert_status_code_is_ok(update_response)
group_role = update_response.json()
self._assert_valid_group_role(group_role, assert_id=encoded_role_id)
assert (group_role["url"] == f"/api/groups/{encoded_group_id}/roles/{encoded_role_id}" or group_role["url"] == "*deprecated attribute not filled in by FastAPI server*")
assert (
group_role["url"] == f"/api/groups/{encoded_group_id}/roles/{encoded_role_id}"
or group_role["url"] == DEPRECATED_URL_ATTRIBUTE_MESSAGE
)
def test_update_only_admin(self):
encoded_group_id = "any-group-id"