Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

90 url for returns wrong path #257

Merged
merged 5 commits into from
Dec 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Latest changes

* Fix bug where `Request.url_for` is not working as intended [[yuval9313/FastApi-RESTful#90](https://github.com/yuval9313/FastApi-RESTful/issues/90)]
* Update multiple dependencies using @dependebot
* Fix `repeat_every` is only running once [#142](https://github.com/yuval9313/FastApi-RESTful/pull/142)

Expand Down
1 change: 1 addition & 0 deletions fastapi_restful/cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None:
router.routes.remove(route)
route.path = route.path[prefix_length:]
_update_cbv_route_endpoint_signature(cls, route)
route.name = cls.__name__ + "." + route.name
cbv_router.routes.append(route)
router.include_router(cbv_router)

Expand Down
19 changes: 18 additions & 1 deletion tests/test_cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, ClassVar, Optional

import pytest
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Request
from starlette.testclient import TestClient

from fastapi_restful.cbv import cbv
Expand Down Expand Up @@ -130,3 +130,20 @@ def root(self) -> str:
response = client.get("/api/item")
assert response.status_code == 200
assert response.json() == "hello"

def test_url_for(self, router: APIRouter) -> None:
@cbv(router)
class Foo:
@router.get("/foo")
def example(self, request: Request) -> str:
return str(request.url_for("Bar.example"))

@cbv(router)
class Bar:
@router.get("/bar")
def example(self, request: Request) -> str:
return str(request.url_for("Foo.example"))

client = TestClient(router)
response = client.get("/foo")
assert response.json() == "http://testserver/bar"
Loading