Fastapi: [BUG] response_model_include in the path operation decorator has no effect in the openapi spec

Created on 19 May 2020  路  2Comments  路  Source: tiangolo/fastapi

Describe the bug

path operation decorator has an option called "response_model_include" which allows us
to select only certain sections of the complete response model.
this is extremely useful where i can just reuse the same model in different
APIs and return only the requested data.
But when the openAPI spec is rendered, i see the ENTIRE response_model in the spec response.
instead it should ONLY show the members included in response_model_include option.

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

Replace each part with your own scenario:

  1. Create a file with:
from fastapi import FastAPI

app = FastAPI()


@app.get("/status",
                  response_model=customerEvent,
                  response_model_include={"status"})
def get_customer_statuus():
    return customerEvent(status="Available")
  1. run the above at port 5000 for example
  2. Open the browser and call the endpoint 'localhost:5000/redoc'.
  3. the openapi Spec for 200 status code shows the ENTIRE customerEvent schema.
  4. But I expected the spec to only show {"status": str}.

Expected behavior

expected the spec to only show {"status": str}. which is the only included member of the
customerEvent class

python -c "import fastapi; print(fastapi.__version__)"
**0.54.1**

  • Python version, get it with:
python --version
Python 3.7.7
answered question

All 2 comments

Yeah, that's why you should have the specific model that should be returned in each specific path operation.

It's documented here: https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude

But it is still recommended to use the ideas above, using multiple classes, instead of these parameters.

This is because the JSON Schema generated in your app's OpenAPI (and the docs) will still be the one for the complete model, even if you use response_model_include or response_model_exclude to omit some attributes.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings