Skip to content

Commit

Permalink
v20 - glue
Browse files Browse the repository at this point in the history
empty responses are allowed
  • Loading branch information
commonism committed Nov 29, 2024
1 parent 314ec3d commit ef1f03c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
6 changes: 5 additions & 1 deletion aiopenapi3/v20/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def _process_stream(self, result: httpx.Response) -> tuple["ResponseHeadersType"
return headers, expected_response.schema_

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

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

if expected_response.schema_ is None:
"""Swagger treats no schema as a response without a body."""
return rheaders, None

if status_code == "204":
return rheaders, None

Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ def with_paths_response_header(openapi_version):
yield _get_parsed_yaml("paths-response-header.yaml", openapi_version)


@pytest.fixture(params=["", "-v20"], ids=["v3x", "v20"])
def with_paths_response_content_empty_vXX(request):
return _get_parsed_yaml(f"paths-response-content-empty{request.param}.yaml")


@pytest.fixture
def with_paths_response_content_type_octet(openapi_version):
yield _get_parsed_yaml("paths-response-content-type-octet.yaml", openapi_version)
Expand Down
33 changes: 33 additions & 0 deletions tests/fixtures/paths-response-content-empty-v20.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
swagger: "2.0"
info:
title: with empty response
description: with empty response
version: 1.0.0
host: api.example.com
basePath: /v1
schemes:
- https

consumes:
- application/json
produces:
- application/json

definitions: {}

paths:
/empty:
get:
operationId: empty
responses:
"200":
description: OK
/headers:
get:
operationId: headers
responses:
"200":
description: OK
headers:
X-required:
type: string
28 changes: 28 additions & 0 deletions tests/fixtures/paths-response-content-empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.3
info:
title: 'with empty response'
version: 0.0.0
servers:
- url: http://127.0.0.1/api

security:
- {}

paths:
/empty:
get:
operationId: empty
responses:
"200":
description: "ok"
/headers:
get:
operationId: headers
responses:
"200":
description: "ok"
headers:
X-required:
schema:
type: string
required: true
12 changes: 12 additions & 0 deletions tests/path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ def test_paths_response_header(httpx_mock, with_paths_response_header):
return


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_response_content_empty(httpx_mock, with_paths_response_content_empty_vXX):
httpx_mock.add_response(status_code=200)
api = OpenAPI(URLBASE, with_paths_response_content_empty_vXX, session_factory=httpx.Client)
h, b = api._.empty(return_headers=True)
assert b is None and h == {}

httpx_mock.add_response(status_code=200, headers={"X-required": "1"})
h, b = api._.headers(return_headers=True)
assert b is None and h["X-required"] == "1"


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_response_content_type_octet(httpx_mock, with_paths_response_content_type_octet):
CONTENT = b"\x00\x11"
Expand Down

0 comments on commit ef1f03c

Please sign in to comment.