Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vitalik/django-ninja
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Oct 5, 2021
2 parents 6670b01 + ba7e47f commit c865a8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ninja/signature/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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]+"

Expand Down
1 change: 1 addition & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c865a8b

Please sign in to comment.