Commit fb229bbf authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '19766-dont-crash-when-no-objects-to-cache' into 'master'

ObjectRenderer doesn't crash when no objects to cache with Rails.cache.read_multi

## What does this MR do?

Avoid calls to Rails.cache.read_multi without cache keys so it doesn't raise an exception

## What are the relevant issue numbers?

Closes #19766

## Does this MR meet the acceptance criteria?

- [ ] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added. I considered is not needed is a fix over a RC
- ~~[ ] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md)~~
- ~~[ ] API support added~~
- Tests
  - [x] Added for this feature/bug
  - [x] All builds are passing
- [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides)
- [x] Branch has no merge conflicts with `master` (if you do - rebase it please)
- [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits)

See merge request !5229
parents ecd301dd cc752f24
...@@ -61,10 +61,16 @@ module Banzai ...@@ -61,10 +61,16 @@ module Banzai
end end
cacheable_items, non_cacheable_items = items_collection.partition { |item| item.key?(:cache_key) } cacheable_items, non_cacheable_items = items_collection.partition { |item| item.key?(:cache_key) }
items_in_cache = Rails.cache.read_multi(*cacheable_items.map { |item| item[:cache_key] })
items_not_in_cache = cacheable_items.reject do |item| items_in_cache = []
item[:rendered] = items_in_cache[item[:cache_key]] items_not_in_cache = []
items_in_cache.key?(item[:cache_key])
unless cacheable_items.empty?
items_in_cache = Rails.cache.read_multi(*cacheable_items.map { |item| item[:cache_key] })
items_not_in_cache = cacheable_items.reject do |item|
item[:rendered] = items_in_cache[item[:cache_key]]
items_in_cache.key?(item[:cache_key])
end
end end
(items_not_in_cache + non_cacheable_items).each do |item| (items_not_in_cache + non_cacheable_items).each do |item|
......
...@@ -109,6 +109,17 @@ describe Banzai::ObjectRenderer do ...@@ -109,6 +109,17 @@ describe Banzai::ObjectRenderer do
expect(docs[1]).to be_an_instance_of(Nokogiri::HTML::DocumentFragment) expect(docs[1]).to be_an_instance_of(Nokogiri::HTML::DocumentFragment)
expect(docs[1].to_html).to eq('<p>bye</p>') expect(docs[1].to_html).to eq('<p>bye</p>')
end end
it 'returns when no objects to render' do
objects = []
renderer = described_class.new(project, user, pipeline: :note)
expect(Banzai).to receive(:cache_collection_render).
with([]).
and_call_original
expect(renderer.render_attributes(objects, :note)).to eq([])
end
end end
describe '#base_context' do describe '#base_context' do
......
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