-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Closed
Labels
Description
First check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google "How to X in FastAPI" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
Here's a self-contained, minimal, reproducible, example with my use case:
from typing import List
from fastapi.encoders import jsonable_encoder
from pydantic.main import BaseModel
class YourBaseModel(BaseModel):
bar: int
baz: int
class MyBaseModel(BaseModel):
nest: List[YourBaseModel] = None
foo: int
model = MyBaseModel(nest=[YourBaseModel(bar=2, baz=3), YourBaseModel(bar=4, baz=5)], foo=1)
exclude_pattern={'nest': {'__all__': {'baz'}}}
expected={"nest":[{"bar": 2}, {"bar": 4}], "foo": 1}
def test_pydantic_nested_exclude():
assert model.dict(exclude=exclude_pattern) == expected # Passed
def test_jsonable_encoder_nested_exclude():
assert jsonable_encoder(model, exclude=exclude_pattern) == expected # Failed
# actual={'foo': 1} ## i.e. removes all nested elements!Description
- Define a nested model e.g.
{"nest":[{"bar": 2, baz=3}, {"bar": 4, baz=5}], "foo": 1} - Try to exclude one of the fields in the nested model (e.g.
baz) usingjsonable_encoder - It excludes the whole nested field altogether i.e.
nestand returns{'foo': 1} - But I expected it to return
{"nest":[{"bar": 2}, {"bar": 4}], "foo": 1}.
Environment
- OS: Windows
- FastAPI Version: 0.65.1
- Python version: 3.8.2
Additional context
I followed along with issue #2594 and it said the behaviour explained in https://pydantic-docs.helpmanual.io/usage/exporting_models/#advanced-include-and-exclude should work but this doesn't seem to be the case.
What am I doing wrong? :<
Thanks in advance.