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

Records cannot be added to an empty ActiveJSON datastore #203

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

davidstosik
Copy link
Contributor

@davidstosik davidstosik commented Mar 5, 2020

Here's the simplest way to reproduce the error:

# test.rb

require "active_hash"

class Test < ActiveJSON::Base; end

Test.create()
$ echo '[]' > tests.json
$ ruby test.rb
Traceback (most recent call last):
        3: from test.rb:7:in `<main>'
        2: from /Users/david-stosik/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/active_hash-3.0.0/lib/active_hash/base.rb:174:in `create'
        1: from /Users/david-stosik/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/active_hash-3.0.0/lib/active_hash/base.rb:497:in `save'
/Users/david-stosik/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/active_hash-3.0.0/lib/active_hash/base.rb:135:in `insert': undefined method `length' for nil:NilClass (NoMethodError)

In some scenarios, @records can be nil when checking its length and appending a new record.

This PR adds a test case that reproduces the error above, and fixes it.

My first approach was to simply replace @records = nil with @records = [], but then I figured out I could do better and improve the code base a bit, avoiding in the process duplication of code such as @records || [].

All specs still pass.

Please let me know what you think.

Copy link
Collaborator

@kbrock kbrock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks nice.

consolidates the delete_all details.
not sure that records buys us that much but good enough

could you rebase this to fixup your spec and then we can get this merged

@davidstosik davidstosik force-pushed the fix-empty-json-create branch from d92f283 to 2b5072c Compare October 7, 2020 04:00
@davidstosik
Copy link
Contributor Author

Sorry for the delay, this is rebased now. 👌

The expectation is that the code won't trigger an exception, but it
does:

    ActiveJSON::Base.create does not fail when the loaded JSON was empty
    Failure/Error: Empty.create()
    NoMethodError:
      undefined method `length' for nil:NilClass
    # ./lib/active_hash/base.rb:135:in `insert'
    # ./lib/active_hash/base.rb:497:in `save'
    # ./lib/active_hash/base.rb:174:in `create'
    # ./spec/active_json/base_spec.rb:51:in `block (3 levels) in <top (required)>'
…N datastore

Simple fix would be to replace `@records = nil` with `@records = []`,
but the suggested approach as a better impact on the code base, avoiding
repetitions such as `@records || []`.
@davidstosik davidstosik force-pushed the fix-empty-json-create branch from 2b5072c to 4ccff1a Compare April 6, 2022 08:32
@davidstosik
Copy link
Contributor Author

This still seems to be a problem, so I rebased my branch.

@kbrock
Copy link
Collaborator

kbrock commented Mar 27, 2023

Darn. this is still outstanding. After we merge #268 I'd like to get this in.
Please ping me if I haven't gotten this in by 5/15/2023

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.

2 participants