Skip to content

Commit 0c6539a

Browse files
githubjakobJakob Zankerluolingchun
authored
WIP Add support for const (#243)
* Add support for const * fix ruff --------- Co-authored-by: Jakob Zanker <jakob@project-lary.com> Co-authored-by: luolingchun <luolingchun@outlook.com>
1 parent 35c2fac commit 0c6539a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

flask_openapi3/models/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ class Schema(BaseModel):
5454
externalDocs: ExternalDocumentation | None = None
5555
example: Any | None = None
5656
deprecated: bool | None = None
57+
const: str | None = None
5758

5859
model_config = {"populate_by_name": True}

tests/test_openapi.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,25 @@ def test_schema_bigint(request):
488488
max_nr = 9223372036854775807
489489
obj = Schema(maximum=max_nr)
490490
assert obj.model_dump()["maximum"] == max_nr
491+
492+
493+
def test_convert_literal_with_single_value_to_const(request):
494+
test_app = OpenAPI(request.node.name)
495+
test_app.config["TESTING"] = True
496+
497+
class MyResponse(BaseModel):
498+
foobar: Literal["baz"]
499+
500+
@test_app.post("/test", responses={200: MyResponse})
501+
def endpoint_test():
502+
print("do nothing")
503+
504+
with test_app.test_client() as client:
505+
resp = client.get("/openapi/openapi.json")
506+
assert resp.status_code == 200
507+
print("###", resp.json)
508+
assert resp.json["components"]["schemas"]["MyResponse"]["properties"]["foobar"] == {
509+
"const": "baz",
510+
"title": "Foobar",
511+
"type": "string",
512+
}

0 commit comments

Comments
 (0)