Skip to content

Commit 85f3333

Browse files
authored
Add condition to not merge arrays with default (#24)
* Add condition to not merge arrays with default * Update node_spec * Use fallback instead of conditional logic * Move default concern up to public get method
1 parent 2d46eff commit 85f3333

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

lib/vrt/mapping.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ def get(id_list, version)
2626
acc[key.to_sym] = get_key(
2727
id_list: id_list,
2828
mapping: mapping,
29-
key: key,
30-
default: default&.try(:[], key)
31-
)
29+
key: key
30+
) || default&.try(:[], key)
3231
end
3332
else
34-
get_key(id_list: id_list, mapping: mapping, key: @scheme, default: default)
33+
get_key(id_list: id_list, mapping: mapping, key: @scheme) || default
3534
end
3635
end
3736

@@ -65,10 +64,10 @@ def key_by_id(mapping)
6564
end
6665
end
6766

68-
def get_key(id_list:, mapping:, key:, default:)
67+
def get_key(id_list:, mapping:, key:)
6968
# iterate through the id components, keeping track of where we are in the mapping file
7069
# and the most specific mapped value found so far
71-
best_guess = default
70+
best_guess = nil
7271
id_list.each do |id|
7372
entry = mapping[id]
7473
break unless entry # mapping file doesn't go this deep, return previous value

spec/vrt/mappings_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@
127127
end
128128
end
129129
end
130+
131+
context 'with arrays as the mapping values' do
132+
subject { described_class.new(:cwe).get(id_list, version) }
133+
let(:version) { '999.999' }
134+
135+
context 'when mapping has a default' do
136+
let(:id_list) { %i[server_security_misconfiguration] }
137+
138+
it 'does NOT include the default mapping value' do
139+
is_expected.to contain_exactly('CWE-933')
140+
end
141+
142+
context 'no mapping exists' do
143+
let(:id_list) { %i[other] }
144+
145+
it 'only includes the default mapping value' do
146+
is_expected.to contain_exactly('CWE-2000')
147+
end
148+
end
149+
end
150+
end
130151
end
131152
end
132153
end

spec/vrt/node_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@
8080
it 'has the exepected (concatenated) CWE IDs' do
8181
expect(mappings[:cwe]).to eq [
8282
'CWE-942',
83-
'CWE-933',
84-
'CWE-2000'
83+
'CWE-933'
8584
]
8685
end
8786
end

0 commit comments

Comments
 (0)