Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python 3.x - FastAPI. Return same structure in all the responses

I'm working in develop an middleware with FastAPI. I want keep the same response for all the responses like:

/foo/

{
    "status": "success",
    "code": 200,
    "msg": [foo, foo, foo]
 }

/foo/1/

{
    "status": "success",
    "code": 200,
    "msg": foo
 }

It's works with the following code:

class AppOutputSchema(pydantic.BaseModel):
    """
    Standard output schema class of the application
    """
    status: str = "success"
    code: int = 200
    msg: Any

@router.get("/", response_model=AppOutputSchema, status_code=200)
async def read_all_foo() -> AppOutputSchema:
    foo_list = await read_all_foo()
    return AppOutputSchema(msg=foo_list)

But the documentation generated in /docs it's wrong. The response schema is always {status, code, msg} for all the endpoints.

swagger documentation

The documentation should have the foo schema definition.

I read the documentation and I can't find anything about it.

Any idea?

question from:https://stackoverflow.com/questions/65907509/fastapi-return-same-structure-in-all-the-responses

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...