Skip to content

Commit

Permalink
remove null fields from response
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Cezar Bernardelli committed Jul 9, 2021
1 parent d5e5de0 commit 682dbbe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
30 changes: 18 additions & 12 deletions raiden/messages/metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import deepcopy
from dataclasses import dataclass, field
from dataclasses import dataclass
from typing import Any

import canonicaljson
Expand Down Expand Up @@ -79,7 +79,7 @@ class Metadata:
# inititally create the Metadata object as part of a message - this is the case when we are
# the initiator of a transfer.
original_data: Optional[Any] = None
encrypted_secret: Optional[EncryptedSecret] = field(default=None, repr=False)
encrypted_secret: Optional[EncryptedSecret] = None

class Meta:
"""
Expand All @@ -89,6 +89,7 @@ class Meta:
"""

unknown = EXCLUDE
serialize_missing = False

@cached_property
def hash(self) -> bytes:
Expand All @@ -102,18 +103,23 @@ def _post_load( # pylint: disable=no-self-use,unused-argument
return data

@post_dump(pass_many=True)
def _post_dump( # pylint: disable=no-self-use,unused-argument
def _post_dump( # pylint: disable=unused-argument
self, data: Dict[str, Any], many: bool
) -> Dict[str, Any]:
original_data = data.pop("original_data", {})
if original_data:
# We received the metadata (Mediator/Target) and read in the data
# for internal processing,
# so once we pass them to the next node just dump the data exactly as we received them
return original_data
# We initially created the Metadata (Initiator),
# so dump the known fields as per the Schema
return data
"""
`original_data` means we received the metadata (Mediator/Target) and read in the data
for internal processing, so once we pass them to the next node just dump the data exactly
as we received them.
Returning `data` means we initially created the Metadata (Initiator), so dump the known
fields as per the Schema
"""
dumped_data = data.pop("original_data", None) or data

if not self.Meta.serialize_missing:
dumped_data = {k: v for k, v in dumped_data.items() if v is not None}

return dumped_data

def __repr__(self) -> str:
return f"Metadata: routes: {[repr(route) for route in self.routes]}"
Expand Down
3 changes: 1 addition & 2 deletions raiden/tests/unit/serialized_messages/LockedTransfer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"locksroot": "0x07fe6255272aa923234b651199a2d7a277e3b1af3156f18c3bc0ba45d27fa380",
"message_identifier": "1",
"metadata": {
"encrypted_secret": null,
"routes": [
{
"route": [
Expand All @@ -24,7 +23,7 @@
"nonce": "1",
"payment_identifier": "1",
"recipient": "0x7461726765747461726765747461726765747461",
"signature": "0xdae92f24535a87229b04f79d4f419cfd2263a7c27e815194d9fc91cc9d13cbc27108480cbf6a9aa502e36cec23a79cdabc126356dcd76e2631f1b6eb6eaa94611c",
"signature": "0xcf304f3401934837830cff2086a29987eb7035bdfec12b1c73de8793625f982b4ec35504cc40db951ed8eebb44123a87679a615300177858babd950b2da3e4391b",
"target": "0x7461726765747461726765747461726765747461",
"token": "0x746f6b656e746f6b656e746f6b656e746f6b656e",
"token_network_address": "0x6e6574776f726b6e6574776f726b6e6574776f72",
Expand Down
1 change: 0 additions & 1 deletion raiden/tests/unit/serialized_messages/RefundTransfer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"locksroot": "0x07fe6255272aa923234b651199a2d7a277e3b1af3156f18c3bc0ba45d27fa380",
"message_identifier": "1",
"metadata": {
"encrypted_secret": null,
"routes": [
{
"route": [
Expand Down

0 comments on commit 682dbbe

Please sign in to comment.