json_api_client icon indicating copy to clipboard operation
json_api_client copied to clipboard

feat: Add the ability to override getter for JsonApiClient::Resource id

Open randyv12 opened this issue 4 years ago • 1 comments

Hello, I just want to ask and see if we can modify a single line in one file included_data.rb. I ran into an issue needing to add a method id that will normalize the raw ids into integers, in cases of belongs_to, relationships. Please see test file here.

Desired Functionality: I want to be able create a base class that overrides the id method of a resource and consistently return it as an integer. Because I want to be able to transform back, the original type of my resource id.

This happens when we serialize active record / application record objects from mysql through json-apiserializer gem. Their representation of id's turn into strings, that's how the json api documentation declares it so.

There seems to be only a one-liner change so that we can override the id method of a JsonApiClient::Resource

This adds the ability to create a subclass of JsonApiClient::Resource to have a modified id method that returns the id as an integer. This gives the application code the flexibility to match against attributes (ids of type integer) of other resources that are integers, which weren't translated to strings, (from gems such as json-apiserializer).

randyv12 avatar Apr 23 '21 01:04 randyv12

while this is strictly speaking against the spec, i think the change is reasonable. can you please fix the tests and add an entry to the ##Unreleased section of the changelog? Thanks!

gaorlov avatar Aug 12 '22 21:08 gaorlov

Tests seems to pass:

242 runs, 735 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for Unit Tests to /Users/xxx/Dev/json_api_client/coverage. 2908 / 2977 LOC (97.68%) covered.
 json_api_client % rake test

randyv12 avatar May 31 '23 00:05 randyv12

Oh I see, the method name in one of the test was incorrectly prefixed without test_ . Updated.

randyv12 avatar May 31 '23 00:05 randyv12

would you like me to fix those tests for different ruby versions? i think its got to do with Fixnum vs Integer

randyv12 avatar Jun 02 '23 17:06 randyv12

would you like me to fix those tests for different ruby versions? i think its got to do with Fixnum vs Integer That would be ideal, yea. Thanks!

gaorlov avatar Jun 02 '23 17:06 gaorlov

1.21.1 is now live. Thanks for your work and patience!

gaorlov avatar Jun 05 '23 17:06 gaorlov