Commit edca1543 authored by Robert May's avatar Robert May

Replicate current json gem behaviour in wrapper

This is a transparent change that will affect nothing, but it adds
support for emulating the old behaviour of the `json` gem, which will
be upgraded in the next iteration of this work.
parent 821efd72
......@@ -3,8 +3,13 @@
module Gitlab
module Json
class << self
def parse(*args)
adapter.parse(*args)
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) }
data
end
def parse!(*args)
......@@ -28,6 +33,17 @@ module Gitlab
def adapter
::JSON
end
def parser_error
::JSON::ParserError
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
true
end
end
end
end
......@@ -4,24 +4,52 @@ require "spec_helper"
RSpec.describe Gitlab::Json do
describe ".parse" do
it "parses an object" do
expect(subject.parse('{ "foo": "bar" }')).to eq({ "foo" => "bar" })
end
context "legacy_mode is on by default" do
it "parses an object" do
expect(subject.parse('{ "foo": "bar" }')).to eq({ "foo" => "bar" })
end
it "parses an array" do
expect(subject.parse('[{ "foo": "bar" }]')).to eq([{ "foo" => "bar" }])
end
it "parses an array" do
expect(subject.parse('[{ "foo": "bar" }]')).to eq([{ "foo" => "bar" }])
end
it "raises an error on a string" do
expect { subject.parse('"foo"') }.to raise_error(JSON::ParserError)
end
it "raises an error on a string" do
expect { subject.parse('"foo"') }.to raise_error(JSON::ParserError)
end
it "raises an error on a true bool" do
expect { subject.parse("true") }.to raise_error(JSON::ParserError)
it "raises an error on a true bool" do
expect { subject.parse("true") }.to raise_error(JSON::ParserError)
end
it "raises an error on a false bool" do
expect { subject.parse("false") }.to raise_error(JSON::ParserError)
end
end
it "raises an error on a false bool" do
expect { subject.parse("false") }.to raise_error(JSON::ParserError)
context "legacy_mode is disabled" do
it "parses an object" do
expect(subject.parse('{ "foo": "bar" }', legacy_mode: false)).to eq({ "foo" => "bar" })
end
it "parses an array" do
expect(subject.parse('[{ "foo": "bar" }]', legacy_mode: false)).to eq([{ "foo" => "bar" }])
end
# These are expected errors now until we upgrade the `json` gem,
# and then it will be expected to not raise errors and these tests
# will be updated accordingly.
it "raises an error on a string" do
expect { subject.parse('"foo"') }.to raise_error(JSON::ParserError)
end
it "raises an error on a true bool" do
expect { subject.parse("true") }.to raise_error(JSON::ParserError)
end
it "raises an error on a false bool" do
expect { subject.parse("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