-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix recursive ORM parsing error #2718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nuno-andre Please update with tests and change file thanks
|
@PrettyWood Should I create a new test case or would you prefer I adapt |
The _ORM mode_ doesn't parse dictionaries if it's not explicitly called.
|
Thank you! |
|
This change actually prevents models (that are ORM-enabled) from being initialized the normal way from dictionaries. Prior to this change, the following code worked: from types import SimpleNamespace
from pydantic import BaseModel
class User(BaseModel):
first_name: str
last_name: str
class Config:
orm_mode = True
class State(BaseModel):
user: User
class Config: # (The issue happens regardless of whether the outer class has orm_mode set)
orm_mode = True
# Pass an "orm instance"
state = State.from_orm(SimpleNamespace(user=SimpleNamespace(first_name='John', last_name='Appleseed')))
# Pass dictionary data directly
state = State(**{'user': {'first_name': 'John', 'last_name': 'Appleseed'}})After this change, the final line throws a validation error: This is actually a bit of an issue given the way FastAPI handles response models. During the processing of a request, it serializes the data, and then passes it through a response model (if one was provided) to make sure the response matches the desired output schema. If you're using an ORM-mode model for convenience (like one generated from Tortoise-ORM's Edit: I've opened an issue about this for the sake of organization - #3181 |
The _ORM mode_ doesn't parse dictionaries if it's not explicitly called.
The ORM mode doesn't parse dictionaries if it's not explicitly called.
Change Summary
Prepend the
orm_modechecking to thedict's.Related issue number
Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)
Example