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.

validation_error_definition schema is incorrect when detecting an error in an array

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.

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

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
dconathacommented, Aug 30, 2021

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

1reaction
AbhijithGaneshcommented, Sep 4, 2021

Thanks for the clarification

Read more comments on GitHub >

github_iconTop Results From Across the Web

Json schema validation error - Stack Overflow
Therefore you receive an error about wrong type. JSON schema type MUST be single value or array of such strings: "array","boolean","integer" ...
Read more >
[ERROR_HANDLING] different response schema between ...
The JSON response schema differs between queries having a field validation error and queries having resolver errors What did you expect?
Read more >
Handling operation errors - Apollo GraphQL Docs
Validation errors (e.g., a query included a schema field that doesn't exist); Resolver errors (e.g., an error occurred while attempting to populate a...
Read more >
Understanding schema errors | HESA
Where the error message refers to an invalid child element it means that the fields within an entity in the file are not...
Read more >
Fix Swagger Validator errors in Power Platform connectors
Arrays must have unique values, but your swagger contains duplicates. ... It's only allowed to be defined in the schema.
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