Skip to content

Commit 994e7b7

Browse files
committed
[#42] Fix required header is not found when _ in header field
1 parent c62c353 commit 994e7b7

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

flask_openapi3/do_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
def _do_header(header, request_kwargs):
1515
request_headers = dict(request.headers) or {}
1616
for key, value in header.__annotations__.items():
17-
key_title = key.title()
17+
key_title = key.replace("_", "-").title()
1818
# add original key
1919
if key_title in request_headers.keys():
2020
request_headers[key] = request_headers[key_title]

tests/test_request.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ class BookCookie(BaseModel):
4040

4141

4242
class BookHeader(BaseModel):
43-
Hello1: str = Field("what's up", max_length=12, description='sds')
43+
Hello1: str = Field("what's up", max_length=12, description="sds")
4444
# required
45-
hello2: str = Field(..., max_length=12, description='sds')
45+
hello2: str = Field(..., max_length=12, description="sds")
46+
api_key: str = Field(..., description="API Key")
4647

4748

4849
def decorator(func):
@@ -72,7 +73,7 @@ def api_error_json(body: BookBody):
7273
return {"code": 0, "message": "ok"}
7374

7475

75-
@app.get('/header')
76+
@app.get("/header")
7677
def get_book(header: BookHeader):
7778
return header.dict()
7879

@@ -97,7 +98,7 @@ def test_form(client):
9798
"string": "a",
9899
"string_list": ["a", "b", "c"]
99100
}
100-
r = client.post("/form", data=data, content_type='multipart/form-data')
101+
r = client.post("/form", data=data, content_type="multipart/form-data")
101102
assert r.status_code == 200
102103

103104

@@ -113,7 +114,7 @@ def test_cookie(client):
113114

114115

115116
def test_header(client):
116-
headers = {'Hello1': '111', 'hello2': '222'}
117+
headers = {"Hello1": "111", "hello2": "222", "api_key": "333"}
117118
resp = client.get("/header", headers=headers)
118119
print(resp.json)
119120
assert resp.status_code == 200

0 commit comments

Comments
 (0)