Commit bb92989c authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'json-gem-upgrade-fix-3' into 'master'

Cover metadata parsing for json upgrade

See merge request gitlab-org/gitlab!31725
parents 5e49ac9e a5e305c9
...@@ -228,7 +228,11 @@ module Vulnerabilities ...@@ -228,7 +228,11 @@ module Vulnerabilities
def metadata def metadata
strong_memoize(:metadata) do strong_memoize(:metadata) do
Gitlab::Json.parse(raw_metadata) data = Gitlab::Json.parse(raw_metadata)
data = {} unless data.is_a?(Hash)
data
rescue JSON::ParserError rescue JSON::ParserError
{} {}
end end
......
...@@ -606,4 +606,28 @@ describe Vulnerabilities::Occurrence do ...@@ -606,4 +606,28 @@ describe Vulnerabilities::Occurrence do
it { is_expected.to eql(expected_cve) } it { is_expected.to eql(expected_cve) }
end end
describe "#metadata" do
let(:occurrence) { build(:vulnerabilities_occurrence) }
subject { occurrence.metadata }
it "handles bool JSON data" do
allow(occurrence).to receive(:raw_metadata) { "true" }
expect(subject).to eq({})
end
it "handles string JSON data" do
allow(occurrence).to receive(:raw_metadata) { '"test"' }
expect(subject).to eq({})
end
it "parses JSON data" do
allow(occurrence).to receive(:raw_metadata) { '{ "test": true }' }
expect(subject).to eq({ "test" => true })
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