Initial Checks
Description
If we have an object, and a primite type, we are still creating an anyOf. That unfortunately doesn't work well with neither ReDoc nor Swagger.
From the code below, the following OpenAPI spec is generated:
Details
{
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"title": "Detail",
"type": "array"
}
},
"title": "HTTPValidationError",
"type": "object"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"type": ["string", "integer"]
},
"title": "Location",
"type": "array"
},
"msg": {
"title": "Message",
"type": "string"
},
"type": {
"title": "Error Type",
"type": "string"
}
},
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError",
"type": "object"
}
}
},
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"openapi": "3.1.0",
"paths": {
"/": {
"get": {
"operationId": "root__get",
"parameters": [
{
"description": "Date to query",
"in": "query",
"name": "at",
"required": false,
"schema": {
"anyOf": [
{
"enum": [
"today",
"tomorrow",
"yesterday"
],
"type": "string"
},
{
"type": "null"
}
],
"description": "Date to query",
"title": "At"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {}
}
},
"description": "Successful Response"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
"description": "Validation Error"
}
},
"summary": "Root"
}
}
}
}
Ideally, we'd have the following schema:
Details
{
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"title": "Detail",
"type": "array"
}
},
"title": "HTTPValidationError",
"type": "object"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"title": "Location",
"type": "array"
},
"msg": {
"title": "Message",
"type": "string"
},
"type": {
"title": "Error Type",
"type": "string"
}
},
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError",
"type": "object"
}
}
},
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"openapi": "3.1.0",
"paths": {
"/": {
"get": {
"operationId": "root__get",
"parameters": [
{
"description": "Date to query",
"in": "query",
"name": "at",
"required": false,
"schema": {
"enum": [
"today",
"tomorrow",
"yesterday"
],
"type": ["string", "null"]
"description": "Date to query",
"title": "At"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {}
}
},
"description": "Successful Response"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
"description": "Validation Error"
}
},
"summary": "Root"
}
}
}
}
As you can see above, the type becomes an array of primitive types. This is mentioned in the Assertions and Instance Primitive Types on the JSON Schema Draft 2020-12.
Example Code
from typing import Literal, Annotated
from fastapi import Query, FastAPI
app = FastAPI()
@app.get("/")
def root(
at: Annotated[
Literal["today", "tomorrow", "yesterday"] | None,
Query(
description="Date to query",
),
] = None
):
return at
Python, Pydantic & OS Version
pydantic version: 2.0.2
pydantic-core version: 2.2.0 release build profile
install path: /Users/marcelotryle/dev/pydantic/pydantic/pydantic
python version: 3.11.1 (main, Apr 20 2023, 11:08:52) [Clang 14.0.3 (clang-1403.0.22.14.1)]
platform: macOS-13.4-arm64-arm-64bit
optional deps. installed: ['devtools', 'email-validator', 'typing-extensions']
Ref.:
Selected Assignee: @dmontagu
Initial Checks
Description
If we have an object, and a primite type, we are still creating an
anyOf. That unfortunately doesn't work well with neither ReDoc nor Swagger.From the code below, the following OpenAPI spec is generated:
Details
{ "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "title": "Detail", "type": "array" } }, "title": "HTTPValidationError", "type": "object" }, "ValidationError": { "properties": { "loc": { "items": { "type": ["string", "integer"] }, "title": "Location", "type": "array" }, "msg": { "title": "Message", "type": "string" }, "type": { "title": "Error Type", "type": "string" } }, "required": [ "loc", "msg", "type" ], "title": "ValidationError", "type": "object" } } }, "info": { "title": "FastAPI", "version": "0.1.0" }, "openapi": "3.1.0", "paths": { "/": { "get": { "operationId": "root__get", "parameters": [ { "description": "Date to query", "in": "query", "name": "at", "required": false, "schema": { "anyOf": [ { "enum": [ "today", "tomorrow", "yesterday" ], "type": "string" }, { "type": "null" } ], "description": "Date to query", "title": "At" } } ], "responses": { "200": { "content": { "application/json": { "schema": {} } }, "description": "Successful Response" }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, "description": "Validation Error" } }, "summary": "Root" } } } }Ideally, we'd have the following schema:
Details
{ "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "title": "Detail", "type": "array" } }, "title": "HTTPValidationError", "type": "object" }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [ { "type": "string" }, { "type": "integer" } ] }, "title": "Location", "type": "array" }, "msg": { "title": "Message", "type": "string" }, "type": { "title": "Error Type", "type": "string" } }, "required": [ "loc", "msg", "type" ], "title": "ValidationError", "type": "object" } } }, "info": { "title": "FastAPI", "version": "0.1.0" }, "openapi": "3.1.0", "paths": { "/": { "get": { "operationId": "root__get", "parameters": [ { "description": "Date to query", "in": "query", "name": "at", "required": false, "schema": { "enum": [ "today", "tomorrow", "yesterday" ], "type": ["string", "null"] "description": "Date to query", "title": "At" } } ], "responses": { "200": { "content": { "application/json": { "schema": {} } }, "description": "Successful Response" }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, "description": "Validation Error" } }, "summary": "Root" } } } }As you can see above, the
typebecomes an array of primitive types. This is mentioned in the Assertions and Instance Primitive Types on the JSON Schema Draft 2020-12.Example Code
Python, Pydantic & OS Version
Ref.:
Selected Assignee: @dmontagu