Commit 4783fab6 authored by George Koltsov's avatar George Koltsov

Convert empty strings to nil in ImportExport::BaseObjectBuilder

- BaseObjectBuilder is used to look up existing objects
  like labels during import, instead of creating one
- For example, if a label is used on an epic and a board,
  we do not want to create 2 labels and instead reuse an
  one
- If a label is exported with description: '', and then imported
  back, rails will convert description to be nil
- Later on, when we try to identify a label BaseObjectBuilder
  uses description: '' as one of the where causes and
  nil != '', hence label lookup fails and the relation creation
  fails as well
- This MR converts any empty strings to nil inside BaseObjectBuilder
  so it does not happen anymore
parent 1a623a92
---
title: Fix Group Import existing objects lookup when description attribute is an empty
string
merge_request: 25187
author:
type: fixed
......@@ -18,12 +18,21 @@ module Gitlab
super
@group = @attributes['group']
update_description
end
private
attr_reader :group
# Convert description empty string to nil
# due to existing object being saved with description: nil
# Which makes object lookup to fail since nil != ''
def update_description
attributes['description'] = nil if attributes['description'] == ''
end
def where_clauses
[
where_clause_base,
......
......@@ -26,6 +26,16 @@ describe Gitlab::ImportExport::GroupObjectBuilder do
expect(label.persisted?).to be true
end
context 'when description is an empty string' do
let(:label_attributes) { base_attributes.merge('type' => 'GroupLabel', 'description' => '') }
it 'finds the existing group label' do
group_label = create(:group_label, label_attributes)
expect(described_class.build(Label, label_attributes)).to eq(group_label)
end
end
end
context 'milestones' do
......@@ -41,4 +51,16 @@ describe Gitlab::ImportExport::GroupObjectBuilder do
expect(milestone.persisted?).to be true
end
end
describe '#initialize' do
context 'when attributes contain description as empty string' do
let(:attributes) { base_attributes.merge('description' => '') }
it 'converts empty string to nil' do
builder = described_class.new(Label, attributes)
expect(builder.send(:attributes)).to include({ 'description' => nil })
end
end
end
end
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