I have two problems with how Enum declared in OAS.
When I create 3 different models with same enum, i find, that in always paste in schema As Is. There is not schema declared in components->schemas section of OAS.
It cause a problem with frontend, when they create bindings with code-gen tools. They need to handle this strings directly.
I think it is better to register Enum as objects in schemas and refer to them.
When you try to make an Enum field optional, instead of getting oneOf{null, schema{enum{...}} you get enum{..., null}, which is incorrect, i think. Because i'm not realy declared null as value of this enum.
This problem caused by the same logic as above.
I have code to fix it (we use it as monkey-patch in our project), so if we confirm that this is a problem, i'll provide PR.
No response
from enum import StrEnum
from litestar import Litestar, get
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import JsonRenderPlugin
from pydantic import BaseModel, Field
class TestEnum(StrEnum):
""" "Description!"""
ONE = "one"
TWO = "two"
class TestSchemaA(BaseModel):
enum_field: TestEnum | None = Field(default=None)
other_filed: int | None = Field(default=None)
class TestSchemaB(BaseModel):
enum_field: TestEnum
other_field: int
@get("/a")
async def test_routerA() -> TestSchemaA:
return TestSchemaA(enum_field=TestEnum.ONE)
@get("/b")
async def test_routerB() -> TestSchemaB:
return TestSchemaB(enum_field=TestEnum.TWO)
app = Litestar(
route_handlers=[test_routerA, test_routerB],
openapi_config=OpenAPIConfig(
path="/docs",
title="Test Api",
version="0.1.0",
render_plugins=[JsonRenderPlugin()],
),
)
{
"info": {
"title": "Test Api",
"version": "0.1.0"
},
"openapi": "3.1.0",
"servers": [
{
"url": "/"
}
],
"paths": {
"/a": {
"get": {
"summary": "TestRoutera",
"operationId": "ATestRoutera",
"responses": {
"200": {
"description": "Request fulfilled, document follows",
"headers": {
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestSchemaA"
}
}
}
}
},
"deprecated": false
}
},
"/b": {
"get": {
"summary": "TestRouterb",
"operationId": "BTestRouterb",
"responses": {
"200": {
"description": "Request fulfilled, document follows",
"headers": {
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestSchemaB"
}
}
}
}
},
"deprecated": false
}
}
},
"components": {
"schemas": {
"TestSchemaA": {
"properties": {
"enum_field": {
"type": [
"null",
"string"
],
"enum": [
"one",
"two",
null]
},
"other_filed": {
"oneOf": [
{
"type": "null"
},
{
"type": "integer"
}
]
}
},
"type": "object",
"required": [],
"title": "TestSchemaA"
},
"TestSchemaB": {
"properties": {
"enum_field": {
"type": "string",
"enum": [
"one",
"two"
]
},
"other_field": {
"type": "integer"
}
},
"type": "object",
"required": [
"enum_field",
"other_field"
],
"title": "TestSchemaB"
}
}
}
}
2.8.2
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too