Skip to content

OpenAPI generation fails with dates with Annotated constraint #11687

Discussion options

You must be logged in to vote

Well if it's a Body param you need to use POST instead of GET. Taking that in mind, you can rewrite the code as

from datetime import date, datetime
from typing import Annotated
from pydantic import BaseModel, Field
from fastapi import FastAPI

app = FastAPI()

class DataInput(BaseModel):
    birthdate: Annotated[date, Field(gt=datetime(1900, 1, 1).timestamp(), lt=datetime(2000, 1, 1).timestamp())]

@app.post("/")
def read_root(
    data_input: DataInput
):
    return {"birthdate": data_input.birthdate}

Replies: 1 comment 12 replies

Comment options

You must be logged in to vote
12 replies
@jereflit
Comment options

@JavierSanchezCastro
Comment options

Answer selected by jereflit
@jereflit
Comment options

@JavierSanchezCastro
Comment options

@jereflit
Comment options

@JavierSanchezCastro
Comment options

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