Skip to content

Commit

Permalink
Exclude unset
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanarijit committed Jan 5, 2024
1 parent da0e68a commit f63e45c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion apphelpers/rest/hug.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ def build(self, method, method_args, method_kw, f):
validator = TypeAdapter(response_model)

def callable_return_type(ret):
return validator.dump_python(validator.validate_python(ret))
return validator.dump_python(
validator.validate_python(ret), exclude_unset=True
)

f.__annotations__["return"] = callable_return_type

Expand Down
1 change: 1 addition & 0 deletions tests/app/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def process_request(request, body):
class RawResp(BaseModel):
raw_body: str
headers: Dict[str, str]
unset_field: Optional[str] = None


@ep.response_model(RawResp)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ def test_not_found():
assert requests.get(url).status_code == 404

url = urls.base + "snakes/viper"
assert requests.get(url).status_code == 200
resp = requests.get(url)
assert resp.status_code == 200
assert resp.json() == "viper"

url = urls.base + "snakes-legacy/"
assert requests.get(url).status_code == 404
Expand Down Expand Up @@ -298,6 +300,8 @@ def test_raw_request():
req = requests.post(url, data={"z": 1}, headers={"testheader": "testheader-value"})
resp = req.json()
assert "testheader".upper() in resp["headers"]
assert "raw_body" in resp
assert "unset_field" not in resp


def test_custom_authorization_access():
Expand Down

0 comments on commit f63e45c

Please sign in to comment.