Skip to content

Commit

Permalink
Support for HTTP Status Wildcards (#159)
Browse files Browse the repository at this point in the history
v30/glue -  HTTP Status Code Wildcards

Per section 4.7.16.2
https://spec.openapis.org/oas/v3.0.0#patterned-fields-0

"... 2XX represents all response codes between [200-299]. The following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response range is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code."
  • Loading branch information
ggpwnkthx committed Sep 14, 2023
1 parent cb72ac9 commit 7ff2ba6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ openapi.yaml
openapi.json
*.swp
*.pyc
openapi3.egg-info/*
build/*
dist/*
.idea
tests/data/
tests/my_*.py
docs/build/
.pdm-python
/*.egg-info
/*.ipynb
20 changes: 10 additions & 10 deletions aiopenapi3/v30/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ def args(self, content_type: str = "application/json") -> Dict[str, Any]:
return {"parameters": parameters, "data": schema}

def return_value(self, http_status: int = 200, content_type: str = "application/json") -> Optional["SchemaType"]:
if (a := self.operation.responses.get(str(http_status), None)) is not None:
if (b := a.content.get(content_type, None)) is not None:
status_key = str(http_status)
if a := self.operation.responses.get(status_key) or self.operation.responses.get(status_key[0] + "XX"):
if b := a.content.get(content_type):
return b.schema_
return None

Expand Down Expand Up @@ -424,19 +425,18 @@ def _prepare(self, data: Optional["RequestData"], parameters: Optional["RequestP
self._prepare_body(data, rbq)

def _process__status_code(self, result: httpx.Response, status_code: str) -> "v3xResponseType":
# find the response model in spec we received
expected_response = None
if status_code in self.operation.responses:
expected_response = self.operation.responses[status_code]
elif "default" in self.operation.responses:
expected_response = self.operation.responses["default"]

expected_response = (
self.operation.responses.get(status_code)
or self.operation.responses.get(status_code[0] + "XX")
or self.operation.responses.get("default")
)
if expected_response is None:
options = ",".join(self.operation.responses.keys())
raise HTTPStatusError(
self.operation,
result.status_code,
f"""Unexpected response {result.status_code} from {self.operation.operationId} (expected one of {options}), no default is defined""",
f"Unexpected response {result.status_code} from {self.operation.operationId} "
f"(expected one of {options}), no default is defined",
result,
)
return expected_response
Expand Down

0 comments on commit 7ff2ba6

Please sign in to comment.