Skip to content
Discussion options

You must be logged in to vote

This seems to be working:

from typing import Annotated

from fastapi import FastAPI, Path

app = FastAPI()

item_id_type = Annotated[
    str,
    Path(description="This is my Item ID", ge=10, le=50, example=1),
]


@app.get("/route_a")
def operation_a(item_id: Annotated[item_id_type, Path(example=12)]) -> str:
    if item_id == 17:
        raise ValueError("item with id 17 doesnt support this operation")
    return f"id: {item_id}"


@app.get("/route_b")
def operation_b(item_id: Annotated[item_id_type, Path(example=17)]) -> str:
    if item_id == 12:
        raise ValueError("item with id 12 doesnt support this operation")
    return f"id: {item_id}"

BTW, ge and le are not working for str

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants