Commit b02a0313 authored by Robert May's avatar Robert May

Some improvements to json wrapper

parent edca1543
......@@ -2,12 +2,14 @@
module Gitlab
module Json
INVALID_LEGACY_TYPES = [String, TrueClass, FalseClass].freeze
class << self
def parse(string, *args, **named_args)
legacy_mode = legacy_mode_enabled?(named_args.delete(:legacy_mode))
data = adapter.parse(string, *args, **named_args)
raise parser_error if legacy_mode && [String, TrueClass, FalseClass].any? { |type| data.is_a?(type) }
raise parser_error if legacy_mode && INVALID_LEGACY_TYPES.any? { |type| data.is_a?(type) }
data
end
......@@ -39,11 +41,12 @@ module Gitlab
end
def legacy_mode_enabled?(arg_value)
# This will change to the following once the `json` gem is upgraded:
# arg_value.nil? ? true : arg_value
if ::JSON::VERSION_MAJOR >= 2
arg_value.nil? ? true : arg_value
else
true
end
end
end
end
end
......@@ -40,15 +40,15 @@ RSpec.describe Gitlab::Json do
# will be updated accordingly.
it "raises an error on a string" do
expect { subject.parse('"foo"') }.to raise_error(JSON::ParserError)
expect { subject.parse('"foo"', legacy_mode: false) }.to raise_error(JSON::ParserError)
end
it "raises an error on a true bool" do
expect { subject.parse("true") }.to raise_error(JSON::ParserError)
expect { subject.parse("true", legacy_mode: false) }.to raise_error(JSON::ParserError)
end
it "raises an error on a false bool" do
expect { subject.parse("false") }.to raise_error(JSON::ParserError)
expect { subject.parse("false", legacy_mode: false) }.to raise_error(JSON::ParserError)
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