First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
from fastapi import FastAPI
from pydantic import BaseModel
class Test(BaseModel):
foo: str
bar: str
class Test2(BaseModel):
test: Test
baz: str
app = FastAPI()
@app.get(
"/",
response_model=Test2,
response_model_include={'baz': ..., 'test': {'foo'}},
response_model_exclude={'test': {'bar'}}
)
def item():
return Test2(test=Test(foo="visible field", bar="invisible field"), baz="also visible field")
Description
Dictionary, passed to response_mode_include/exclude casts to set.
- Open the browser and call the endpoint
/.
- It returns a JSON with
{"baz":"also visible field"}.
- But I expected it to return
{"baz":"also visible field", "test": {"foo": "visible field"}}.
Environment
- OS: Windows
- FastAPI Version: 0.60.1
- Python version: Python 3.7.3
Additional context
So, this happens due to cast of include and exclude params to set at https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py#L37 and https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py#L39.
According to pydantic docs, this is a normal usage of include/exclude.
So, i think, fix can be simple, like appending ... and not isinstance(include, dict) to conditions. But i'm not familiar with fastapi, and not sure that fix can break something.
First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
Description
Dictionary, passed to response_mode_include/exclude casts to set.
/.{"baz":"also visible field"}.{"baz":"also visible field", "test": {"foo": "visible field"}}.Environment
Additional context
So, this happens due to cast of include and exclude params to
setat https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py#L37 and https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py#L39.According to pydantic docs, this is a normal usage of include/exclude.
So, i think, fix can be simple, like appending
... and not isinstance(include, dict)to conditions. But i'm not familiar with fastapi, and not sure that fix can break something.