olosegres/jsona

Deserializer uses wrong cache if array of entities does not have id

christianjul opened this issue · 2 comments

In https://github.com/olosegres/jsona/blob/master/src/builders/JsonDeserializer.ts#L56 type and id are used as key in a local to avoid dezeriazling the same object more than once.

I a scenario where the entities does not have an id yet, which is allowed upon creation of resources, this leads to a fake cache hit and thus an array of the same objects.

Example:

{
  "data": [
    {
      "type": "language-knowledges",
      "relationships": {
        "sourceLanguage": {
          "data": {
            "type": "source-languages",
            "id": "2b86b376-07aa-45ab-9287-5e3fcdfb34df"
          }
        },
        "workArea": {
          "data": {
            "type": "work-areas",
            "id": "843b6b88-0c4e-4078-bedb-72ec4d4a19f5"
          }
        }
      }
    },
    {
      "type": "language-knowledges",
      "relationships": {
        "sourceLanguage": {
          "data": {
            "type": "source-languages",
            "id": "2caa0dc2-877c-4667-80e0-869707ad3ad1"
          }
        },
        "workArea": {
          "data": {
            "type": "work-areas",
            "id": "843b6b88-0c4e-4078-bedb-72ec4d4a19f5"
          }
        }
      }
    }
  ]
}

deserializes to

[ { type: 'language-knowledges',
    id: undefined,
    sourceLanguage:
     { type: 'source-languages',
       id: '2b86b376-07aa-45ab-9287-5e3fcdfb34df' },
    workArea:
     { type: 'work-areas',
       id: '843b6b88-0c4e-4078-bedb-72ec4d4a19f5' },
    relationshipNames: [ 'sourceLanguage', 'workArea' ] },
  { type: 'language-knowledges',
    id: undefined,
    sourceLanguage:
     { type: 'source-languages',
       id: '2b86b376-07aa-45ab-9287-5e3fcdfb34df' },
    workArea:
     { type: 'work-areas',
       id: '843b6b88-0c4e-4078-bedb-72ec4d4a19f5' },
    relationshipNames: [ 'sourceLanguage', 'workArea' ] } ]

Note the id's of the source-languages.

It can be worked around by assigning and id before deserializing.

It would be good if Jsona either used a more robust key for the cache, or threw an error on a missing id.

Thanks for supplying this great library!

@christianjul Thank you!

This omission fixed in 1.1.10

From now it will not use cache for models without ids.
fc986c0

@olosegres That was fast! I can confirm that it fixes the issue in my case. Thank you so much!