Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix: ActiveRecord::Store TypeError conversion when using YAML coder #13593

Merged
merged 1 commit into from

3 participants

Tháles de Oliveira Yves Senn Lauro Caetano
Tháles de Oliveira

As on PR #13575, after GitHub trolling us hehe (: cc @senny

Closes #13570

activerecord/test/cases/store_test.rb
@@ -162,4 +162,8 @@ class StoreTest < ActiveRecord::TestCase
assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
+
+ test "using YAML coder with a store must not return nil" do
+ assert_not_nil @john.params
Yves Senn Owner
senny added a note

can't we assert on what it should return?

Well observed, will do (:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Tháles de Oliveira

@senny There it is! (:

activerecord/test/cases/store_test.rb
@@ -162,4 +162,11 @@ class StoreTest < ActiveRecord::TestCase
assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
+
+ test "correctly store YAML coder" do
+ assert_equal Hash.new, @john.params
+
Yves Senn Owner
senny added a note

can we also add an assertion before the assignment? This will illustrate the initial state:

assert_equal ..., @john.params

I thought that this exact line was doing that (assert_equal Hash.new, @john.params)!
Could you explain? Sorry for not getting it yet hehe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/test/cases/store_test.rb
@@ -162,4 +162,11 @@ class StoreTest < ActiveRecord::TestCase
assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
+
+ test "correctly store YAML coder" do
Yves Senn Owner
senny added a note

just a nitpick but we don't actually store the coder. The coder is used to serialize the data. So maybe we can reword this to:

store works with YAML coder

Hmmm, nice! Thanks for the clarification man :D Will do it!

Yves Senn Owner
senny added a note

if we are only going to verify that it works with nil without the saves, we should name the test accordingly:

YAML coder initializes the store when a Nil value is given

Done it! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/test/cases/store_test.rb
@@ -162,4 +162,11 @@ class StoreTest < ActiveRecord::TestCase
assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
+
+ test "correctly store YAML coder" do
+ assert_equal Hash.new, @john.params
+
+ @john.params['color'] = 'blue'
+ assert_equal @john.params['color'], 'blue'
Yves Senn Owner
senny added a note

as there were no other YAML tests we should also make a DB roundtrip and verify that deserialization using YAML works as expected. Maybe even save twice.

1.) with blank state
2.) with the color assignment

I see! But isn't the default behaviour (that is, when no coder is specified) to use YAML coder?

As on lines 169~173 of store.rb:

if coder_or_class_name.respond_to?(:load) && coder_or_class_name.respond_to?(:dump)
coder_or_class_name
else
ActiveRecord::Coders::YAMLColumn.new(coder_or_class_name || Object)
end

Please correct me if I'm wrong (:

Yves Senn Owner
senny added a note

that's true. Does this problem only happen when you specify the coder explicitly (eg: coder: YAML) or also when you don't specify a coder at all? If it happens always there is no need to define a new store called params we could simply use one of the existing ones (like settings), no?

Yeah, I considered using settings too (: But with some local tests, I've found that the error only happened when the YAML coder was specified manually, as in the original issue. So I added the new store 'params'. Is there a better alternative?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/test/cases/store_test.rb
@@ -162,4 +162,8 @@ class StoreTest < ActiveRecord::TestCase
assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
+
+ test "YAML coder initializes the store when a Nil value is given" do
+ assert_equal Hash.new, @john.params
Yves Senn Owner
senny added a note

Don't like Hash.new let's use assert_equal({}, @john.params)

Perfect, was in doubt which one to use, thanks for clarifying :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Fixed ActiveRecord::Store nil conversion TypeError when using YAML coder.
+ In case the YAML passed as paramter is nil, uses an empty string
+
+ Fixes #13570
Yves Senn Owner
senny added a note

Just a nitpick: can you add a . after Fixes XXX.. It should look like:

Fixes #13570.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Yves Senn
Owner

@oliveiraethales thanks for sticking with me. I added some final comments but this is looking good.

Tháles de Oliveira

No, thank you for the patience! :D Awesome to work with guys like you (:

I'll make the improvements, let me know anything else that can be improved and when to squash.

Yves Senn
Owner

@oliveiraethales please squash after you've made the changes.

activerecord/test/cases/store_test.rb
((8 lines not shown))
end
+
Yves Senn Owner
senny added a note

let's keep the newline at the end of this file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Fixed ActiveRecord::Store nil conversion TypeError when using YAML coder.
+ In case the YAML passed as paramter is nil, uses an empty string
Yves Senn Owner
senny added a note

also missing a dot at the end of the sentence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Yves Senn
Owner

@oliveiraethales two tiny bits more. you can simply commit --amend them and then force push again.

Tháles de Oliveira oliveiraethales Fix: ActiveRecord::Store TypeError conversion when using YAML coder
Renaming the test accordingly to its behaviour

Adding 'Fixes' statement to changelog

Improving tests legibility & changelog

Undoing mistakenly removed empty line & further improving changelog
901a0c8
Tháles de Oliveira

Done :3 Next time will use --amend, I was doing the 'old' way, pushing and then rebasing

Yves Senn senny merged commit f2b80a4 into from
Yves Senn
Owner

@oliveiraethales thank you for your contribution :yellow_heart:

Lauro Caetano
Collaborator

:yellow_heart: :green_heart:

Tháles de Oliveira

Thanks a LOT @senny! <3 <3 :heart: :heart_eyes_cat:
Looking forward to the next one :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 6, 2014
  1. Tháles de Oliveira

    Fix: ActiveRecord::Store TypeError conversion when using YAML coder

    oliveiraethales authored
    Renaming the test accordingly to its behaviour
    
    Adding 'Fixes' statement to changelog
    
    Improving tests legibility & changelog
    
    Undoing mistakenly removed empty line & further improving changelog
This page is out of date. Refresh to see the latest.
7 activerecord/CHANGELOG.md
View
@@ -1,3 +1,10 @@
+* Fixed ActiveRecord::Store nil conversion TypeError when using YAML coder.
+ In case the YAML passed as paramter is nil, uses an empty string.
+
+ Fixes #13570.
+
+ *Thales Oliveira*
+
* Deprecate unused `ActiveRecord::Base.symbolized_base_class`
and `ActiveRecord::Base.symbolized_sti_name` without replacement.
2  activerecord/lib/active_record/store.rb
View
@@ -178,7 +178,7 @@ def dump(obj)
end
def load(yaml)
- self.class.as_indifferent_hash(@coder.load(yaml))
+ self.class.as_indifferent_hash(@coder.load(yaml || ''))
end
def self.as_indifferent_hash(obj)
4 activerecord/test/cases/store_test.rb
View
@@ -162,4 +162,8 @@ class StoreTest < ActiveRecord::TestCase
assert_equal [:color], first_model.stored_attributes[:data]
assert_equal [:width, :height], second_model.stored_attributes[:data]
end
+
+ test "YAML coder initializes the store when a Nil value is given" do
+ assert_equal({}, @john.params)
+ end
end
1  activerecord/test/models/admin/user.rb
View
@@ -14,6 +14,7 @@ def load(s)
end
belongs_to :account
+ store :params, accessors: [ :color ], coder: YAML
store :settings, :accessors => [ :color, :homepage ]
store_accessor :settings, :favorite_food
store :preferences, :accessors => [ :remember_login ]
1  activerecord/test/schema/schema.rb
View
@@ -45,6 +45,7 @@ def create_table(*args, &block)
t.string :preferences, null: true, default: '', limit: 1024
t.string :json_data, null: true, limit: 1024
t.string :json_data_empty, null: true, default: "", limit: 1024
+ t.text :params
t.references :account
end
Something went wrong with that request. Please try again.