-
Notifications
You must be signed in to change notification settings - Fork 420
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
Allow has_one nil association #48
Conversation
Thanks @jeremyjung! If you could, could you add some tests? For guidance, I would suggest at least adding a test to |
The existing movie context (Movie, Actor, etc.) doesn’t have any has_one relations in it. I wasn’t sure if I should modify the existing models in there or create new models specifically to test has_one. |
@jeremyjung @grossadamm This issue could be fixed by doing a nil check in line 12 of this file https://github.com/Netflix/fast_jsonapi/blob/master/lib/extensions/has_one.rb Something like this association(:#{name}).reader&.id |
@shishirmk Good call with the dig method. I'm having a little trouble figuring out the appropriate tests for this. Putting this test in it 'returns correct json when has_one returns nil' do
supplier.account_id = nil
json = SupplierSerializer.new(supplier).serialized_json
serializable_hash = JSON.parse(json)
expect(serializable_hash['data']['relationships']['account']['data']).to be nil
end Another possibility could be just testing the extension method in the it 'has account_id method return nil if not present' do
expect(Supplier.find(@supplier_id_without_account).account_id).to eq nil
end In addition, any thoughts on removing the has_one extension method completely? Without the extension method I think we could still get similar behavior by using: public_send(:object_method_name)&.id |
@jeremyjung I think the test can go to Throughout the gem we try to support serialization with just the |
The &. Operator is available only ruby 2.3 onwards. That's why the builds are failing on 2.2.* version. Do you mind using something more verbose? |
@jeremyjung Thank you for making all the necessary changes |
* Allow has_one nil association * add test for when has_one returns nil * modify has_one extension method, add active record test * Use try operator to support old rubies
Attempt to fix issue #35 .
Any feedback or suggestions on where to add a test would be appreciated. I created a sample rails app to verify functionality.