Skip to content

Commit

Permalink
Merge pull request #46535 from nashby/type-json
Browse files Browse the repository at this point in the history
Fix `as_json` call on ActiveModel::Type's child classes.
  • Loading branch information
rafaelfranca committed Nov 28, 2022
2 parents f6b386d + 433bd59 commit 5df0f0d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,3 +1,8 @@
* Raise `NoMethodError` in `ActiveModel::Type::Value#as_json` to avoid unpredictable
results.

*Vasiliy Ermolovich*

* Custom attribute types that inherit from Active Model built-in types and do
not override the `serialize` method will now benefit from an optimization
when serializing attribute values for the database.
Expand Down
4 changes: 4 additions & 0 deletions activemodel/lib/active_model/type/value.rb
Expand Up @@ -135,6 +135,10 @@ def immutable_value(value) # :nodoc:
value
end

def as_json(*)
raise NoMethodError
end

private
# Convenience method for types which do not need separate type casting
# behavior for user and database inputs. Called by Value#cast for
Expand Down
7 changes: 7 additions & 0 deletions activemodel/test/cases/type/value_test.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "cases/helper"
require "active_support/core_ext/object/json"

module ActiveModel
module Type
Expand All @@ -10,6 +11,12 @@ def test_type_equality
assert_not_equal Type::Value.new, Type::Integer.new
assert_not_equal Type::Value.new(precision: 1), Type::Value.new(precision: 2)
end

def test_as_json_not_defined
assert_raises NoMethodError do
Type::Value.new.as_json
end
end
end
end
end

0 comments on commit 5df0f0d

Please sign in to comment.