This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Silently add __root__ or document the Pydantic dependency #2909
Comments
That was added #1524.
Expected behaviour since the syntax looks valid.
Again, valid according to your model, and FastAPI has no idea about what is going on here. See the schema of your model. >>> your_model.schema()
{'title': 'Bananas', 'type': 'object', 'additionalProperties': {'$ref': '#/definitions/Banana'}, 'definitions': {'Banana': {'title': 'Banana', 'type': 'object', 'properties': {'whatever': {'title': 'Whatever', 'type': 'number'}, 'foo': {'title': 'Foo', 'type': 'string'}}, 'required': ['whatever', 'foo']}}} There is nothing called
You are missing that Bananas(__root__={"dummy": {"whatever": 12.3, "foo": "abc"}}) But the |
Sorry, I am not understanding your reply. Release #1524 squashed the
Finally, you said that should not use
when the model is initialised using a function call
Thanks for patience 😄 |
Here's a screenshot of the success request. And again as curl
I am curious to know if you see the same on your setup. |
Thanks for the help @ycd! 🙇 @snow6oy In FastAPI you would rarely need/want to use from pydantic import BaseModel
class Banana(BaseModel):
whatever: float
foo: str
app = FastAPI()
@app.post("/bananas")
async def post_bananas(b: dict[str, Banana]):
print(b) # returns a dictionary: I would think that's probably a better solution to the final intention (independent of
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Example
Description
When defining a Pydantic model with a custom root I would like it so that either:
A. The example displayed by FastAPI at /docs displays the "root" key that my client needs to send OR (even better)
B. The client does not receive a 422 error when the key is omitted and instead FastAPI silently provides it for me
Open the browser and call the endpoint
localhost:8000/docs
.It displays a JSON example with
{ "additionalProp1": { "whatever": 0, "foo": "string" }, "additionalProp2": { "whatever": 0, "foo": "string" }, "additionalProp3": { "whatever": 0, "foo": "string" } }
.But I expected it to prefix with
{"__root__": "{ ...}" }
.Environment
Additional context
The text was updated successfully, but these errors were encountered: