From 421ed187576a229e77e31403ef472cf381c481be Mon Sep 17 00:00:00 2001 From: Stephen Rauch Date: Tue, 5 Oct 2021 07:25:25 -0700 Subject: [PATCH 1/3] Allow dot in query name alias to fix #238 --- ninja/signature/details.py | 8 ++++++-- tests/main.py | 11 ++++++++++- tests/test_query.py | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ninja/signature/details.py b/ninja/signature/details.py index 6eec55387..c9b2d1bd8 100644 --- a/ninja/signature/details.py +++ b/ninja/signature/details.py @@ -25,6 +25,10 @@ class ViewSignature: + FLATTEN_PATH_SEP = ( + "\x1e" # ASCII Record Separator. IE: not generally used in query names + ) + def __init__(self, path: str, view_func: Callable) -> None: self.view_func = view_func self.signature = get_typed_signature(self.view_func) @@ -157,7 +161,7 @@ def _args_flatten_map(self, args: List[FuncParam]) -> Dict[str, Tuple[str, ...]] raise ConfigError( f"Duplicated name: '{name}' in params: '{arg_names[name]}' & '{arg.name}'" ) - flatten_map[name] = tuple(path.split(".")) + flatten_map[name] = tuple(path.split(self.FLATTEN_PATH_SEP)) arg_names[name] = arg.name else: name = arg.alias @@ -173,7 +177,7 @@ def _args_flatten_map(self, args: List[FuncParam]) -> Dict[str, Tuple[str, ...]] def _model_flatten_map(self, model: TModel, prefix: str) -> Generator: for field in model.__fields__.values(): field_name = field.alias - name = f"{prefix}.{field_name}" + name = f"{prefix}{self.FLATTEN_PATH_SEP}{field_name}" if is_pydantic_model(field.type_): yield from self._model_flatten_map(field.type_, name) else: diff --git a/tests/main.py b/tests/main.py index 63e2540cd..20a9cdb4c 100644 --- a/tests/main.py +++ b/tests/main.py @@ -2,7 +2,7 @@ from django.urls import register_converter -from ninja import Path, Query, Router +from ninja import Field, Path, Query, Router, Schema router = Router() @@ -225,6 +225,15 @@ def get_query_param_required_type(request, query: int = Query(...)): return f"foo bar {query}" +class AliasedSchema(Schema): + query: str = Field(..., alias="aliased.-_~name") + + +@router.get("/query/aliased-name") +def get_query_aliased_name(request, query: AliasedSchema = Query(...)): + return f"foo bar {query.query}" + + class CustomPathConverter1: regex = "[0-9]+" diff --git a/tests/test_query.py b/tests/test_query.py index b4f91e47d..d3f1f3079 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -54,6 +54,7 @@ ("/query/param-required/int", 422, response_missing), ("/query/param-required/int?query=50", 200, "foo bar 50"), ("/query/param-required/int?query=foo", 422, response_not_valid_int), + ("/query/aliased-name?aliased.-_~name=foo", 200, "foo bar foo"), ], ) def test_get_path(path, expected_status, expected_response): From be25b26646bc00f2013a1e6f2a5b61f50608472b Mon Sep 17 00:00:00 2001 From: Vitaliy Kucheryaviy Date: Tue, 5 Oct 2021 18:49:13 +0300 Subject: [PATCH 2/3] python 3.10 tests --- .github/workflows/test_full.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_full.yml b/.github/workflows/test_full.yml index 0e3cd1e6f..4bea6dcfd 100644 --- a/.github/workflows/test_full.yml +++ b/.github/workflows/test_full.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.6', '3.7', '3.8', '3.9'] + python-version: ['3.6', '3.7', '3.8', '3.9', '3.10'] django-version: ['<2.1', '<2.2', '<3.0', '<3.1', '<3.2', '<3.3'] steps: From ba7e47f7851b32fa31ffc4ad7b202923b5de5f5d Mon Sep 17 00:00:00 2001 From: Vitaliy Kucheryaviy Date: Tue, 5 Oct 2021 21:31:03 +0300 Subject: [PATCH 3/3] python 3.10 still not available in github actions --- .github/workflows/test_full.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_full.yml b/.github/workflows/test_full.yml index 4bea6dcfd..0e3cd1e6f 100644 --- a/.github/workflows/test_full.yml +++ b/.github/workflows/test_full.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.6', '3.7', '3.8', '3.9', '3.10'] + python-version: ['3.6', '3.7', '3.8', '3.9'] django-version: ['<2.1', '<2.2', '<3.0', '<3.1', '<3.2', '<3.3'] steps: