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.

[BUG] Union of a model with a Color raises error in creating `openAPI.json`.

See original GitHub issue

To Reproduce

  1. 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'}
  1. run uvicorn main:app

  2. open http://127.0.0.1:8000/docs

  3. 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
  1. 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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
varunchopracommented, Jun 30, 2019

Not sure if you’re allowed to use var as it’s a keyword.

0reactions
tiangolocommented, Aug 31, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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