Skip to content

datatime json_encoders breaks docs #10619

Discussion options

You must be logged in to vote

Check this one out:

import datetime
from fastapi import FastAPI
from pydantic import Field, BaseModel

app = FastAPI()

class Task(BaseModel):
    created_at: datetime.datetime = Field(
        ...,
        description="The creation datetime of the task.",
        example="2023-01-01T00:00:00Z",
    )
    class Config:
        json_encoders = {
            datetime.datetime: lambda v: v.isoformat(),
        }


@app.post("/task/", response_model=Task)
async def create_task():
    return Task(
        created_at=datetime.datetime.utcnow()
    )

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@CharlesJu1
Comment options

@hasansezertasan
Comment options

Answer selected by CharlesJu1
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