From 37306bb12369b0810005c9c206ed66eee3080a7e Mon Sep 17 00:00:00 2001 From: Jakob Zanker Date: Thu, 16 Oct 2025 15:21:20 +0200 Subject: [PATCH 1/2] Add support for const --- flask_openapi3/models/schema.py | 1 + tests/test_openapi.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) 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..4d8bd830 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -488,3 +488,24 @@ 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"} \ No newline at end of file From b1db5d9d97c749afa5c12d2afcb68b339b5402be Mon Sep 17 00:00:00 2001 From: luolingchun Date: Sat, 18 Oct 2025 13:57:39 +0800 Subject: [PATCH 2/2] fix ruff --- tests/test_openapi.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_openapi.py b/tests/test_openapi.py index 4d8bd830..b84d1c5c 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -497,10 +497,7 @@ def test_convert_literal_with_single_value_to_const(request): class MyResponse(BaseModel): foobar: Literal["baz"] - - @test_app.post("/test", responses={ - 200: MyResponse - }) + @test_app.post("/test", responses={200: MyResponse}) def endpoint_test(): print("do nothing") @@ -508,4 +505,8 @@ def endpoint_test(): 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"} \ No newline at end of file + assert resp.json["components"]["schemas"]["MyResponse"]["properties"]["foobar"] == { + "const": "baz", + "title": "Foobar", + "type": "string", + }