fastapi-cloudevents icon indicating copy to clipboard operation
fastapi-cloudevents copied to clipboard

CloudEvents are not well represented in response schema in the apidoc.

Open autra opened this issue 7 months ago • 1 comments

Describe the bug

When configuring response_model, and/or typing the return value, or setting different response model for some http code, the openapi.json (and therefore /docs and /redoc) does not contain the correct schema nor the correct example.

To Reproduce

Given the following snippet:

from typing import Literal
from fastapi import FastAPI
from fastapi_cloudevents.cloudevent_response import StructuredCloudEventResponse
from fastapi_cloudevents import CloudEvent

app = FastAPI()


class Item(CloudEvent):
    type: Literal["my.type.v1"]
    name: str
    price: float

class Error(CloudEvent):
    message: str

@app.post(
    "/items/",
    response_class=StructuredCloudEventResponse,
    response_model=Item,
    responses={429: {"model": Error}},
    )
async def create_item(item: Item) -> Item:
    return item


@app.get("/items/",
    response_class=StructuredCloudEventResponse,
    response_model=Item,
    responses={429: {"model": Error}},
    )
async def read_items() -> Item:
    return Item(name="Portal Gun", price=42.0, type="my.type.v1"),

  1. start fastapi with uvicorn --port 9999 utils.test:app --reload for instance (if the file is utils/test.py)
  2. load localhost:9999/docs

Expected behavior the response model both for 200 and 409 are correctly documented

Actual behaviour The response models are some generic json, and aren't CloudEvent instances. See screenshot.

Screenshots

Image

System Details (please complete the following information):

  • Python Version: 3.12.11
  • OS: Linux
  • FastAPI Version: 0.116.1

Thanks!

autra avatar Sep 16 '25 14:09 autra

Hi @autra,

Thank you so much for reporting this bug.

I will look into that during the weekend.

Best, Alexander

sasha-tkachev avatar Sep 16 '25 17:09 sasha-tkachev