Sitelet https://github.com/jsonapi-serializer/jsonapi-serializer/pull/17
Skip to content

Support for polymorphic id_method_name - #17

Merged
stas merged 2 commits into
jsonapi-serializer:masterfrom
pegulabs:id-bugs
Dec 3, 2019
Merged

stas merged 2 commits into
jsonapi-serializer:masterfrom
pegulabs:id-bugs

Conversation

@aub

@aub aub commented Oct 30, 2019

Copy link
Copy Markdown
Contributor

This is a duplicate of my PR against the Netflix project.

For polymorphic has_many relations, instead of getting the ids in a batch, the code loops over each object to get its type and id. In Relationship#id_hash_from_record it was hardcoded to get the id by calling record.id, which overrides the id_method_name options passed into the relationship.

I tried fixing this by only changing the code in Relationship to dynamically call the id_method_name on the object, but unfortunately on has_many relationships the default value for id_method_name would be passed as if the ids would be pulled in a batch (for example groupee_ids). That default works for regular has_manys where we get the ids all at once but fails for polymorphic ones where it loops over each object.

So, fixing the problem required passing the polymorphic information to compute_id_method_name, which now defaults to id rather than id_method_name_from_relationship in the case where the relationship is polymorphic.

This also fixes a bug where the record_id is not carried over through inheritance.

@stas

stas commented Oct 30, 2019

Copy link
Copy Markdown
Collaborator

@aub, thanks for submitting this, it looks very clean. Do you mind changing the tests or adding a new one maybe?

@aub

aub commented Nov 5, 2019

Copy link
Copy Markdown
Contributor Author

Hey @stas sorry for the holdup but I've added a couple of tests. Thanks!

@stas stas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is good to go. Thanks @aub !

it 'inherits the id source' do
e = Employee.new
e.id = 2
e.uuid = 'dfsdfsd'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@aub consider using SecureRandom.uuid to have valid realistic data in tests.

@stas

stas commented Nov 21, 2019

Copy link
Copy Markdown
Collaborator

@jopotts @kpheasey any interest in taking a look or we're good to go here?

@dudo

dudo commented Nov 21, 2019

Copy link
Copy Markdown

I'm pretty stoked to see this pulled over, and to see continued development on this incredible gem. I'll be contributing myself now that it's properly open sourced!

@jopotts

jopotts commented Dec 3, 2019

Copy link
Copy Markdown
Contributor

Sorry for the holdup on this. Yes - looks ok to me. I agree with the point about the UUID in the test, as a keyboard mash doesn't give the right signals in terms of code quality, but it's not a blocker.

aub added 2 commits December 3, 2019 09:47
(cherry picked from commit 5791c356b894bd4e0338953721f121522e56defd)

add specs

(cherry picked from commit a25580a59cfda36af6de396b87e0767322e33d7e)
@aub

aub commented Dec 3, 2019

Copy link
Copy Markdown
Contributor Author

Ok, I changed it to use SecureRandom.uuid

@stas stas changed the title fix id method bugs Support for polymorphic id_method_name Dec 3, 2019
@stas
stas merged commit 1d7c18f into jsonapi-serializer:master Dec 3, 2019
@stas

stas commented Dec 3, 2019

Copy link
Copy Markdown
Collaborator

Thank you very much @aub!

BrianSigafoos pushed a commit to BrianSigafoos/fast_jsonapi that referenced this pull request Dec 3, 2019
* fix id method bugs, add specs
* Use SecureRandom.uuid
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.

4 participants