Skip to content
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

validation_error_definition schema is incorrect when detecting an error in an array #3790

Closed
9 tasks done
silversurfer34 opened this issue Aug 30, 2021 · 7 comments
Closed
9 tasks done
Labels
answered bug Something isn't working reviewed

Comments

@silversurfer34
Copy link

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.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Optional, List

from fastapi import FastAPI
from pydantic import BaseModel

class SubItem(BaseModel):
    price: float

class Item(BaseModel):
    price_list: Optional[List[SubItem]] = None


app = FastAPI()


@app.post("/items/")
async def create_item(item: Item):
    return item

Description

Run the sample program and call the /items/ api with this body:

{
  "price_list": [
    {
      "price": "zero"
    }
  ]
}

You will get a 422 like this

{
  "detail": [
    {
      "loc": [
        "body",
        "price_list",
        0,
        "price"
      ],
      "msg": "value is not a valid float",
      "type": "type_error.float"
    }
  ]
}

But this 422 response does not follow the validation_error_definition schema :

validation_error_definition = {
    "title": "ValidationError",
    "type": "object",
    "properties": {
        "loc": {"title": "Location", "type": "array", "items": {"type": "string"}},
        "msg": {"title": "Message", "type": "string"},
        "type": {"title": "Error Type", "type": "string"},
    },
    "required": ["loc", "msg", "type"],
}

So the 422 response from fastapi does not respect the schema of the response...

The issue comes from the error in the array, pydantic returns 0 as index of the error, but it is not a string, it is an integer. And it makes sense to return an integer, otherwise we wouldn't know if it is a key in a dict or an index.

As a developer I can workaround this issue by redefining validation_error_definition in my own code

from fastapi.openapi.utils import validation_error_definition

validation_error_definition["properties"] = {
    "loc": {
        "title": "Location", "type": "array", "items": {
            "oneOf": [
                {"type": "string"},
                {"type": "integer"}
            ]
        }
    },
    "msg": {"title": "Message", "type": "string"},
    "type": {"title": "Error Type", "type": "string"},
}

But it would be better to do the change directly in the code. What is your opinion ?

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.68.1

Python Version

3.7.9

Additional Context

No response

@silversurfer34 silversurfer34 added the question Question or problem label Aug 30, 2021
@dconatha
Copy link
Contributor

I just stumbled across this error by using https://schemathesis.readthedocs.io/en/stable/ and I agree this should be fixed.

The schema should match the schema of Pydantic's "loc" found here:

https://github.com/samuelcolvin/pydantic/blob/5ccbdcb5904f35834300b01432a665c75dc02296/pydantic/error_wrappers.py#L12

@dconathan
Copy link
Contributor

I'm working on a PR for this but it breaks lots of tests because the ValidationError schema is hardcoded in lots of tests...

@AbhijithGanesh
Copy link

It passes the tests, so shouldn't this issue be closed?

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Sep 3, 2021

No. Issues are closed when problems are solved on the last release of the package.

@AbhijithGanesh
Copy link

Thanks for the clarification

@tiangolo tiangolo added bug Something isn't working and removed question Question or problem labels Apr 17, 2022
@tiangolo
Copy link
Owner

Thanks for the report @silversurfer34! 🤓

This was solved on #3810 by @dconatha 🙇 🍰

It will be available in FastAPI 0.75.2 released in a couple of hours. 🚀

@github-actions
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered bug Something isn't working reviewed
Projects
None yet
Development

No branches or pull requests

6 participants