olosegres / jsona

Tiny and simple JSON:API serializer / deserializer. Creates simplified objects from JSON or stored reduxObject, creates JSON from the same simplified objects (in according with JSON API specification)
MIT License
206 stars 27 forks source link

Links are ignored during deserialization #22

Closed FeliciousX closed 4 years ago

FeliciousX commented 6 years ago

the default simplePropertyMappers does not work with pagination when body contains link

example..

{
  "meta": {
    "totalPages": 13
  },
  "data": [
    {
      "type": "articles",
      "id": "3",
      "attributes": {
        "title": "JSON:API paints my bikeshed!",
        "body": "The shortest article. Ever.",
        "created": "2015-05-22T14:56:29.000Z",
        "updated": "2015-05-22T14:56:28.000Z"
      }
    }
  ],
  "links": {
    "self": "http://example.com/articles?page[number]=3&page[size]=1",
    "first": "http://example.com/articles?page[number]=1&page[size]=1",
    "prev": "http://example.com/articles?page[number]=2&page[size]=1",
    "next": "http://example.com/articles?page[number]=4&page[size]=1",
    "last": "http://example.com/articles?page[number]=13&page[size]=1"
  }
}

when deserialized, will be

[ { type: 'articles',
    id: '3',
    title: 'JSON:API paints my bikeshed!',
    body: 'The shortest article. Ever.',
    created: '2015-05-22T14:56:29.000Z',
    updated: '2015-05-22T14:56:28.000Z' } ]

I see that setLinks exists on the simplePropertyMapper here

but it seems that only links inside data will be checked, but not the body.links ( which is where links will exist if it's for pagination )

olosegres commented 5 years ago

@FeliciousX @stilio @kartikluke

Hi! Is the problem still relevant?

And do you have any ideas how should look deserialized output with root meta and links, when there is collection or one item in data?

Like this?

type TDeserialized = {
  stuff: TJsonaModel | TJsonaModel[] | null,
  meta: TAnyKeyValueObject,
  links: TLinks,
};
konovalov-nk commented 5 years ago

The problem is still relevant @olosegres. I'm looking forward to migrate from previously used jsonapi deserializer library because it doesn't support circular relationships properly and maintainer seems to be very busy 🙂

I believe you should do it like that:

type TDeserialized = {
  data: TJsonaModel | TJsonaModel[] | null, // "stuff" isn't very descriptive
  meta: TAnyKeyValueObject,
  links: TLinks,
};
olosegres commented 5 years ago

@konovalov-nk I use "stuff" to show that it is not "data" from json, in the code they are found nearby

olosegres commented 5 years ago

@konovalov-nk Such an update creates a number of inconveniences, the main of which is the difficulty of updating to new versions for those already using jsona.

Why you can't extend Jsona class in your code or just wrap jsona.deserialize in function, that will add links and meta to output?

konovalov-nk commented 5 years ago

Yup that's exactly what we are doing now but we also would like to deeply camelize all keys (even nested ones) we're receiving from the back-end. Otherwise, we have to use some kind of pre-processing, like transformResponse from the axios. It works, but workaround seems to be a little hacky.

This could be an option and surely doesn't have to be a default behaviour. It's more about the vision for the jsona. Do you feel like it should stick to the JSON:API standard and implement specifications exactly like visioned? Or rather implement things a bit different using your/community perspective?

olosegres commented 5 years ago

Jsona is a tool to simplify the work with standardized data. Everything inside the meta is not standardized and jsona cannot take into account all the special cases. Thus, everything that is not standardized will look a bit hacky, I think this is normal.

But if you see a way to make jsona better, then welcome, we can consider your PRs :)