Unexpected ValidationError with accessing /openapi.json when some model uses typing.Tuple
See original GitHub issueFirst 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 pydantic import BaseModel
from fastapi import FastAPI
from typing import Tuple
class FooModel(BaseModel):
prop: Tuple[int, str]
app = FastAPI()
@app.get('/', response_model=FooModel)
def get_foo():
return {"prop": (0, 'ok')}
print(app.openapi())
Description
Summary
The above code must be failed with a ValidationError like this:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
a()
File "<pyshell#1>", line 17, in a
print(app.openapi())
File "/usr/local/lib/python39/lib/site-packages/fastapi/applications.py", line 136, in openapi
self.openapi_schema = get_openapi(
File "/usr/local/lib/python39/lib/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 -> FooModel -> properties -> prop -> items
value is not a valid dict (type=type_error.dict)
components -> schemas -> FooModel -> $ref
field required (type=value_error.missing)
in fastapi.openapi.models in line 103~142
class Schema(BaseModel):
ref: Optional[str] = Field(None, alias="$ref")
title: Optional[str] = None
multipleOf: Optional[float] = None
maximum: Optional[float] = None
exclusiveMaximum: Optional[float] = None
minimum: Optional[float] = None
exclusiveMinimum: Optional[float] = None
maxLength: Optional[int] = Field(None, gte=0)
minLength: Optional[int] = Field(None, gte=0)
pattern: Optional[str] = None
maxItems: Optional[int] = Field(None, gte=0)
minItems: Optional[int] = Field(None, gte=0)
uniqueItems: Optional[bool] = None
maxProperties: Optional[int] = Field(None, gte=0)
minProperties: Optional[int] = Field(None, gte=0)
required: Optional[List[str]] = None
enum: Optional[List[Any]] = None
type: Optional[str] = None
allOf: Optional[List["Schema"]] = None
oneOf: Optional[List["Schema"]] = None
anyOf: Optional[List["Schema"]] = None
not_: Optional["Schema"] = Field(None, alias="not")
items: Optional["Schema"] = None
properties: Optional[Dict[str, "Schema"]] = None
additionalProperties: Optional[Union["Schema", Reference, bool]] = None
description: Optional[str] = None
format: Optional[str] = None
default: Optional[Any] = None
nullable: Optional[bool] = None
discriminator: Optional[Discriminator] = None
readOnly: Optional[bool] = None
writeOnly: Optional[bool] = None
xml: Optional[XML] = None
externalDocs: Optional[ExternalDocumentation] = None
example: Optional[Any] = None
deprecated: Optional[bool] = None
class Config:
extra: str = "allow"
It seems Schema.items
should accept an array of schemas according to the JSON schema spec, but only the type Schema
is allowed in the current code.
Operating System
Linux, Windows
Operating System Details
No response
FastAPI Version
0.70.0
Python Version
Python 3.9.5
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
draft 2020-12 - Ajv JSON schema validator
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Read more >Migrating from OpenAPI 3.0 to 3.1.0
In OpenAPI v3.0, describing file uploads was signalled with a type: string and the format set to byte, binary, or base64. JSON Schema...
Read more >latest PDF - Flask-RESTX Documentation
While Flask provides easy access to request data (i.e. querystring or POST form encoded data), it's still a pain to validate form data....
Read more >Changelog - pydantic
Json [list[str]] will be now inferred as list[str] , Json[Any] should be used instead ... Use parent model's Config when validating nested NamedTuple...
Read more >Common Problems Found in RAML 1.0 API Specifications
'schema' keyword it's deprecated for 1.0 version, should use 'type' instead ... Not correctly using curly braces and brackets in JSON examples.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1! Also, duplicate of #3665, #3686, #3782, and #3898 (the list is growing!)
nitp - Your sample code has a small issue that might confuse people, so you probably want to fix that for posteriority:
The return value of your sample route is an array, but, according to the model declaration, it’s supposed to be a dict containing a
prop
key which has a tuple of (int, str) as value: