Commit a0686586 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'nicolasdular/fix-json-cache-issue' into 'master'

Fix encoding issue with JSON Cache

See merge request gitlab-org/gitlab!21407
parents ecb6b73b 50dd6fbf
......@@ -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