Skip to content

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

validation errors for openAPI schema with length >=2 tuples #3898

Closed
9 tasks done
dconathan opened this issue Sep 13, 2021 · 7 comments
Closed
9 tasks done

validation errors for openAPI schema with length >=2 tuples #3898

dconathan opened this issue Sep 13, 2021 · 7 comments

Comments

@dconathan
Copy link
Contributor

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 Tuple
from fastapi import FastAPI
from pydantic import BaseModel


class FooArgs(BaseModel):
    bars: Tuple[str, str]


app = FastAPI()


@app.post("/foo")
def foo(args: FooArgs):
    ...

Description

If you try to retrieve the /openapi.json, the FooArgs get a validation error:

  File ".../lib/python3.9/site-packages/fastapi/openapi/utils.py", line 410, in get_openapi
    return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True)  # type: ignore
  File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for OpenAPI
components -> schemas -> FooArgs -> properties -> bars -> items
  value is not a valid dict (type=type_error.dict)
components -> schemas -> FooArgs -> $ref
  field required (type=value_error.missing)

This error is new to fastapi>=0.68, it does not happen with fastapi==0.67.0.

I think this is related to Pydantic coercing Tuple[str, str] into Dict[str, str] somewhere?

Things that don't cause this error:

class FooArgs(BaseModel):
    bars: Tuple[str]
class FooArgs(BaseModel):
    bars: Tuple[str, ...]

This that do cause this error:

class FooArgs(BaseModel):
    bars: Tuple[str, str]
class FooArgs(BaseModel):
    bars: Tuple[int, str]
class FooArgs(BaseModel):
    bars: Tuple[str, str, str]  # etc.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.68.1

Python Version

3.9.1 (also seeing this in 3.8.*)

Additional Context

Seems pretty likely that this commit introduced it, but there's a lot going on so I'm not sure where:

97fa743#diff-4939896f79697822446463d5d6dfbf7a0ea5805c46565d355c7305d86f3405c7

@dconathan dconathan added the question Question or problem label Sep 13, 2021
@dconathan dconathan changed the title validation errors for openAPI schema with length >2 tuples validation errors for openAPI schema with length >=2 tuples Sep 13, 2021
@trevorwang
Copy link

Tested your code on fastapi 0.68.1 with python 3.8. works pretty well.

class FooArgs(BaseModel):
    bars: Tuple[str, str]


@app.post("/foo")
def foo(args: FooArgs):
    pass

Screen Shot 2021-09-16 at 1 55 20 PM

@dconathan
Copy link
Contributor Author

Where is that screenshot from? Are you able to curl http://127.0.0.1:8000/openapi.json ?

$ cat app.py
from typing import List, Tuple
from fastapi import FastAPI
from pydantic import BaseModel


class FooArgs(BaseModel):
    bars: Tuple[str, str]

app = FastAPI()


@app.post("/foo")
def foo(args: FooArgs):
    ...

$ uvicorn app:app
INFO:     Started server process [72531]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:49438 - "GET /openapi.json HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application

... (same error as above)
$ curl http://localhost:8000/openapi.json
Internal Server Error

If it matters, I'm on:

$ pip freeze | grep pydantic
pydantic==1.8.2

@trevorwang
Copy link

trevorwang commented Sep 17, 2021

it's http://localhost:8000/docs

It is not working now. I got the same error as yours. Sad.

@fakegermano
Copy link

fakegermano commented Sep 17, 2021

Can confirm had the same errors here, docs work on previous versions, but update seems to break something.

After a bit of search, I found an issue in pydantic related to this:

pydantic/pydantic#3210

@fakegermano
Copy link

fakegermano commented Sep 17, 2021

after another bit of search, I think this issue is a duplicate of both #3665 and #3782, there is also some more info there!

@tiangolo
Copy link
Member

It seems this is a duplicate of #3665

In short, it should be fixed by FastAPI 0.73.0, released in the next hours. 🎉

For more info, read the last comment in that issue: #3665 (comment)

And the comment in the PR: #3874 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2022

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.

@github-actions github-actions bot closed this as completed Feb 3, 2022
@tiangolo tiangolo reopened this Feb 27, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6583 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants