json_api_client icon indicating copy to clipboard operation
json_api_client copied to clipboard

Not all prefix parameters specified

Open egoholic opened this issue 9 years ago • 7 comments

I have relation belongs_to for all parent association but I got the "Not all prefix parameters specified" error when try to create/update entity. Could anyone explain how I should define associations to make POST request on entityAs/:entityA_id/entityBs/entityB_id/entityCs/entityC_id/entityDs?

Now for EntityD I have:

  belongs_to :entityA
  belongs_to :entityB
  belongs_to :entityC

For now I can successfuly get EntityDs of EntityC, but I can't create or update them.

egoholic avatar Feb 24 '16 14:02 egoholic

I've got this problem as well. I can't work out how to add records that have a belongs_to relationship defined in the class.

JohnSmall avatar Jan 26 '17 13:01 JohnSmall

Yep, even in the more simple case of Content belongs_to ContentType, I can include the :content_type_id in my created Content < JsonApiClient::Resource object, and it will correctly route to .../content_types/:content_type_id/contents on a POST. However the default behavior seems to not include actually setting the content_type_id attribute on the created object.

Either it creates params like {"data"=>{"type"=>"contents", "attributes"=>{"content-type-id"=>1, "status"=>"Live"}}, "content_type_id"=>"1"} as seen in https://github.com/chingor13/json_api_client/issues/258 which simply returns "Param not allowed" OR If you exclude the foreign key in the 'attributes' hash, then the default behavior now ignores the relation and adds an error to the created object's errors hash like {"title"=>"must exist", "detail"=>"content-type - must exist", "code"=>"100", "source"=>{"pointer"=>"/data/relationships/content-type"}, "status"=>"422"}

Genkilabs avatar Jun 16 '17 00:06 Genkilabs

Same issue for me when I use polymorphic association.

Hitesh-bigscal avatar Jun 20 '18 06:06 Hitesh-bigscal

Is there any solution or workaround for this please? We used to use an early version of the gem (1.1.1) and we didn't have any issues. After upgrading to 1.5.3 we started seeing this "Not all prefix parameters specified" issue when trying to update a child resource.

adamdullenty avatar Sep 26 '18 12:09 adamdullenty

Hi @egoholic @JohnSmall @Genkilabs @Hitesh-bigscal @adamdullenty

I've reproduced your issue on version 1.5.3, but in 1.6.0 fixed You can upgrade to 1.6.0 but keep in mind that this version has one breaking change that could affect you (take a look at release notes)

class TestResource < JsonApiClient::Resource
  self.site = "http://example.com/"

  def self.key_formatter
    JsonApiClient::DasherizedKeyFormatter
  end

  def self.route_formatter
    JsonApiClient::UnderscoredKeyFormatter
  end
end

class MultiWordParent < Formatted
end

class MultiWordChild < Formatted
  belongs_to :multi_word_parent
end

class BelongsTo < MiniTest::Test
  def test_belongs_to_urls_create_record
    stub_request(:post, 'http://example.com/multi_word_parents/1/multi_word_children').
        with(headers: { content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json' }, body: {
            data: {
                type: 'multi_word_children',
                attributes: {
                    foo: 'bar',
                    'multi-word-field': true
                }
            }
        }.to_json)
        .to_return(headers: { content_type: 'application/vnd.api+json' }, body: {
            data: {
                    id: '2',
                    type: 'multi_word_children',
                    attributes: {
                        foo: 'bar',
                        'multi-word-field': true
                    }
                }
        }.to_json)

    record = MultiWordChild.new(multi_word_parent_id: 1, foo: 'bar', multi_word_field: true)
    result = record.save
    assert result
    assert_equal('2', record.id)
  end
end

senid231 avatar Oct 02 '18 16:10 senid231

we have this issue fixed in 1.6.0

to make it work in 1.5.3 you need to add prefix parameter to read_only_attributes

@Genkilabs in your case it could be something like:

class BaseResource < JsonApiClient::Resource
  self.site = "http://example.com/"
end

class ContentType < BaseResource
end

class Content < BaseResource
  belongs_to :content_type
  self.read_only_attributes = read_only_attributes + [:content_type_id]
end

senid231 avatar Oct 03 '18 07:10 senid231

@egoholic you have a very strange case but if I think if you add [:entity_a, :entity_b, :entity_c] to read_only_attributes it will work too

senid231 avatar Oct 03 '18 07:10 senid231