Skip to content

Commit

Permalink
added option
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoV committed Dec 20, 2024
1 parent 64b0b17 commit 1cd836a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
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

0 comments on commit 1cd836a

Please sign in to comment.