From 682dbbe4bb3c397558889bfdf77ac6102be02344 Mon Sep 17 00:00:00 2001 From: Fernando Cezar Bernardelli Date: Fri, 9 Jul 2021 14:50:38 +0200 Subject: [PATCH] remove null fields from response --- raiden/messages/metadata.py | 30 +++++++++++-------- .../serialized_messages/LockedTransfer.json | 3 +- .../serialized_messages/RefundTransfer.json | 1 - 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/raiden/messages/metadata.py b/raiden/messages/metadata.py index 5a200f9b8e1..8157f61ce64 100644 --- a/raiden/messages/metadata.py +++ b/raiden/messages/metadata.py @@ -1,5 +1,5 @@ from copy import deepcopy -from dataclasses import dataclass, field +from dataclasses import dataclass from typing import Any import canonicaljson @@ -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: """ @@ -89,6 +89,7 @@ class Meta: """ unknown = EXCLUDE + serialize_missing = False @cached_property def hash(self) -> bytes: @@ -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]}" diff --git a/raiden/tests/unit/serialized_messages/LockedTransfer.json b/raiden/tests/unit/serialized_messages/LockedTransfer.json index 6bf3dc10865..fc97b27f38d 100644 --- a/raiden/tests/unit/serialized_messages/LockedTransfer.json +++ b/raiden/tests/unit/serialized_messages/LockedTransfer.json @@ -11,7 +11,6 @@ "locksroot": "0x07fe6255272aa923234b651199a2d7a277e3b1af3156f18c3bc0ba45d27fa380", "message_identifier": "1", "metadata": { - "encrypted_secret": null, "routes": [ { "route": [ @@ -24,7 +23,7 @@ "nonce": "1", "payment_identifier": "1", "recipient": "0x7461726765747461726765747461726765747461", - "signature": "0xdae92f24535a87229b04f79d4f419cfd2263a7c27e815194d9fc91cc9d13cbc27108480cbf6a9aa502e36cec23a79cdabc126356dcd76e2631f1b6eb6eaa94611c", + "signature": "0xcf304f3401934837830cff2086a29987eb7035bdfec12b1c73de8793625f982b4ec35504cc40db951ed8eebb44123a87679a615300177858babd950b2da3e4391b", "target": "0x7461726765747461726765747461726765747461", "token": "0x746f6b656e746f6b656e746f6b656e746f6b656e", "token_network_address": "0x6e6574776f726b6e6574776f726b6e6574776f72", diff --git a/raiden/tests/unit/serialized_messages/RefundTransfer.json b/raiden/tests/unit/serialized_messages/RefundTransfer.json index e1314406973..3a330d86fea 100644 --- a/raiden/tests/unit/serialized_messages/RefundTransfer.json +++ b/raiden/tests/unit/serialized_messages/RefundTransfer.json @@ -11,7 +11,6 @@ "locksroot": "0x07fe6255272aa923234b651199a2d7a277e3b1af3156f18c3bc0ba45d27fa380", "message_identifier": "1", "metadata": { - "encrypted_secret": null, "routes": [ { "route": [