[BUG] Union of a model with a Color raises error in creating `openAPI.json`.
See original GitHub issueTo Reproduce
- Create the following app (
main.py
):
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.color import Color
class A(BaseModel):
type: str
class B(BaseModel):
bla: Color
C = Union[A, B]
app = FastAPI()
@app.get('/{var}', response_model=C)
def get(var: int):
if var == 1:
return {'type': 'a'}
else:
return {'bla': '#ff00ff'}
-
run
uvicorn main:app
-
See error (
Failed to load API definition
on the browser), mini stack trace:
File ".../venv/lib/python3.7/site-packages/pydantic/schema.py", line 772, in field_singleton_schema
raise ValueError(f'Value not declarable with JSON Schema, field: {field}')
ValueError: Value not declarable with JSON Schema, field: bla type=Color required
- http://127.0.0.1:8000/1 and http://127.0.0.1:8000/2, no error in shown.
Expected behavior No error
Environment:
- OS: [macOS]
- FastAPI Version 0.31.0.
- Python version: python 3.7.3
Additional context No additional context, plain FastAPI. I am not sure if this is coming from pydantic or fastAPI: it may be a pydantic problem, or how fastAPI is using pydantic.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Integrated Swagger UI throws error on valid OpenAPI spec file
Summary When opening a valid OpenAPI JSON file from the Repository / Files section, an error message is displayed saying:
Read more >Changelog - pydantic
Revert Breaking Change: v1.9.1 introduced a breaking change where model fields were deep copied by default, this release reverts the default behaviour to ......
Read more >pydantic — pydantic v0.28 documentation
If validation fails pydantic with raise an error with a breakdown of what was wrong: ... Pydantic allows auto creation of JSON Schemas...
Read more >Pydantic in a Nutshell - Python in Plain English
The library datamodel-code-generator allows generating pydantic code (models) from a variety of widely used data sources: OpenAPI 3 (YAML/JSON); JSON Schema ...
Read more >Converting Smithy to OpenAPI
Converting a Smithy model to OpenAPI is a lossy conversion. ... of the jsonName trait when creating JSON schema properties for structure and...
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
Not sure if you’re allowed to use
var
as it’s a keyword.Thanks @euri10 for your help here and implementing it in Pydantic! 🍰 🚀
Thanks @jorgecarleitao for reporting back and closing the issue.
Just in case, this should be available in a recent version of FastAPI.