question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Casting dictionary in response_model_include/response_model_exclude to set.

See original GitHub issue

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 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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tiangolocommented, Jul 4, 2021

Thanks for the very detailed and reproducible example, and thanks for the PR with the fix @Rubikoid! 🙇 🍰

Also thanks for coming back to close the issue. 👏

And thanks everyone for the discussion here.

This is now available in FastAPI 0.66.0 🎉

1reaction
andreixkcommented, Dec 18, 2020

The opposite happens if you have a nested list, e.g.:

class Test2(BaseModel):
    test: [Test]
    baz: str

app = FastAPI()

@app.get(
    "/",
    response_model=Test2,
    response_model_exclude={'test': {'__all__': {'bar'}}}
    )
def item():
    return Test2(test=[Test(foo="visible field", bar="invisible field")], baz="also visible field")

according to Pydantic doc that should’ve excluded bar field from all items of the test list, however, it removes test list entirely

Read more comments on GitHub >

github_iconTop Results From Across the Web

Casting Definition & Meaning | Dictionary.com
the act or process of choosing actors to play the various roles in a theatrical production, motion picture, etc. the act or skill...
Read more >
C# cast Dictionary<string, AnyType> to ... - Stack Overflow
Following AakashM's answer, the Cast doesn't seem to play ball. You can get around it by using a little helper method though:
Read more >
Casting Definition & Meaning - Merriam-Webster
The meaning of CASTING is something (such as the excrement of an earthworm) that is cast out or off. How to use casting...
Read more >
Python | Convert string dictionary to dictionary - GeeksforGeeks
This article discusses yet another problem of interconversion of the dictionary, in string format to a dictionary.
Read more >
Dictionary<TKey,TValue> Class (System.Collections.Generic)
Create a new dictionary of strings, with string keys. // Dictionary<string, string> openWith ... Casts the elements of an IEnumerable to the specified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found