Skip to content

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

@autra

Description

@autra

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!

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions