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

Commit

Permalink
90 url for returns wrong path (#257)
Browse files Browse the repository at this point in the history
* Add spearation between classes while building urls

* Add test to check new behaviour

* Add changelog line

* Remove additional import
  • Loading branch information
yuval9313 authored Dec 22, 2023
1 parent ca3e9ff commit 2e4c58d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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"

0 comments on commit 2e4c58d

Please sign in to comment.