Skip to content

Commit 010f7af

Browse files
authored
Add PrefixItems to Schema Model for use with Tuple types (#197)
1 parent 7883df4 commit 010f7af

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

flask_openapi3/models/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Schema(BaseModel):
3939
anyOf: Optional[List[Union[Reference, "Schema"]]] = None
4040
schema_not: Optional[Union[Reference, "Schema"]] = Field(default=None, alias="not")
4141
items: Optional[Union[Reference, "Schema"]] = None
42+
prefixItems: Optional[List[Union[Reference, "Schema"]]] = None
4243
properties: Optional[Dict[str, Union[Reference, "Schema"]]] = None
4344
additionalProperties: Optional[Union[bool, Reference, "Schema"]] = None
4445
description: Optional[str] = None

tests/test_openapi.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Generic, TypeVar, List, Optional
3+
from typing import Generic, TypeVar, List, Optional, Tuple, Literal
44

55
from pydantic import BaseModel, Field
66

@@ -543,3 +543,31 @@ def endpoint_test():
543543
data = test_app.api_doc["paths"]["/test"]["post"]
544544

545545
assert "deprecated" not in data
546+
547+
548+
class TupleModel(BaseModel):
549+
my_tuple: Tuple[Literal["a", "b"], Literal["c", "d"]]
550+
551+
552+
def test_prefix_items(request):
553+
test_app = OpenAPI(request.node.name)
554+
test_app.config["TESTING"] = True
555+
556+
@test_app.post("/test")
557+
def endpoint_test(body: TupleModel):
558+
print([]) # pragma: no cover
559+
560+
schema = test_app.api_doc["paths"]["/test"]["post"]["requestBody"]["content"]["application/json"]["schema"]
561+
assert schema == {'$ref': '#/components/schemas/TupleModel'}
562+
components = test_app.api_doc["components"]["schemas"]
563+
assert components["TupleModel"] == {'properties': {'my_tuple': {'maxItems': 2,
564+
'minItems': 2,
565+
'prefixItems': [{'enum': ['a', 'b'],
566+
'type': 'string'},
567+
{'enum': ['c', 'd'],
568+
'type': 'string'}],
569+
'title': 'My Tuple',
570+
'type': 'array'}},
571+
'required': ['my_tuple'],
572+
'title': 'TupleModel',
573+
'type': 'object'}

0 commit comments

Comments
 (0)