Skip to content

Multi-value Literal isn't compliant with OpenAPI #1350

@nphilou

Description

@nphilou

Multi-value Literal returns const in the JSON sub-schema of an anyOf, which isn't supported by OpenAPI:

anyOf – the subschemas must be OpenAPI schemas and not standard JSON Schemas.

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.4
            pydantic compiled: False
                 install path: /Users/[...]/venv/lib/python3.7/site-packages/pydantic
               python version: 3.7.4 (default, Sep  7 2019, 18:27:02)  [Clang 10.0.1 (clang-1001.0.46.4)]
                     platform: Darwin-18.7.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

Code to reproduce

from pydantic import BaseModel
from typing_extensions import Literal

class FooBar(BaseModel):
    values: Literal["value_1", "value_2"]

print(FooBar.schema_json(indent=2))

output:

{
  "title": "FooBar",
  "type": "object",
  "properties": {
    "values": {
      "title": "Values",
      "anyOf": [
        {
          "const": "value_1",
          "type": "string"
        },
        {
          "const": "value_2",
          "type": "string"
        }
      ]
    }
  },
  "required": [
    "values"
  ]
}

Literal with only one value is OpenAPI compliant btw

This issue is linked to: fastapi/fastapi#562, #649 (the simple enum would work well)

Metadata

Metadata

Assignees

No one assigned

    Labels

    changeSuggested alteration to Pydantic, not a new feature nor a bug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions