Commit 50dd6fbf authored by nicolasdular's avatar nicolasdular

Fix encoding issue with enums

Using the result from `build_from_database` and passing it to
the `attributes` from init_with` was not sufficent to decode enums
correctly. Since `init_with` is guaranteed to work with `encode_with`,
this MR uses now `encode_with` instead to fix the encoding issue.
parent b966e0d6
......@@ -80,15 +80,10 @@ module Gitlab
# when the new_record? method incorrectly returns false.
#
# See https://gitlab.com/gitlab-org/gitlab/issues/9903#note_145329964
klass
.allocate
.init_with(
"attributes" => attributes_for(klass, raw),
"new_record" => new_record?(raw, klass)
)
klass.allocate.init_with(encode_for(klass, raw))
end
def attributes_for(klass, raw)
def encode_for(klass, raw)
# We have models that leave out some fields from the JSON export for
# security reasons, e.g. models that include the CacheMarkdownField.
# The ActiveRecord::AttributeSet we build from raw does know about
......@@ -96,7 +91,10 @@ module Gitlab
missing_attributes = (klass.columns.map(&:name) - raw.keys)
missing_attributes.each { |column| raw[column] = nil }
klass.attributes_builder.build_from_database(raw, {})
coder = {}
klass.new(raw).encode_with(coder)
coder["new_record"] = new_record?(raw, klass)
coder
end
def new_record?(raw, klass)
......
......@@ -378,6 +378,12 @@ describe Gitlab::JsonCache do
expect(result).to eq(broadcast_message)
end
it 'decodes enums correctly' do
result = cache.fetch(key, as: BroadcastMessage) { 'block result' }
expect(result.broadcast_type).to eq(broadcast_message.broadcast_type)
end
context 'when the cached value is an instance of ActiveRecord::Base' do
it 'returns a persisted record when id is set' do
backend.write(expanded_key, broadcast_message.to_json)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment