跳转至

响应

responses⚓︎

By default, you can pass a BaseModel to automatically generate the application/json schema.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from pydantic import BaseModel

from star_openapi import OpenAPI

app = OpenAPI()


class BookModel(BaseModel):
    name: str
    age: int


@app.post("/book2", responses={200: BookModel})
def response(body: BookModel):
    ...

You can customize the schema:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@app.post(
    "/book3",
    responses={
        "200": {
            "description": "Custom OK",
            "content": {"application/custom+json": {"schema": BookModel.model_json_schema()}},
        }
    },
)
def response(body: BookModel):
    ...

Other content types, such as text/csv

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from http import HTTPStatus
from starlette.requests import Request


@app.post(
    "/book3",
    responses={
        HTTPStatus.OK: {
            "description": "Custom csv OK",
            "content": {"text/csv": {"schema": {"type": "string"}}}
        }
    },
)
def response(request: Request):
    ...

you can use string, int, and HTTPStatus as response's key.