JsonApiClient / json_api_client

Build client libraries compliant with specification defined by jsonapi.org
MIT License
361 stars 186 forks source link

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

Closed randyv12 closed 1 year ago

randyv12 commented 3 years ago

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).

gaorlov commented 2 years ago

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!

randyv12 commented 1 year ago

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 commented 1 year ago

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

randyv12 commented 1 year ago

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

gaorlov commented 1 year ago

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 commented 1 year ago

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