From bd9105139974dd60533e1f1744f1c2866f3c7b27 Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Mon, 22 May 2023 11:19:31 +0200 Subject: [PATCH 1/3] aiohttp auto-unencode --- .../catalog/_handlers.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/services/web/server/src/simcore_service_webserver/catalog/_handlers.py b/services/web/server/src/simcore_service_webserver/catalog/_handlers.py index f5df935b39a..65fbea585b4 100644 --- a/services/web/server/src/simcore_service_webserver/catalog/_handlers.py +++ b/services/web/server/src/simcore_service_webserver/catalog/_handlers.py @@ -10,7 +10,7 @@ import orjson from aiohttp import web from aiohttp.web import Request, RouteTableDef -from models_library.services import ServiceInput, ServiceKeyEncoded, ServiceOutput +from models_library.services import ServiceInput, ServiceOutput from models_library.services_resources import ( ServiceResourcesDict, ServiceResourcesDictHelpers, @@ -74,7 +74,7 @@ def create(cls, request: Request) -> "_RequestContext": class _ServicePathParams(BaseModel): - service_key: ServiceKeyEncoded + service_key: ServiceKey service_version: ServiceVersion class Config: @@ -105,7 +105,9 @@ async def get_service_handler(request: Request): ctx = _RequestContext.create(request) path_params = parse_request_path_parameters_as(_ServicePathParams, request) - data = await get_service(path_params.service_key, path_params.service_version, ctx) + data = await get_service( + path_params.service_key.to_service_key(), path_params.service_version, ctx + ) return envelope_json_response(data) @@ -120,7 +122,10 @@ async def update_service_handler(request: Request): # Evaluate and return validated model data = await update_service( - path_params.service_key, path_params.service_version, update_data, ctx + path_params.service_key, + path_params.service_version, + update_data, + ctx, ) return envelope_json_response(data) @@ -157,7 +162,10 @@ async def get_service_input_handler(request: Request): # Evaluate and return validated model response_model = await get_service_input( - path_params.service_key, path_params.service_version, path_params.input_key, ctx + path_params.service_key, + path_params.service_version, + path_params.input_key, + ctx, ) data = response_model.dict(**RESPONSE_MODEL_POLICY) @@ -357,7 +365,6 @@ async def update_service( async def list_service_inputs( service_key: ServiceKey, service_version: ServiceVersion, ctx: _RequestContext ) -> list[ServiceOutputGet]: - service = await client.get_service( ctx.app, ctx.user_id, service_key, service_version, ctx.product_name ) @@ -376,7 +383,6 @@ async def get_service_input( input_key: ServiceInputKey, ctx: _RequestContext, ) -> ServiceInputGet: - service = await client.get_service( ctx.app, ctx.user_id, service_key, service_version, ctx.product_name ) @@ -471,7 +477,6 @@ async def get_compatible_outputs_given_target_input( to_input_key: ServiceInputKey, ctx: _RequestContext, ) -> list[ServiceOutputKey]: - # N outputs service_outputs = await list_service_outputs(service_key, service_version, ctx) From 83a9620f989f55ab92a1b59754ab060a7ca71ce0 Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Mon, 22 May 2023 12:29:22 +0200 Subject: [PATCH 2/3] clean --- .../server/src/simcore_service_webserver/catalog/_handlers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/web/server/src/simcore_service_webserver/catalog/_handlers.py b/services/web/server/src/simcore_service_webserver/catalog/_handlers.py index 65fbea585b4..ae6c1a2c7d6 100644 --- a/services/web/server/src/simcore_service_webserver/catalog/_handlers.py +++ b/services/web/server/src/simcore_service_webserver/catalog/_handlers.py @@ -105,9 +105,7 @@ async def get_service_handler(request: Request): ctx = _RequestContext.create(request) path_params = parse_request_path_parameters_as(_ServicePathParams, request) - data = await get_service( - path_params.service_key.to_service_key(), path_params.service_version, ctx - ) + data = await get_service(path_params.service_key, path_params.service_version, ctx) return envelope_json_response(data) From c5c135be8a2be6049c423aa5796da0e910b0acf2 Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Mon, 22 May 2023 15:04:07 +0200 Subject: [PATCH 3/3] fix test --- .../simcore_service_webserver/catalog/_handlers.py | 12 ++++++++++-- .../tests/unit/with_dbs/01/test_catalog_api.py | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/services/web/server/src/simcore_service_webserver/catalog/_handlers.py b/services/web/server/src/simcore_service_webserver/catalog/_handlers.py index ae6c1a2c7d6..6659915625c 100644 --- a/services/web/server/src/simcore_service_webserver/catalog/_handlers.py +++ b/services/web/server/src/simcore_service_webserver/catalog/_handlers.py @@ -5,6 +5,7 @@ """ import logging +import urllib.parse from typing import Any, Iterator import orjson @@ -17,7 +18,7 @@ ) from models_library.users import UserID from pint import UnitRegistry -from pydantic import BaseModel, Extra, Field +from pydantic import BaseModel, Extra, Field, validator from servicelib.aiohttp.requests_validation import ( handle_validation_as_http_error, parse_request_path_parameters_as, @@ -81,6 +82,14 @@ class Config: allow_population_by_field_name = True extra = Extra.forbid + @validator("service_key", pre=True) + @classmethod + def ensure_unquoted(cls, v): + # NOTE: this is needed as in pytest mode, the aiohttp server does not seem to unquote automatically + if v is not None: + return urllib.parse.unquote(v) + return v + @routes.get(f"{VTAG}/catalog/services") @login_required @@ -284,7 +293,6 @@ async def get_service_resources_handler(request: Request): """ ctx = _RequestContext.create(request) path_params = parse_request_path_parameters_as(_ServicePathParams, request) - service_resources: ServiceResourcesDict = await client.get_service_resources( request.app, user_id=ctx.user_id, diff --git a/services/web/server/tests/unit/with_dbs/01/test_catalog_api.py b/services/web/server/tests/unit/with_dbs/01/test_catalog_api.py index 5b814f5a56f..9515e25f378 100644 --- a/services/web/server/tests/unit/with_dbs/01/test_catalog_api.py +++ b/services/web/server/tests/unit/with_dbs/01/test_catalog_api.py @@ -3,6 +3,7 @@ # pylint:disable=redefined-outer-name import re +import urllib.parse import pytest from aiohttp import web @@ -118,7 +119,7 @@ async def test_get_service_resources( assert client.app assert client.app.router url = client.app.router["get_service_resources_handler"].url_for( - service_key="simcore%2Fservices%2Fdynamic%2Fsomeservice", + service_key=urllib.parse.quote("simcore/services/dynamic/someservice", safe=""), service_version="3.4.5", ) response = await client.get(f"{url}")