diff --git a/.rubocop.yml b/.rubocop.yml index d34b133edee9f16066751a883eb5c24dca942b26..19f0b0b294fbd1413d2b01bac590c37706932d08 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -254,7 +254,6 @@ Gitlab/HTTParty: Gitlab/Json: Enabled: true Exclude: - - 'db/**/*' - 'qa/**/*' - 'scripts/**/*' - 'tooling/rspec_flaky/**/*' diff --git a/db/post_migrate/20190517153211_migrate_k8s_service_integration.rb b/db/post_migrate/20190517153211_migrate_k8s_service_integration.rb index 4bd04edb239522f47d979f4aff0831f485c37026..0b409cd28660505ec29226f5c72513c9daf84d89 100644 --- a/db/post_migrate/20190517153211_migrate_k8s_service_integration.rb +++ b/db/post_migrate/20190517153211_migrate_k8s_service_integration.rb @@ -70,7 +70,7 @@ def token private def parsed_properties - @parsed_properties ||= JSON.parse(self.properties) + @parsed_properties ||= JSON.parse(self.properties) # rubocop:disable Gitlab/Json end end diff --git a/ee/spec/lib/gitlab/geo/oauth/session_spec.rb b/ee/spec/lib/gitlab/geo/oauth/session_spec.rb index 5c8378c2653a0d25d5a596692d1f323f2f26fa01..4e8225a87ef5bfa6c78182ddae65e56968e3c50b 100644 --- a/ee/spec/lib/gitlab/geo/oauth/session_spec.rb +++ b/ee/spec/lib/gitlab/geo/oauth/session_spec.rb @@ -49,7 +49,7 @@ def stub_relative_url(host, script_name) describe '#authenticate' do let(:api_url) { "#{primary_node.internal_url.chomp('/')}/api/v4/user" } - let(:user_json) { ActiveSupport::JSON.encode({ id: 555, email: 'user@example.com' }.as_json) } + let(:user_json) { Gitlab::Json.dump({ id: 555, email: 'user@example.com' }.as_json) } context 'on success' do before do @@ -96,7 +96,7 @@ def stub_relative_url(host, script_name) describe '#get_token' do context 'primary is configured with relative URL' do it "makes the request to a primary's relative URL" do - response = ActiveSupport::JSON.encode({ access_token: 'fake-token' }.as_json) + response = Gitlab::Json.dump({ access_token: 'fake-token' }.as_json) primary_node.update!(url: 'http://example.com/gitlab/') api_url = "#{primary_node.internal_url}oauth/token" diff --git a/lib/gitlab/import_export/json/legacy_reader.rb b/lib/gitlab/import_export/json/legacy_reader.rb index 97b34088e3ed9ec89a3819d12c57bd6d4a24b3ba..dc80c92f507f2b328cd4898f49f2a9e881a38ed6 100644 --- a/lib/gitlab/import_export/json/legacy_reader.rb +++ b/lib/gitlab/import_export/json/legacy_reader.rb @@ -27,7 +27,7 @@ def tree_hash end def read_hash - ActiveSupport::JSON.decode(IO.read(@path)) + Gitlab::Json.parse(IO.read(@path)) rescue StandardError => e Gitlab::ErrorTracking.log_exception(e) raise Gitlab::ImportExport::Error, 'Incorrect JSON format' diff --git a/lib/gitlab/import_export/json/ndjson_reader.rb b/lib/gitlab/import_export/json/ndjson_reader.rb index 4899bd3b0ee8b57650d35616afbecbeaea705ecc..510da61d3ab8e987f40a8f418ad6f107dc01d7d1 100644 --- a/lib/gitlab/import_export/json/ndjson_reader.rb +++ b/lib/gitlab/import_export/json/ndjson_reader.rb @@ -47,8 +47,8 @@ def consume_relation(importable_path, key, mark_as_consumed: true) private def json_decode(string) - ActiveSupport::JSON.decode(string) - rescue ActiveSupport::JSON.parse_error => e + Gitlab::Json.parse(string) + rescue JSON::ParserError => e Gitlab::ErrorTracking.log_exception(e) raise Gitlab::ImportExport::Error, 'Incorrect JSON format' end diff --git a/lib/gitlab/import_export/lfs_restorer.rb b/lib/gitlab/import_export/lfs_restorer.rb index d73ae1410a3553457443f8a5816811fa19f490e6..9931b09e9cafaafa1421b3fc87da8748014dc61c 100644 --- a/lib/gitlab/import_export/lfs_restorer.rb +++ b/lib/gitlab/import_export/lfs_restorer.rb @@ -72,7 +72,7 @@ def lfs_json @lfs_json ||= begin json = IO.read(lfs_json_path) - ActiveSupport::JSON.decode(json) + Gitlab::Json.parse(json) rescue StandardError raise Gitlab::ImportExport::Error, 'Incorrect JSON format' end diff --git a/lib/gitlab/json_cache.rb b/lib/gitlab/json_cache.rb index 4314c131adadf388fade2fa143f1dabec61686e3..41c18f82a4b8c61334ea7af6efe2e756d2d8bd67 100644 --- a/lib/gitlab/json_cache.rb +++ b/lib/gitlab/json_cache.rb @@ -58,7 +58,7 @@ def fetch(key, options = {}, &block) private def parse_value(raw, klass) - value = ActiveSupport::JSON.decode(raw.to_s) + value = Gitlab::Json.parse(raw.to_s) case value when Hash then parse_entry(value, klass) @@ -66,7 +66,7 @@ def parse_value(raw, klass) else value end - rescue ActiveSupport::JSON.parse_error + rescue JSON::ParserError nil end diff --git a/rubocop/cop/gitlab/json.rb b/rubocop/cop/gitlab/json.rb index 7cc719aca098b9015db367e8970170a2353bc232..d2ba0012ca0bec1eb2b4c23d73528f2b99c0e1ad 100644 --- a/rubocop/cop/gitlab/json.rb +++ b/rubocop/cop/gitlab/json.rb @@ -10,7 +10,7 @@ class Json < RuboCop::Cop::Cop EOL def_node_matcher :json_node?, <<~PATTERN - (send (const nil? :JSON)...) + (send (const {nil? | (const nil? :ActiveSupport)} :JSON)...) PATTERN def on_send(node) diff --git a/spec/features/projects/import_export/export_file_spec.rb b/spec/features/projects/import_export/export_file_spec.rb index 7f8ded4fa43214bab37ba5f58a131b2321349117..ccf3ccc6a961d1c037c43f5c020af975f3460b22 100644 --- a/spec/features/projects/import_export/export_file_spec.rb +++ b/spec/features/projects/import_export/export_file_spec.rb @@ -82,8 +82,7 @@ relations << Gitlab::Json.parse(IO.read(project_json_path)) Dir.glob(File.join(tmpdir, 'tree/project', '*.ndjson')) do |rb_filename| File.foreach(rb_filename) do |line| - json = ActiveSupport::JSON.decode(line) - relations << json + relations << Gitlab::Json.parse(line) end end diff --git a/spec/lib/gitlab/import_export/group/legacy_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/group/legacy_tree_restorer_spec.rb index bfcd4994995b4b080eb0569a55cc608ae53dd022..dbd6cb243f6ed32869e86fffb44050b1a5e747cd 100644 --- a/spec/lib/gitlab/import_export/group/legacy_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/group/legacy_tree_restorer_spec.rb @@ -77,7 +77,7 @@ let(:group) { create(:group) } let(:shared) { Gitlab::ImportExport::Shared.new(group) } let(:group_tree_restorer) { described_class.new(user: importer_user, shared: shared, group: group, group_hash: nil) } - let(:group_json) { ActiveSupport::JSON.decode(IO.read(File.join(shared.export_path, 'group.json'))) } + let(:group_json) { Gitlab::Json.parse(IO.read(File.join(shared.export_path, 'group.json'))) } shared_examples 'excluded attributes' do excluded_attributes = %w[ diff --git a/spec/lib/gitlab/import_export/group/tree_restorer_spec.rb b/spec/lib/gitlab/import_export/group/tree_restorer_spec.rb index d2153221e8fd2eca59a7dde3bbbaed7dd5f2b0b0..b67d42d1b71fcddbd0157ea7c15b1981fbfcf9e0 100644 --- a/spec/lib/gitlab/import_export/group/tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/group/tree_restorer_spec.rb @@ -111,7 +111,7 @@ let(:shared) { Gitlab::ImportExport::Shared.new(group) } let(:group_tree_restorer) { described_class.new(user: importer_user, shared: shared, group: group) } let(:exported_file) { File.join(shared.export_path, 'tree/groups/4352.json') } - let(:group_json) { ActiveSupport::JSON.decode(IO.read(exported_file)) } + let(:group_json) { Gitlab::Json.parse(IO.read(exported_file)) } shared_examples 'excluded attributes' do excluded_attributes = %w[ diff --git a/spec/lib/gitlab/import_export/import_test_coverage_spec.rb b/spec/lib/gitlab/import_export/import_test_coverage_spec.rb index 9c6d2708607773f3c8d622cb65ba043d00b12321..90966cb491503336487245330fcead9af6f868d9 100644 --- a/spec/lib/gitlab/import_export/import_test_coverage_spec.rb +++ b/spec/lib/gitlab/import_export/import_test_coverage_spec.rb @@ -86,7 +86,7 @@ def tested_relations end def relations_from_json(json_file) - json = ActiveSupport::JSON.decode(IO.read(json_file)) + json = Gitlab::Json.parse(IO.read(json_file)) [].tap {|res| gather_relations({ project: json }, res, [])} .map {|relation_names| relation_names.join('.')} diff --git a/spec/lib/gitlab/json_cache_spec.rb b/spec/lib/gitlab/json_cache_spec.rb index 8265c3449bb717cd51ecf19df7d19b9027e3e81b..7899d01b4759d50d757a72516991fd7ea15bcdf3 100644 --- a/spec/lib/gitlab/json_cache_spec.rb +++ b/spec/lib/gitlab/json_cache_spec.rb @@ -130,7 +130,7 @@ .with(expanded_key) .and_return(nil) - expect(ActiveSupport::JSON).not_to receive(:decode) + expect(Gitlab::Json).not_to receive(:parse) expect(cache.read(key)).to be_nil end @@ -140,7 +140,7 @@ .with(expanded_key) .and_return(true) - expect(ActiveSupport::JSON).to receive(:decode).with("true").and_call_original + expect(Gitlab::Json).to receive(:parse).with("true").and_call_original expect(cache.read(key, BroadcastMessage)).to eq(true) end end @@ -151,7 +151,7 @@ .with(expanded_key) .and_return(false) - expect(ActiveSupport::JSON).to receive(:decode).with("false").and_call_original + expect(Gitlab::Json).to receive(:parse).with("false").and_call_original expect(cache.read(key, BroadcastMessage)).to eq(false) end end diff --git a/spec/requests/git_http_spec.rb b/spec/requests/git_http_spec.rb index 3fb683ea0fa8ebd03523e32dfdf8bd8cba0d874e..2a91dae32b81b29c0410fd91fb13a8ad0138bc96 100644 --- a/spec/requests/git_http_spec.rb +++ b/spec/requests/git_http_spec.rb @@ -267,7 +267,7 @@ it "responds to pulls with the wiki's repo" do download(path) do |response| - json_body = ActiveSupport::JSON.decode(response.body) + json_body = Gitlab::Json.parse(response.body) expect(json_body['Repository']['relative_path']).to eq(wiki.repository.relative_path) end @@ -1584,7 +1584,7 @@ def attempt_login(include_password) it "responds to pulls with the wiki's repo" do download(path) do |response| - json_body = ActiveSupport::JSON.decode(response.body) + json_body = Gitlab::Json.parse(response.body) expect(json_body['Repository']['relative_path']).to eq(wiki.repository.relative_path) end diff --git a/spec/rubocop/cop/gitlab/json_spec.rb b/spec/rubocop/cop/gitlab/json_spec.rb index 66b2c675e80ef6943002d058a36fb2dd7d4d534b..7998f26da4eac1a20aefff8dcce4cb656a4d3392 100644 --- a/spec/rubocop/cop/gitlab/json_spec.rb +++ b/spec/rubocop/cop/gitlab/json_spec.rb @@ -6,7 +6,7 @@ RSpec.describe RuboCop::Cop::Gitlab::Json do subject(:cop) { described_class.new } - context 'when JSON is called' do + context 'when ::JSON is called' do it 'registers an offense' do expect_offense(<<~RUBY) class Foo @@ -18,4 +18,17 @@ def bar RUBY end end + + context 'when ActiveSupport::JSON is called' do + it 'registers an offense' do + expect_offense(<<~RUBY) + class Foo + def bar + ActiveSupport::JSON.parse('{ "foo": "bar" }') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid calling `JSON` directly. [...] + end + end + RUBY + end + end end diff --git a/spec/support/import_export/common_util.rb b/spec/support/import_export/common_util.rb index 5fb6af99b792b64dd4e367c3b85bb4c0e61527c3..1aa20dab6f8c7082556c9ba383610165d67b9593 100644 --- a/spec/support/import_export/common_util.rb +++ b/spec/support/import_export/common_util.rb @@ -83,7 +83,7 @@ def consume_attributes(dir_path, exportable_path) path = File.join(dir_path, "#{exportable_path}.json") return unless File.exist?(path) - ActiveSupport::JSON.decode(IO.read(path)) + Gitlab::Json.parse(IO.read(path)) end def consume_relations(dir_path, exportable_path, key) @@ -93,7 +93,7 @@ def consume_relations(dir_path, exportable_path, key) relations = [] File.foreach(path) do |line| - json = ActiveSupport::JSON.decode(line) + json = Gitlab::Json.parse(line) relations << json end @@ -101,7 +101,7 @@ def consume_relations(dir_path, exportable_path, key) end def project_json(filename) - ActiveSupport::JSON.decode(IO.read(filename)) + Gitlab::Json.parse(IO.read(filename)) end end end