Skip to content

Fix nested namespaces parsing and resource resolving#1028

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
elvanja:fix-nested-relationships
May 5, 2017
Merged

Fix nested namespaces parsing and resource resolving#1028
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
elvanja:fix-nested-relationships

Conversation

@elvanja
Copy link
Copy Markdown
Contributor

@elvanja elvanja commented Apr 14, 2017

With this setup:

class Managing::Profile < ActiveRecord::Base
  belongs_to :company
end

class Company < ActiveRecord::Base
  belongs_to :default_managing_profile, class_name: '::Managing::Profile'
end

class Api::V1::BaseResource < JSONAPI::Resource
  abstract

  attributes :created_at, :updated_at

  def self.updatable_fields(context)
    super - [:created_at, :updated_at]
  end

  def self.creatable_fields(context)
    super - [:created_at, :updated_at]
  end
end

class Api::V1::Managing::ProfileResource < Api::V1::BaseResource
  model_name 'Managing::Profile'
  attributes :name
end

class Api::V1::CompanyResource < Api::V1::BaseResource
  model_name 'Company'
  attributes :name
  has_one :default_managing_profile, class_name: 'Managing::Profile'
end

, there were several things that didn't work:

  • on startup, without class_name: 'Managing::Profile' in CompanyResource, it would report that it could not find related model
  • requesting included profile via http://localhost:3000/api/v1/companies/3?include=default_managing_profile, it would also break, unable to find the related resource
  • on posting a new company, with relation to existing managing profile, it would again break, for the same reason

example of post payload:

{
  "data": {
    "attributes": {
      "name": "Dummy"
    },
    "relationships": {
      "default_managing_profile": {
        "data": {
          "type": "profiles",
          "id": "3"
        }
      }
    },
    "type": "companies"
  }
}

I also tried setting the data type for default_managing_profile to one of:

  • managing/profiles
  • /managing/profiles
  • Managing::Profile
  • managing_profiles

and probably some other too, but without luck.

The fix in resource.rb solved first two issues. The fix in request_parser.rb fixed the last one. I tried adding related tests, but could not find a simple way to test the last issue.

Hopefully this makes sense 😄

P.S. I've noticed that RequestParser#parse_to_many_relationship has a bit different logic than parse_to_one_relationship so maybe I'm missing something here. Maybe there is a way to accomplish the desired outcome without this fix?

P.P.S. Possibly related to #893 and #927.

@lgebhardt lgebhardt merged commit e58687c into JSONAPI-Resources:master May 5, 2017
@lgebhardt
Copy link
Copy Markdown
Contributor

@elvanja Thanks!

@elvanja elvanja deleted the fix-nested-relationships branch May 17, 2017 07:16
@elvanja elvanja restored the fix-nested-relationships branch May 17, 2017 08:28
@elvanja elvanja deleted the fix-nested-relationships branch June 12, 2017 14:51
@kented
Copy link
Copy Markdown

kented commented Jun 29, 2017

This issue was causing me some pain. Thanks for the fix, @elvanja

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.

3 participants