Skip to content

Commit

Permalink
Merge pull request #106 from sapcc/handle_json_string
Browse files Browse the repository at this point in the history
handle payload json string to dict, type check
  • Loading branch information
notque authored Feb 3, 2025
2 parents 2361acf + 7ae2293 commit 5addf8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions auditmiddleware/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,26 @@ def _get_action_from_payload(self, request, res_spec, res_id):
"""Determine the CADF action from the payload."""
try:
payload = request.json

# Handle case where payload is a string (JSON-encoded)
if isinstance(payload, str):
try:
payload = json.loads(payload)
except json.JSONDecodeError:
self._log.warning(
"Invalid JSON string payload for path: %s",
request.path)
return None

if payload:
# Type checking for payload
if not isinstance(payload, dict):
self._log.warning(
"Unexpected payload type %s for path: %s",
type(payload), request.path
)
return None

rest_action = next(iter(payload))
# check for individual mapping of action
action = res_spec.custom_actions.get(rest_action)
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ coverage!=4.4,>=4.0 # Apache-2.0
fixtures>=3.0.0 # Apache-2.0/BSD
mock>=3.0.0 # BSD
oslotest>=3.8.0 # Apache-2.0
pytz==2024.2 # MIT
requests-mock>=1.2.0 # Apache-2.0
#stevedore>=1.20.0 # Apache-2.0
stestr>=2.0.0 # Apache-2.0
Expand Down

0 comments on commit 5addf8c

Please sign in to comment.