frioux / DBIx-Class-Helpers

https://metacpan.org/pod/DBIx::Class::Helpers
20 stars 38 forks source link

Relationships JSON #75

Closed 0xbad0c0d3 closed 7 years ago

0xbad0c0d3 commented 8 years ago

When we use relationships - your TO_JSON won't process it. For example: if table 'users' and 'plans' with relationship user.plan -> plan.id then, when we do $user->TO_JSON we'll receive this: {"user_id":1,"plan":MyAPP::Result::Plan=HASH(0x00000000)} With this patch result will be like this: {"user_id":1,"plan":{"id":1,"name":"wow"}}

coveralls commented 8 years ago

Coverage Status

Coverage remained the same at 98.447% when pulling c40852e47fa843954a646101dfea46aa440b3701 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 8 years ago

Coverage Status

Coverage remained the same at 98.447% when pulling c40852e47fa843954a646101dfea46aa440b3701 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 8 years ago

Coverage Status

Coverage remained the same at 98.447% when pulling c40852e47fa843954a646101dfea46aa440b3701 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 8 years ago

Coverage Status

Coverage remained the same at 98.447% when pulling c40852e47fa843954a646101dfea46aa440b3701 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

frioux commented 8 years ago

Please add a test demonstrating and exercising this. Thanks.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.003%) to 98.45% when pulling fd98a996296a450f9ef10176ff3440c8312bbf62 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.003%) to 98.45% when pulling fd98a996296a450f9ef10176ff3440c8312bbf62 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.003%) to 98.45% when pulling fd98a996296a450f9ef10176ff3440c8312bbf62 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.003%) to 98.45% when pulling fd98a996296a450f9ef10176ff3440c8312bbf62 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.003%) to 98.45% when pulling fd98a996296a450f9ef10176ff3440c8312bbf62 on 0xbad0c0d3:deepJson into 9f681d693268e03e867c809d3c4e858e174c66e5 on frioux:master.

frioux commented 7 years ago

Ok, I have a bunch of problems with this. I asked for tests because the original PR was unclear as to exactly what it does. Here are my main issues, in order of importance:

  1. It would break code that expected ids.
  2. It only works for relationships that happen to be exactly the same name as columns.
  3. Even if you fixed my second issue, it would be increasing the size of the returned JSON object in a way that people would have to opt out of, instead of opt in to.

I would rather add documentation on how to achieve what you've done generically than change code that people are already depending on. Another option would be to make a subclass of this called something like ToJSON::WithRelated, but even then, it is strange to include belongs_to's but not has_manys, and if we fix that we can end up with loops, which I am not super interested in resolve.

What do you think?

frioux commented 7 years ago

Closing this since it's been two months.