diff --git a/flask_openapi3/models/schema.py b/flask_openapi3/models/schema.py index c02761aa..912b0165 100644 --- a/flask_openapi3/models/schema.py +++ b/flask_openapi3/models/schema.py @@ -54,5 +54,6 @@ class Schema(BaseModel): externalDocs: ExternalDocumentation | None = None example: Any | None = None deprecated: bool | None = None + const: str | None = None model_config = {"populate_by_name": True} diff --git a/tests/test_openapi.py b/tests/test_openapi.py index 46e01d39..b84d1c5c 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -488,3 +488,25 @@ def test_schema_bigint(request): max_nr = 9223372036854775807 obj = Schema(maximum=max_nr) assert obj.model_dump()["maximum"] == max_nr + + +def test_convert_literal_with_single_value_to_const(request): + test_app = OpenAPI(request.node.name) + test_app.config["TESTING"] = True + + class MyResponse(BaseModel): + foobar: Literal["baz"] + + @test_app.post("/test", responses={200: MyResponse}) + def endpoint_test(): + print("do nothing") + + with test_app.test_client() as client: + resp = client.get("/openapi/openapi.json") + assert resp.status_code == 200 + print("###", resp.json) + assert resp.json["components"]["schemas"]["MyResponse"]["properties"]["foobar"] == { + "const": "baz", + "title": "Foobar", + "type": "string", + }