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

added "ending_slash" option #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions fastapi_jsonapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(
pagination_default_limit: Optional[int] = None,
methods: Iterable[str] = (),
max_cache_size: int = 0,
ending_slash: bool = True,
) -> None:
"""
Initialize router items.
Expand Down Expand Up @@ -118,6 +119,7 @@ def __init__(
self.schema_detail = schema
# tuple and not set, so ordering is persisted
self.methods = tuple(methods) or self.DEFAULT_METHODS
self.ending_suffix = "/" if ending_slash else ""

if self.type_ in self.all_jsonapi_routers:
msg = f"Resource type {self.type_!r} already registered"
Expand Down Expand Up @@ -187,7 +189,7 @@ def _register_get_resource_list(self, path: str):
status.HTTP_200_OK: {"model": self.list_response_schema},
}
self._router.add_api_route(
path=path,
path=path + self.ending_suffix,
tags=self._tags,
responses=list_response_example | self.default_error_responses,
methods=["GET"],
Expand All @@ -201,7 +203,7 @@ def _register_post_resource_list(self, path: str):
status.HTTP_201_CREATED: {"model": self.detail_response_schema},
}
self._router.add_api_route(
path=path,
path=path + self.ending_suffix,
tags=self._tags,
responses=create_resource_response_example | self.default_error_responses,
methods=["POST"],
Expand All @@ -216,7 +218,7 @@ def _register_delete_resource_list(self, path: str):
status.HTTP_200_OK: {"model": self.detail_response_schema},
}
self._router.add_api_route(
path=path,
path=path + self.ending_suffix,
tags=self._tags,
responses=detail_response_example | self.default_error_responses,
methods=["DELETE"],
Expand All @@ -232,7 +234,7 @@ def _register_get_resource_detail(self, path: str):
self._router.add_api_route(
# TODO: variable path param name (set default name on DetailView class)
# TODO: trailing slash (optional)
path=path + "/{obj_id}",
path=path + "/{obj_id}" + self.ending_suffix,
tags=self._tags,
responses=detail_response_example | self.default_error_responses,
methods=["GET"],
Expand All @@ -248,7 +250,7 @@ def _register_patch_resource_detail(self, path: str):
self._router.add_api_route(
# TODO: variable path param name (set default name on DetailView class)
# TODO: trailing slash (optional)
path=path + "/{obj_id}",
path=path + "/{obj_id}" + self.ending_suffix,
tags=self._tags,
responses=update_response_example | self.default_error_responses,
methods=["PATCH"],
Expand All @@ -267,7 +269,7 @@ def _register_delete_resource_detail(self, path: str):
self._router.add_api_route(
# TODO: variable path param name (set default name on DetailView class)
# TODO: trailing slash (optional)
path=path + "/{obj_id}",
path=path + "/{obj_id}" + self.ending_suffix,
tags=self._tags,
responses=delete_response_example | self.default_error_responses,
methods=["DELETE"],
Expand Down
6 changes: 3 additions & 3 deletions tests/test_api/test_api_sqla_with_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,15 +1525,15 @@ async def test_create_user_and_fetch_data(self, app: FastAPI, client: AsyncClien
},
}
app.url_path_for("get_user_list")
res = await client.post("/users", json=create_user_body)
res = await client.post("/users/", json=create_user_body)
assert res.status_code == status.HTTP_201_CREATED, res.text
response_data = res.json()
assert "data" in response_data, response_data
assert response_data["data"]["attributes"] == create_user_body["data"]["attributes"]

user_id = response_data["data"]["id"]

res = await client.get(f"/users/{user_id}")
res = await client.get(f"/users/{user_id}/")
assert res.status_code == status.HTTP_200_OK, res.text
response_data = res.json()
assert "data" in response_data, response_data
Expand Down Expand Up @@ -2746,7 +2746,7 @@ async def test_filters_really_works(
params = {"filter[name]": fake_name}
assert user_1.name != fake_name
assert user_2.name != fake_name
res = await client.get("/users", params=params)
res = await client.get("/users/", params=params)
assert res.status_code == status.HTTP_200_OK, res.text
assert res.json() == {
"data": [],
Expand Down
8 changes: 4 additions & 4 deletions tests/test_api/test_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class DependencyInjectionDetailView(DetailViewBaseGeneric):

app = build_app(DependencyInjectionDetailView, resource_type="test_dependency_handler_call")
async with AsyncClient(app=app, base_url="http://test") as client:
res = await client.get("/users/1")
res = await client.get("/users/1/")

assert res.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR, res.text
assert res.json() == {
Expand Down Expand Up @@ -154,7 +154,7 @@ class DependencyInjectionDetailView(DetailViewBaseGeneric):
resource_type = fake.word()
app = build_app(DependencyInjectionDetailView, resource_type=resource_type)
async with AsyncClient(app=app, base_url="http://test") as client:
res = await client.get(f"/users/{user_1.id}", headers={"X-AUTH": "not_admin"})
res = await client.get(f"/users/{user_1.id}/", headers={"X-AUTH": "not_admin"})

assert res.status_code == status.HTTP_403_FORBIDDEN, res.text
assert res.json() == {
Expand All @@ -168,7 +168,7 @@ class DependencyInjectionDetailView(DetailViewBaseGeneric):
],
}

res = await client.get(f"/users/{user_1.id}", headers={"X-AUTH": "admin"})
res = await client.get(f"/users/{user_1.id}/", headers={"X-AUTH": "admin"})
assert res.json() == {
"data": {
"attributes": UserAttributesBaseSchema.from_orm(user_1).dict(),
Expand Down Expand Up @@ -207,7 +207,7 @@ class DependencyInjectionDetailView(DetailViewBaseGeneric):

app = build_app(DependencyInjectionDetailView, resource_type="test_manipulate_data_layer_kwargs")
async with AsyncClient(app=app, base_url="http://test") as client:
res = await client.get(f"/users/{user_1.id}")
res = await client.get(f"/users/{user_1.id}/")

assert res.status_code == status.HTTP_404_NOT_FOUND, res.text
assert res.json() == {
Expand Down
Loading