Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
45 lines
939 B
Python
45 lines
939 B
Python
from typing import List
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.routing import APIRoute
|
|
from pydantic import BaseModel
|
|
|
|
|
|
def custom_generate_unique_id(route: APIRoute):
|
|
return f"{route.tags[0]}-{route.name}"
|
|
|
|
|
|
app = FastAPI(generate_unique_id_function=custom_generate_unique_id)
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
price: float
|
|
|
|
|
|
class ResponseMessage(BaseModel):
|
|
message: str
|
|
|
|
|
|
class User(BaseModel):
|
|
username: str
|
|
email: str
|
|
|
|
|
|
@app.post("/items/", response_model=ResponseMessage, tags=["items"])
|
|
async def create_item(item: Item):
|
|
return {"message": "Item received"}
|
|
|
|
|
|
@app.get("/items/", response_model=List[Item], tags=["items"])
|
|
async def get_items():
|
|
return [
|
|
{"name": "Plumbus", "price": 3},
|
|
{"name": "Portal Gun", "price": 9001},
|
|
]
|
|
|
|
|
|
@app.post("/users/", response_model=ResponseMessage, tags=["users"])
|
|
async def create_user(user: User):
|
|
return {"message": "User received"}
|