Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource load method dies on Array contains Numeric elements

#2305

Here is sample json which cause problem for ActiveResource load method:


{"id":13679082001,"referenceId":"homepage_main","name":"Main Video Homepage","shortDescription":null,
"videoIds":[13451141001,13460674001,13451144001,13460676001],"thumbnailURL":null,"filterTags":null,"playlistType":"EXPLICIT"}

The main problem is with Array element "video_ids":[13451141001,13460674001,13451144001,13460676001] while processing.
Load method raise exception:
ArgumentError: expected an attributes Hash, got 13451141001 
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:942:in `load'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:655:in `initialize'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:949:in `new'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:949:in `load'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:949:in `map'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:949:in `load'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:944:in `each'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:944:in `load'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:655:in `initialize'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:603:in `new'
	from /Library/Ruby/Gems/1.8/gems/activeresource-2.3.2/lib/active_resource/base.rb:603:in `instantiate_record' 

I think there should be additional case when there are Numeric elements in Array child. I assume json file is in valid format.

Is it a bug ? or am I wrong ? I've added some test to this case and made a patch(attached). It works for me.

Reported by GrzesF · March 20th, 2009 @ 11:50 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. GrzesF
    GrzesF

    Attached is example json file which cause the problem.

    March 20th, 2009 @ 11:51 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from active_resource, load to active_resource, bugmash, load, patch

    August 5th, 2009 @ 03:43 PM

  3. pjammer
    pjammer

    +1 patch verified and works well with the tests. -1 for that json file and using it in the console. I create a mini app, loaded the patch up into it and pasted the contents of that json file into the console and it puked trying to read the hash.

    >> m = {"id":"123445567890","referenceid":"homepage_main","videoIds":[13451141001,13460674001,13451144001,13460676001]}
    SyntaxError: compile error
    (irb):4: odd number list for Hash
    m = {"id":"123445567890","referenceid":"...
              ^
    (irb):4: syntax error, unexpected ':', expecting '}'
    m = {"id":"123445567890","referenceid":"...
              ^
    (irb):4: syntax error, unexpected ',', expecting $end
    m = {"id":"123445567890","referenceid":"homepage_main",...
                             ^
            from (irb):4
    >>
    

    i couldn't test further on the actual merit, but the tests passed using the patch.

    August 9th, 2009 @ 03:14 AM

  4. Elad Meidar
    Elad Meidar

    +1 on patch, applies cleanly on 2-3-stable and master, logic makes sense.

    August 9th, 2009 @ 03:37 AM

  5. Kamal Fariz
    Kamal Fariz

    Just to add, bare numeric elements in an array is legal JSON according to JSONLint.

    August 9th, 2009 @ 03:46 AM

  6. Jatinder Singh
    Jatinder Singh

    +1 verified.

    I came across this defect at work this week, when I was trying to load an ARes from attributes containing a serialized hash column.

    August 9th, 2009 @ 06:50 AM

  7. Pratik
    Pratik

    I don't like

    attrs.dup rescue attrs
    

    It should probably use Object#duplicable?.

    August 9th, 2009 @ 11:18 PM

  8. Elad Meidar
    Elad Meidar

    Attached a fixed patch with #duplicable?, applies to master and 2-3-stable and passes tests

    August 9th, 2009 @ 11:30 PM

  9. Tristan Dunn
    Tristan Dunn

    +1

    Elad's patch applies cleanly to master and 2-3-stable, with passing tests.

    August 9th, 2009 @ 11:37 PM

  10. Repository
    Repository
    • State changed from new to resolved

    (from [59c3b0d0deb8ddd83f20c8a66fde188aed74c51c]) Ensure ActiveResource#load works with numeric arrays [Grzegorz Forysinski, Elad Meidar]

    [#2305 ActiveResource load method dies on Array contains Numeric elements state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com
    http://github.com/rails/rails/commit/59c3b0d0deb8ddd83f20c8a66fde18...

    August 9th, 2009 @ 11:40 PM

  11. Repository
  12. Pratik
    Pratik
    • Tag changed from active_resource, bugmash, load, patch to active_resource, load, patch

    August 9th, 2009 @ 11:52 PM