Skip to content

Commit 2f06443

Browse files
authored
v20 - glue (#302)
empty responses are allowed
1 parent 314ec3d commit 2f06443

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

aiopenapi3/v20/glue.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def _process_stream(self, result: httpx.Response) -> tuple["ResponseHeadersType"
295295
return headers, expected_response.schema_
296296

297297
def _process_request(self, result: httpx.Response) -> tuple["ResponseHeadersType", "ResponseDataType"]:
298-
rheaders: "ResponseHeadersType" = dict()
298+
rheaders: "ResponseHeadersType"
299299
# spec enforces these are strings
300300
status_code = str(result.status_code)
301301
content_type = result.headers.get("Content-Type", None)
@@ -316,6 +316,10 @@ def _process_request(self, result: httpx.Response) -> tuple["ResponseHeadersType
316316

317317
rheaders = self._process__headers(result, headers, expected_response)
318318

319+
if expected_response.schema_ is None:
320+
"""Swagger treats no schema as a response without a body."""
321+
return rheaders, None
322+
319323
if status_code == "204":
320324
return rheaders, None
321325

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ def with_paths_response_header(openapi_version):
272272
yield _get_parsed_yaml("paths-response-header.yaml", openapi_version)
273273

274274

275+
@pytest.fixture(params=["", "-v20"], ids=["v3x", "v20"])
276+
def with_paths_response_content_empty_vXX(request):
277+
return _get_parsed_yaml(f"paths-response-content-empty{request.param}.yaml")
278+
279+
275280
@pytest.fixture
276281
def with_paths_response_content_type_octet(openapi_version):
277282
yield _get_parsed_yaml("paths-response-content-type-octet.yaml", openapi_version)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
swagger: "2.0"
2+
info:
3+
title: with empty response
4+
description: with empty response
5+
version: 1.0.0
6+
host: api.example.com
7+
basePath: /v1
8+
schemes:
9+
- https
10+
11+
consumes:
12+
- application/json
13+
produces:
14+
- application/json
15+
16+
definitions: {}
17+
18+
paths:
19+
/empty:
20+
get:
21+
operationId: empty
22+
responses:
23+
"200":
24+
description: OK
25+
/headers:
26+
get:
27+
operationId: headers
28+
responses:
29+
"200":
30+
description: OK
31+
headers:
32+
X-required:
33+
type: string
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.3
2+
info:
3+
title: 'with empty response'
4+
version: 0.0.0
5+
servers:
6+
- url: http://127.0.0.1/api
7+
8+
security:
9+
- {}
10+
11+
paths:
12+
/empty:
13+
get:
14+
operationId: empty
15+
responses:
16+
"200":
17+
description: "ok"
18+
/headers:
19+
get:
20+
operationId: headers
21+
responses:
22+
"200":
23+
description: "ok"
24+
headers:
25+
X-required:
26+
schema:
27+
type: string
28+
required: true

tests/path_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,18 @@ def test_paths_response_header(httpx_mock, with_paths_response_header):
427427
return
428428

429429

430+
@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
431+
def test_paths_response_content_empty(httpx_mock, with_paths_response_content_empty_vXX):
432+
httpx_mock.add_response(status_code=200)
433+
api = OpenAPI(URLBASE, with_paths_response_content_empty_vXX, session_factory=httpx.Client)
434+
h, b = api._.empty(return_headers=True)
435+
assert b is None and h == {}
436+
437+
httpx_mock.add_response(status_code=200, headers={"X-required": "1"})
438+
h, b = api._.headers(return_headers=True)
439+
assert b is None and h["X-required"] == "1"
440+
441+
430442
@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
431443
def test_paths_response_content_type_octet(httpx_mock, with_paths_response_content_type_octet):
432444
CONTENT = b"\x00\x11"

0 commit comments

Comments
 (0)