Replies: 12 comments
-
If anyone wants to submit a PR to fix this I'd be happy to review it. (I think it's worth handling this properly.) |
Beta Was this translation helpful? Give feedback.
-
For now created issue for |
Beta Was this translation helpful? Give feedback.
-
I wouldn't recommend using But in FastAPI, everywhere you can use a Pydantic model you can also use what would be the (arguably?) most "Pythonic" way, using
Given that, as it's still valid Pydantic, I would be happy to support it if someone wants to add a PR with it (as @dmontagu says). |
Beta Was this translation helpful? Give feedback.
-
@tiangolo I understands that |
Beta Was this translation helpful? Give feedback.
-
@tiangolo Supporting pydantic root types would allow a single validator (defined in the wrapper class) to be run on all objects of a certain type- otherwise, the validator must be specified in each object that has a child of that type (as far as I can tell- I'm new to fastAPI, please let me know if there's a better way). |
Beta Was this translation helpful? Give feedback.
-
as @sidmani also mentioned, I'm running into wanting the ability to be able to say: pydantic_list_as_root.dict() and the above output a dict. Rather than having to manually loop through my However, I do appreciate what @tiangolo is trying to achieve by keeping things as pythonic as possible, but I would imagine that many if not all FastAPI implementations heavily rely on Pydantic for defining schemas. Therefore, I think it would be a great idea to embrace all/most of its capabilities. |
Beta Was this translation helpful? Give feedback.
-
Yeah, I would be happy to support it if someone wants to add a PR with it. |
Beta Was this translation helpful? Give feedback.
-
This should be fixed in #1524 by @patrickkwang 🎉 🚀 Available in FastAPI I'm re-opening this to let the original issue author confirm and close it directly 😄 |
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
When a custom root type is used together with a nested model, FastAPI does not seem to export the nested model. For example Might it be better if Pydantic was offered on an opt-in basis? |
Beta Was this translation helpful? Give feedback.
-
@snow6oy You should create another issue since the original issue has been resolved (and I am not entirely sure if this is a Pydantic or FastAPI issue) or no one will pay attention to the reply to a closed issue. |
Beta Was this translation helpful? Give feedback.
-
The fix in #1524 is not working for me as I'm using multiple levels of nested models with custom roots. This causes an error in Its's an easy fix though I'm not sure if it renders #1524 redundant? PR here: #4428 Temp fix for those in a hurry:
|
Beta Was this translation helpful? Give feedback.
-
Describe the bug
https://pydantic-docs.helpmanual.io/usage/models/#custom-root-types
Pydantic allows to create models with only
__root__
field. In such scenario the model behaves as transparent wrapper for this single type.When such model is used in response (request also?) fastapi does not treat it correctly and renders it as object with
__root__
field.Object is treated correctly by pydantic itself.
To Reproduce
Expected behavior
The response should be:
but at the moment is:
Screenshots
N/A
Environment
Additional context
N/A
Beta Was this translation helpful? Give feedback.
All reactions