Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 5, 2023

Conversation

randyv12
Copy link
Contributor

@randyv12 randyv12 commented Apr 23, 2021

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

… to have a modified id method that returns the id as an integer. This gives the application code the flexibility to match against attributes of other resources that are integers.
@gaorlov
Copy link
Collaborator

gaorlov commented Aug 12, 2022

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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

Copy link
Collaborator

@gaorlov gaorlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! thanks for contributing!
Please merge in main to get tests running in the PR
Thanks!

@randyv12
Copy link
Contributor Author

randyv12 commented Jun 2, 2023

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

@gaorlov
Copy link
Collaborator

gaorlov commented Jun 2, 2023

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 gaorlov merged commit f7fa7e8 into JsonApiClient:master Jun 5, 2023
@gaorlov
Copy link
Collaborator

gaorlov commented Jun 5, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants