Skip to content

Commit 160ab49

Browse files
luolingchunluolingchun
and
luolingchun
authored
ServerVariable.enum should be optional (#194)
Co-authored-by: luolingchun <[email protected]>
1 parent 9b94105 commit 160ab49

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

flask_openapi3/models/server_variable.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
# @Time : 2023/7/4 9:57
44
from typing import List, Optional
55

6-
from pydantic import BaseModel
6+
from pydantic import BaseModel, Field
77

88

99
class ServerVariable(BaseModel):
1010
"""
1111
https://spec.openapis.org/oas/v3.1.0#server-variable-object
1212
"""
1313

14-
enum: List[str]
14+
enum: Optional[List[str]] = Field(None, min_length=1)
1515
default: str
1616
description: Optional[str] = None
1717

tests/test_server.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
# @Author : llc
3+
# @Time : 2024/11/10 12:17
4+
from pydantic import ValidationError
5+
6+
from flask_openapi3 import Server, ServerVariable
7+
8+
9+
def test_server_variable():
10+
Server(
11+
url="http://127.0.0.1:5000",
12+
variables=None
13+
)
14+
try:
15+
variables = {"one": ServerVariable(default="one", enum=[])}
16+
Server(
17+
url="http://127.0.0.1:5000",
18+
variables=variables
19+
)
20+
error = 0
21+
except ValidationError:
22+
error = 1
23+
assert error == 1
24+
try:
25+
variables = {"one": ServerVariable(default="one")}
26+
Server(
27+
url="http://127.0.0.1:5000",
28+
variables=variables
29+
)
30+
error = 0
31+
except ValidationError:
32+
error = 1
33+
assert error == 0
34+
try:
35+
variables = {"one": ServerVariable(default="one", enum=["one", "two"])}
36+
Server(
37+
url="http://127.0.0.1:5000",
38+
variables=variables
39+
)
40+
error = 0
41+
except ValidationError:
42+
error = 1
43+
assert error == 0

0 commit comments

Comments
 (0)