jsonapi-rb / jsonapi-deserializable

Conveniently deserialize JSON API payloads into custom hashes.
http://jsonapi-rb.org
MIT License
26 stars 20 forks source link

Relationships and attributes property collision #27

Open leoamigood opened 11 months ago

leoamigood commented 11 months ago

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