Skip to content
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

Create with belongs_to includes foreign key in data.attributes #258

Open
pbrisbin opened this issue Jun 5, 2017 · 0 comments
Open

Create with belongs_to includes foreign key in data.attributes #258

pbrisbin opened this issue Jun 5, 2017 · 0 comments

Comments

@pbrisbin
Copy link

pbrisbin commented Jun 5, 2017

We might be missing something here, but I think this behavior is incorrect:

Model

module CC
  class API
    class RefPoint < Base
      belongs_to :repo

      property :commit_sha, type: :string
      property :created_at, type: :time
      property :source, type: :string
    end
  end
end

Action

# Note: as_service just adds some authentication headers
CC::API::RefPoint.as_service(SERVICE_NAME).create(
  repo_id: repo.id,
  commit_sha: commit.id,
  created_at: commit.committed_date,
  source: REF_POINT_SOURCE,
)

Resulting POST

Started POST "/v1/repos/58f7a549ff0bb002860006aa/ref_points" ...
  Processing by V1::Repos::RefPointsController#create as JSON
  Parameters: {"data"=>{"type"=>"ref_points", "attributes"=>{"repo_id"=>"58f7a549ff0bb002860006aa", "commit_sha"=>"dfdf1c8406bcb975f965c6bb2373f7086a0792f8", "created_at"=>"2015-05-29 23:11:49 +0000", "source"=>"backfill"}}, "repo_id"=>"58f7a549ff0bb002860006aa"}

Expected POST

Started POST "/v1/repos/58f7a549ff0bb002860006aa/ref_points" ...
  Processing by V1::Repos::RefPointsController#create as JSON
  Parameters: {"data"=>{"type"=>"ref_points", "attributes"=>{"commit_sha"=>"dfdf1c8406bcb975f965c6bb2373f7086a0792f8", "created_at"=>"2015-05-29 23:11:49 +0000", "source"=>"backfill"}}, "repo_id"=>"58f7a549ff0bb002860006aa"}

So it was hitting repos/:repo_id/ref_points as expected, but it was also including the repo_id in data.attributes of the POST body, which is not expected. This caused a BadRequest because that parameter is not permitted by our server.

Are we doing something wrong? Are we misunderstanding jsonapi? Or is this a bug?

To work around it, we just needed to permit (and ignore) the extra repo_id in our Rails params, but we wanted to report it in case it's a bug in the gem.

Version: 1.5.1

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

No branches or pull requests

1 participant