2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/contact"
|
|
|
|
require "active_support/core_ext/object/instance_variables"
|
2009-06-17 22:27:36 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class JsonSerializationTest < ActiveModel::TestCase
|
2009-06-17 22:27:36 -04:00
|
|
|
def setup
|
|
|
|
@contact = Contact.new
|
2016-08-06 12:38:23 -04:00
|
|
|
@contact.name = "Konata Izumi"
|
2009-06-17 22:27:36 -04:00
|
|
|
@contact.age = 16
|
|
|
|
@contact.created_at = Time.utc(2006, 8, 1)
|
|
|
|
@contact.awesome = true
|
2016-08-06 12:38:23 -04:00
|
|
|
@contact.preferences = { "shows" => "anime" }
|
2009-06-17 22:27:36 -04:00
|
|
|
end
|
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
test "should not include root in json (class method)" do
|
2010-06-07 19:02:45 -04:00
|
|
|
json = @contact.to_json
|
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
assert_no_match %r{^\{"contact":\{}, json
|
2010-06-07 19:02:45 -04:00
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
|
2010-06-07 19:02:45 -04:00
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
|
|
end
|
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
test "should include root in json if include_root_in_json is true" do
|
2018-12-20 12:44:01 -05:00
|
|
|
original_include_root_in_json = Contact.include_root_in_json
|
|
|
|
Contact.include_root_in_json = true
|
|
|
|
json = @contact.to_json
|
|
|
|
|
|
|
|
assert_match %r{^\{"contact":\{}, json
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
|
|
|
assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
|
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
|
|
ensure
|
|
|
|
Contact.include_root_in_json = original_include_root_in_json
|
2011-09-22 17:05:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should include root in json (option) even if the default is set to false" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
json = @contact.to_json(root: true)
|
2014-03-10 09:22:22 -04:00
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
assert_match %r{^\{"contact":\{}, json
|
2010-05-01 00:02:11 -04:00
|
|
|
end
|
|
|
|
|
2011-07-01 22:48:11 -04:00
|
|
|
test "should not include root in json (option)" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
json = @contact.to_json(root: false)
|
2011-07-01 22:48:11 -04:00
|
|
|
|
|
|
|
assert_no_match %r{^\{"contact":\{}, json
|
|
|
|
end
|
|
|
|
|
2010-05-01 00:02:11 -04:00
|
|
|
test "should include custom root in json" do
|
2016-08-06 12:38:23 -04:00
|
|
|
json = @contact.to_json(root: "json_contact")
|
2010-05-01 00:02:11 -04:00
|
|
|
|
2010-06-07 19:02:45 -04:00
|
|
|
assert_match %r{^\{"json_contact":\{}, json
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
|
2010-06-07 19:02:45 -04:00
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
2009-06-17 22:27:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should encode all encodable attributes" do
|
|
|
|
json = @contact.to_json
|
|
|
|
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
|
2009-06-17 22:27:36 -04:00
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should allow attribute filtering with only" do
|
2013-05-01 20:10:06 -04:00
|
|
|
json = @contact.to_json(only: [:name, :age])
|
2009-06-17 22:27:36 -04:00
|
|
|
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
|
|
|
assert_no_match %r{"awesome":true}, json
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
|
2009-06-17 22:27:36 -04:00
|
|
|
assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should allow attribute filtering with except" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
json = @contact.to_json(except: [:name, :age])
|
2009-06-17 22:27:36 -04:00
|
|
|
|
|
|
|
assert_no_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_no_match %r{"age":16}, json
|
|
|
|
assert_match %r{"awesome":true}, json
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
|
2009-06-17 22:27:36 -04:00
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
|
|
|
end
|
2009-06-28 23:12:10 -04:00
|
|
|
|
2010-07-30 16:47:26 -04:00
|
|
|
test "methods are called on object" do
|
2009-06-28 23:12:10 -04:00
|
|
|
# Define methods on fixture.
|
|
|
|
def @contact.label; "Has cheezburger"; end
|
|
|
|
def @contact.favorite_quote; "Constraints are liberating"; end
|
|
|
|
|
|
|
|
# Single method.
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
assert_match %r{"label":"Has cheezburger"}, @contact.to_json(only: :name, methods: :label)
|
2009-06-28 23:12:10 -04:00
|
|
|
|
|
|
|
# Both methods.
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
methods_json = @contact.to_json(only: :name, methods: [:label, :favorite_quote])
|
2009-06-28 23:12:10 -04:00
|
|
|
assert_match %r{"label":"Has cheezburger"}, methods_json
|
|
|
|
assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
|
|
|
|
end
|
2010-07-30 16:47:26 -04:00
|
|
|
|
2012-02-07 13:53:03 -05:00
|
|
|
test "should return Hash for errors" do
|
2010-11-28 08:36:40 -05:00
|
|
|
contact = Contact.new
|
|
|
|
contact.errors.add :name, "can't be blank"
|
|
|
|
contact.errors.add :name, "is too short (minimum is 2 characters)"
|
|
|
|
contact.errors.add :age, "must be 16 or over"
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2012-02-07 13:53:03 -05:00
|
|
|
hash = {}
|
2010-11-28 08:36:40 -05:00
|
|
|
hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"]
|
|
|
|
hash[:age] = ["must be 16 or over"]
|
|
|
|
assert_equal hash.to_json, contact.errors.to_json
|
2010-07-30 16:47:26 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-08-29 10:10:31 -04:00
|
|
|
test "serializable_hash should not modify options passed in argument" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
options = { except: :name }
|
2010-08-29 10:10:31 -04:00
|
|
|
@contact.serializable_hash(options)
|
|
|
|
|
|
|
|
assert_nil options[:only]
|
|
|
|
assert_equal :name, options[:except]
|
|
|
|
end
|
|
|
|
|
2017-12-19 08:15:55 -05:00
|
|
|
test "as_json should serialize timestamps" do
|
|
|
|
assert_equal "2006-08-01T00:00:00.000Z", @contact.as_json["created_at"]
|
|
|
|
end
|
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
test "as_json should return a hash if include_root_in_json is true" do
|
2018-12-20 12:44:01 -05:00
|
|
|
original_include_root_in_json = Contact.include_root_in_json
|
|
|
|
Contact.include_root_in_json = true
|
|
|
|
json = @contact.as_json
|
|
|
|
|
|
|
|
assert_kind_of Hash, json
|
|
|
|
assert_kind_of Hash, json["contact"]
|
|
|
|
%w(name age created_at awesome preferences).each do |field|
|
2020-10-02 00:48:33 -04:00
|
|
|
assert_equal @contact.public_send(field).as_json, json["contact"][field]
|
2010-08-29 10:10:31 -04:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
ensure
|
|
|
|
Contact.include_root_in_json = original_include_root_in_json
|
2010-08-29 10:10:31 -04:00
|
|
|
end
|
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
test "from_json should work without a root (class attribute)" do
|
2011-07-01 22:48:11 -04:00
|
|
|
json = @contact.to_json
|
|
|
|
result = Contact.new.from_json(json)
|
|
|
|
|
|
|
|
assert_equal result.name, @contact.name
|
|
|
|
assert_equal result.age, @contact.age
|
|
|
|
assert_equal Time.parse(result.created_at), @contact.created_at
|
|
|
|
assert_equal result.awesome, @contact.awesome
|
|
|
|
assert_equal result.preferences, @contact.preferences
|
|
|
|
end
|
|
|
|
|
|
|
|
test "from_json should work without a root (method parameter)" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
json = @contact.to_json
|
2011-07-01 22:48:11 -04:00
|
|
|
result = Contact.new.from_json(json, false)
|
|
|
|
|
|
|
|
assert_equal result.name, @contact.name
|
|
|
|
assert_equal result.age, @contact.age
|
|
|
|
assert_equal Time.parse(result.created_at), @contact.created_at
|
|
|
|
assert_equal result.awesome, @contact.awesome
|
|
|
|
assert_equal result.preferences, @contact.preferences
|
|
|
|
end
|
|
|
|
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
test "from_json should work with a root (method parameter)" do
|
|
|
|
json = @contact.to_json(root: :true)
|
|
|
|
result = Contact.new.from_json(json, true)
|
|
|
|
|
|
|
|
assert_equal result.name, @contact.name
|
|
|
|
assert_equal result.age, @contact.age
|
|
|
|
assert_equal Time.parse(result.created_at), @contact.created_at
|
|
|
|
assert_equal result.awesome, @contact.awesome
|
|
|
|
assert_equal result.preferences, @contact.preferences
|
2011-07-01 22:48:11 -04:00
|
|
|
end
|
|
|
|
|
2010-08-29 10:10:31 -04:00
|
|
|
test "custom as_json should be honored when generating json" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
def @contact.as_json(options); { name: name, created_at: created_at }; end
|
2010-08-29 10:10:31 -04:00
|
|
|
json = @contact.to_json
|
|
|
|
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json
|
|
|
|
assert_no_match %r{"awesome":}, json
|
|
|
|
assert_no_match %r{"preferences":}, json
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2013-09-05 04:51:57 -04:00
|
|
|
test "custom as_json options should be extensible" do
|
change AMS::JSON.include_root_in_json default value to false
Changes:
* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.
Problem:
It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> true
# different default values for include_root_in_json
Proposal:
Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.
class User < ActiveRecord::Base; end
class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON
attr_accessor :name, :age
def attributes
instance_values
end
end
user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included
person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included
ActiveRecord::Base.include_root_in_json
=> false
Person.include_root_in_json
=> false
# same behaviour, more consistent
Fixes #6578.
2012-06-06 02:11:39 -04:00
|
|
|
def @contact.as_json(options = {}); super(options.merge(only: [:name])); end
|
2011-09-29 21:07:19 -04:00
|
|
|
json = @contact.to_json
|
|
|
|
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json
|
|
|
|
assert_no_match %r{"awesome":}, json
|
|
|
|
assert_no_match %r{"preferences":}, json
|
|
|
|
end
|
2015-02-23 20:45:46 -05:00
|
|
|
|
|
|
|
test "Class.model_name should be json encodable" do
|
|
|
|
assert_match %r{"Contact"}, Contact.model_name.to_json
|
|
|
|
end
|
2009-06-17 22:27:36 -04:00
|
|
|
end
|