dmontagu / fastapi-utils

Reusable utilities for FastAPI
MIT License
1.89k stars 165 forks source link

[BUG] cbv and path params create strange behaviour #264

Open kickIDnoah opened 1 year ago

kickIDnoah commented 1 year ago

While using the @cbv i encountered the problem that i couldn't use the endpoint without path params on a similar path. It somehow messes with the validator, i think?

from fastapi import APIRouter, FastAPI
from fastapi_utils.cbv import cbv
from pydantic.types import UUID4
from starlette.testclient import TestClient

router = APIRouter()

@cbv(router)
class CBV:

    @router.post("/test")
    def test_post(self):
        return ''

    @router.get("/test/{uuid}")
    def test_post(self, uuid: UUID4):
        return uuid

app = FastAPI()
app.include_router(router)
client = TestClient(app)

print(client.post("/test").json())

output:

{
  "detail":[
    {
      "loc":[
        "query",
        "self"
      ],
      "msg":"field required",
      "type":"value_error.missing"
    }
  ]
}

Expected behavior both endpoints should be just available

Environment:

fastapi_utils_version: 0.2.1
fastapi_version: 0.85.2
pydantic version: 1.10.2
pydantic compiled: True
install path: ...\fastapi_test\venv\Lib\site-packages\pydantic
python version: 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)]
platform: Windows-10-10.0.22621-SP0
optional deps. installed: ['dotenv', 'typing-extensions']
hgalytoby commented 1 year ago

Your function names cannot be repeated.

@cbv(router)
class CBV:

    @router.post("/test")
    def test_post(self):
        return ''

    @router.get("/test/{uuid}")
    def test_get(self, uuid: UUID4):
        return uuid