[Python] How to display both application/json and application/octet-stream content types in...

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have an endpoint that can return JSON or a file (xlsx):

    class ReportItemSerializer(BaseModel):
    id: int
    werks: Annotated[WerkSerializerWithNetwork, Field(validation_alias="werk")]
    plu: Annotated[str, Field(description="Название PLU")]
    start_date: Annotated[date, None, Field(description="Дата начала периода в формате")]
    end_date: Annotated[date | None, Field(description="Дата окончания периода")]
    anfmenge: Annotated[int | None, Field(des cription="Запас на начало периода")]
    endmenge: Annotated[int | None, Field(description="Остаток по PLU в разрезе поставщика на конец периода")]
    soll: Annotated[int, None, Field(description="Поступило за период")]
    haben: Annotated[
    int | None, Field(description="Количество по PLU, которое было возвращено поставщику за указанный период")]
    model_config = ConfigDict(from_attributes=True)


    class PaginatedReportItemsSerializer(BaseModel):
    count: int
    results: list[ReportItemSerializer]


    @router.get(
    "/orders/report",
    responses={
    200: {
    "description": "Return report",
    "content": {
    "application/json": {
    "schema": PaginatedReportItemsSerializer.schema(ref_template="#/components/schemas/{model}")
    },
    "application/octet-stream": {}
    },
    },
    }
    )
    async def get_report():
    pass


    With this I have a problem with swagger. With configuration above there is problem:

    [​IMG]

    Nested ReportItemSerializer is not being displayed (string is instead displayed).

    How can I fix it?

    Continue reading...

Compartilhe esta Página