jsonapi-deserializable icon indicating copy to clipboard operation
jsonapi-deserializable copied to clipboard

Relationships and attributes property collision

Open leoamigood opened this issue 2 years ago • 0 comments

There is an issue in case payload deserialization like this happens:

{
   "data":{
      "type":"billing_period",
      "attributes":{
         "invoiceType":"prepayment",
         "billingPeriodStart":"2022-06-01",
         "billingPeriodEnd":"2022-07-01",
         "amount":"2000"
      },
      "relationships":{
         "invoice":{
            "data":{
               "type":"invoice",
               "id":"42b2a67b-5c50-4d8f-b91e-e4269933b007"
            }
         }
      }
   }
}

In this case due to hash merging order relationships always take precedence over attributes, so the deserialized attribute invoiceType in the case above will be invoice instead of expected prepayment.

The fix may be as easy as switching the order for deserialize_rels and deserialize_attrs

        hashes = [deserialize_type, deserialize_id,
                  deserialize_rels, deserialize_attrs]

though it's not backwards compatible.

Please suggest, Leo

leoamigood avatar Sep 26 '23 12:09 leoamigood