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

Overlapping IDs leads to confusing behaviour

Open doconnor-clintel opened this issue 4 years ago • 0 comments

Not sure if this is better here; or in another place like https://github.com/jsonapi-rb/jsonapi-renderer/issues/34

When you have a similar data type, but it has been modelled as two separate things (for internal reasons you don't wish to inflict on API consumers); you may try to do something like:

    class SerializableFoo < ::JSONAPI::Serializable::Resource
       type "foobar"
       
       attributes :bizz, :buzz, :things_that_arent_id
       
    class SerializableBar < SerializableFoo
       type "foobar"

This works until you create two different records with the same ID and pass them off for serialization in the same payload - only the first record will be rendered.

The workaround is to define id:

    class SerializableFoo < ::JSONAPI::Serializable::Resource
       type "foobar"
       id { "foo-#{@object.id}" }
   
   
   class SerializableBar < SerializableFoo
       type "foobar"
       id { "bar-#{@object.id}" }

However; this is difficult to discover. IE: Where used with activerecord, 95% of the time this will be "fine" in production if you have different IDs, until you hit a rare conflict.

Consider:

  • Expanding the examples in the readme?
  • Emitting warnings if you push something onto the rendering stack that already exists and is different to the existing record or came from a different serializer?

doconnor-clintel avatar Jul 23 '21 10:07 doconnor-clintel