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

Inconsistentcy, have_jsonapi_attributes should not allow symbols #17

Closed
nattfodd opened this issue May 4, 2020 · 5 comments
Closed

Comments

@nattfodd
Copy link

nattfodd commented May 4, 2020

FastJsonapi serializer returns keys as symbols with MySerializer.new(data).serializable_hash, including :attributes. It's pretty unobvious that you can say expect(data).to have_jsonapi_attributes(:foo, :bar) but your data object should be { 'attributes' => { foo: 1, bar: 2 } } with "attribute" key being precisely a string.

return false unless actual.key?('attributes')

@stas
Copy link
Collaborator

stas commented May 4, 2020

@nattfodd thanks for reporting this... but could you please explain or provide an example what exactly is the problem here? I'm not sure I understand where's the inconsistency?

Are you trying to say that have_jsonapi_attributes allows symbols and the other matchers do not?!

@nattfodd
Copy link
Author

nattfodd commented May 5, 2020

Here is an example:

class ProductSerializer
  include FastJsonapi::ObjectSerializer

  attributes :title, :price
end

describe ProductSerializer do
  let(:product) { FactoryBot.create(:product) }

  it 'has title & price attributes' do
    data = ProductSerializer.new(product).serializable_json[:data]
    expect(data).to have_jsonapi_attributes(:title, :price) # fails
    expect(data.with_indifferent_access).to have_jsonapi_attributes(:title, :price) # works
    expect(data).to have_jsonapi_attributes('title', 'price') # also works
  end
end

The first expectation expect(data).to have_jsonapi_attributes(:title, :price) is basically taken from README. FastJsonapi::ObjectSerializer returns data with keys as symbols as well. While the have_jsonapi_attributes matcher expects them to be strings.

@stas stas changed the title have_attributes relies on 'attributes' key, inconsistent with ability to provide symbolized attributes Inconsistentcy with have_jsonapi_attributes, should not allow symbols May 5, 2020
@stas stas changed the title Inconsistentcy with have_jsonapi_attributes, should not allow symbols Inconsistentcy, have_jsonapi_attributes should not allow symbols May 5, 2020
@stas
Copy link
Collaborator

stas commented May 5, 2020

Thanks @nattfodd, makes sense now. Patches welcome btw 🙃

@stas
Copy link
Collaborator

stas commented Jun 29, 2020

@nattfodd please give the latest version a test, there's a new option to configure the matchers to be indifferent to symbols/strings.

@stas
Copy link
Collaborator

stas commented Jul 30, 2020

@nattfodd I'm closing this as part of 56226ed

Since the JSON:API requires a valid JSON document which means string keys, the marchers will always try to do the conversion where spec enforces it:
https://github.com/jsonapi-rb/jsonapi-rspec#on-matcher-arguments

@stas stas closed this as completed Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants