fix: format model polymorphic type from resource object type#1435
Merged
fix: format model polymorphic type from resource object type#1435
Conversation
a45b85d to
4d92877
Compare
bf4
commented
Jan 22, 2024
|
|
||
| class Document < ActiveRecord::Base | ||
| has_many :pictures, as: :imageable | ||
| has_many :pictures, as: :imageable # polymorphic |
bf4
commented
Jan 22, 2024
bf4
commented
Jan 22, 2024
bf4
commented
Jan 22, 2024
lgebhardt
requested changes
Jan 22, 2024
bf4
commented
Jan 22, 2024
| end | ||
|
|
||
| def polymorphic_type_for(model_name) | ||
| model_name&.to_s&.classify |
bf4
commented
Jan 22, 2024
| define_on_resource "#{relationship.foreign_key}=" do |v| | ||
| _model.method("#{relationship.foreign_key}=").call(v[:id]) | ||
| _model.public_send("#{relationship.polymorphic_type}=", v[:type]) | ||
| _model.public_send("#{relationship.foreign_key}=", v[:id]) |
Collaborator
Author
There was a problem hiding this comment.
I removed the instantiation of the method object here in favor of public send since we couldn't think of a reason the method object would be preferable
bf4
commented
Jan 22, 2024
| _model.method("#{relationship.foreign_key}=").call(v[:id]) | ||
| _model.public_send("#{relationship.polymorphic_type}=", v[:type]) | ||
| _model.public_send("#{relationship.foreign_key}=", v[:id]) | ||
| _model.public_send("#{relationship.polymorphic_type}=", self.class.polymorphic_type_for(v[:type])) |
lgebhardt
added a commit
that referenced
this pull request
Apr 18, 2024
* test: failing request posting sti with polymorphic has one * fix: polymorphic resource assignment * Add polymorphic_type_for method * Favor classify over singularize.camelize --------- Co-authored-by: lgebhardt <larry@cerebris.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In our app which is currently on the latest 0.9 we have a test
failing on the 0-11-dev branch (which was also failing on 0-10)
which posts the JSON body
{ "data": { "type": "broker-memberships", "relationships": { "organization": { "data": { "type": "brokers", "id": "1" } }, "user": { "data": { "type": "users", "id": "2" } } }, "attributes": { "external-employee-id": "123" } }, }to
v1/broker-membershipsBrokerMembership is an STI ActiveRecord model
below Membership like below.
our BrokerMembership resource looks like
In
_replace_fieldsyou can see the parsedfield_dataisAnd in the call to
_replace_polymorphic_to_one_link(as defined inJSONAPI::ResourceCommon)
calls the method defined by JSONAPI::ResourceCommon.define_on_resource like
which practially speaking calls
This latter behavior causes a failure since the 'organization_type' should be 'Broker' and not ':brokers'.
I'm not sure if this should be addressed in the definition of
define_on_resource(by making it lookup the polymorphic type)or in the
JSONAPI::Request(Parser).parse_to_one_relationship(by removing the unformat and calling classify) or elsewhere.All Submissions:
New Feature Submissions:
Bug fixes and Changes to Core Features:
Test Plan:
Reviewer Checklist: