Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dot in query name alias #239

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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