Skip to content

Commit f122c77

Browse files
authored
🧹 Remove MultiJSON, JSON and Yajl (Use Oj instead) (#3785)
* Remove MultiJSON - Use Oj.load and Oj.dump - Oj.load => rescue StandardError - Oj.dump + RestController => mode: :compat - No 'pretty' option anymore * Remove JSON - Use Oj.load and Oj.dump * Remove Yajl - Use Oj.load and Oj.dump * Add Cop that detects the usage of other JSON libraries than Oj
1 parent a4f3e24 commit f122c77

240 files changed

Lines changed: 1738 additions & 1664 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.rubocop_cc.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require:
88
- ./spec/linters/migration/include_string_size.rb
99
- ./spec/linters/migration/require_primary_key.rb
1010
- ./spec/linters/match_requires_with_includes.rb
11+
- ./spec/linters/prefer_oj_over_other_json_libraries.rb
1112

1213
AllCops:
1314
TargetRubyVersion: 3.2
@@ -81,7 +82,7 @@ Rails/CreateTableWithTimestamps: # Exclude for old Migrations
8182
- !ruby/regexp /db/migrations/202([0-2]|30[0-7]).+\.rb$/
8283
- spec/**/*
8384

84-
Rails/HttpPositionalArguments: # Brakes Code for specs as it`s not rails used there it`s a test framework(racktest)
85+
Rails/HttpPositionalArguments: # Breaks Code for specs as it`s not rails used there it`s a test framework(racktest)
8586
Exclude:
8687
- spec/**/*
8788

@@ -546,13 +547,13 @@ Style/SingleLineDoEndBlock:
546547
Enabled: true
547548

548549
### Broken Cops that break code
549-
Lint/ShadowedException: # Brakes "bundle exec rake rubocop" if enabled
550+
Lint/ShadowedException: # Breaks "bundle exec rake rubocop" if enabled
550551
Enabled: false
551-
Sequel/SaveChanges: # Brakes Code
552+
Sequel/SaveChanges: # Breaks Code
552553
Enabled: false
553-
Rails/DynamicFindBy: # Brakes Code
554+
Rails/DynamicFindBy: # Breaks Code
554555
Enabled: false
555-
Rails/FindEach: # Brakes Code
556+
Rails/FindEach: # Breaks Code
556557
Enabled: false
557558
Rails/RedundantActiveRecordAllMethod: # As we use Sequel this breaks code as it matches Sequel functions
558559
Enabled: false

‎Gemfile‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ gem 'loggregator_emitter', '~> 5.0'
1919
gem 'membrane', '~> 1.0'
2020
gem 'mime-types', '~> 3.5'
2121
gem 'mock_redis'
22-
gem 'multi_json'
2322
gem 'multipart-parser'
2423
gem 'netaddr', '>= 2.0.4'
2524
gem 'net-ssh'
@@ -47,7 +46,6 @@ gem 'talentbox-delayed_job_sequel', '~> 4.3.0'
4746
gem 'thin'
4847
gem 'unf'
4948
gem 'vmstat', '~> 2.3'
50-
gem 'yajl-ruby'
5149

5250
# Rails Components
5351
gem 'actionpack', '~> 7.1.0'

‎Gemfile.lock‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ DEPENDENCIES
628628
membrane (~> 1.0)
629629
mime-types (~> 3.5)
630630
mock_redis
631-
multi_json
632631
multipart-parser
633632
mysql2 (~> 0.5.6)
634633
net-ssh
@@ -681,7 +680,6 @@ DEPENDENCIES
681680
vcap-concurrency!
682681
vmstat (~> 2.3)
683682
webmock (> 2.3.1)
684-
yajl-ruby
685683

686684
BUNDLED WITH
687685
2.4.10

‎app/controllers/base/base_controller.rb‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,15 @@ def current_user_email
203203
end
204204

205205
def parse_and_validate_json(body)
206-
parsed = body && MultiJson.load(body)
207-
raise MultiJson::ParseError.new('invalid request body') unless parsed.is_a?(Hash)
206+
begin
207+
parsed = Oj.load(body) unless body.nil?
208+
rescue StandardError => e
209+
bad_request!(e.message)
210+
end
211+
212+
bad_request!('invalid request body') unless parsed.is_a?(Hash)
208213

209214
parsed
210-
rescue MultiJson::ParseError => e
211-
bad_request!(e.message)
212215
end
213216

214217
def bad_request!(message)

‎app/controllers/internal/app_crashed_controller.rb‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,11 @@ def crashed(lrp_process_guid)
2626
private
2727

2828
def crashed_request
29-
crashed = {}
30-
begin
31-
payload = body.read
32-
crashed = MultiJson.load(payload)
33-
rescue MultiJson::ParseError => e
34-
logger.error('diego.app_crashed.parse-error', payload: payload, error: e.to_s)
35-
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
36-
end
37-
38-
crashed
29+
payload = body.read
30+
Oj.load(payload)
31+
rescue StandardError => e
32+
logger.error('diego.app_crashed.parse-error', payload: payload, error: e.to_s)
33+
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
3934
end
4035
end
4136
end

‎app/controllers/internal/app_readiness_changed_controller.rb‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@ def readiness_changed(process_guid)
2525
private
2626

2727
def readiness_request
28-
readiness = {}
29-
begin
30-
payload = body.read
31-
readiness = MultiJson.load(payload)
32-
rescue MultiJson::ParseError => e
33-
logger.error('diego.app_readiness_changed.parse-error', payload: payload, error: e.to_s)
34-
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
35-
end
36-
37-
readiness
28+
payload = body.read
29+
Oj.load(payload)
30+
rescue StandardError => e
31+
logger.error('diego.app_readiness_changed.parse-error', payload: payload, error: e.to_s)
32+
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
3833
end
3934
end
4035
end

‎app/controllers/internal/app_rescheduling_controller.rb‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@ def rescheduling(process_guid)
2525
private
2626

2727
def rescheduling_request
28-
rescheduling = {}
29-
begin
30-
payload = body.read
31-
rescheduling = MultiJson.load(payload)
32-
rescue MultiJson::ParseError => e
33-
logger.error('diego.app_rescheduling.parse-error', payload: payload, error: e.to_s)
34-
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
35-
end
36-
37-
rescheduling
28+
payload = body.read
29+
Oj.load(payload)
30+
rescue StandardError => e
31+
logger.error('diego.app_rescheduling.parse-error', payload: payload, error: e.to_s)
32+
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
3833
end
3934
end
4035
end

‎app/controllers/internal/app_security_group_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class AppSecurityGroupController < RestController::BaseController
33
allow_unauthenticated_access
44
get '/internal/v4/asg_latest_update', :return_asg_latest_update
55
def return_asg_latest_update
6-
[HTTP::OK, MultiJson.dump({ last_update: AsgLatestUpdate.last_update.to_datetime.rfc3339(9) }, pretty: true)]
6+
[HTTP::OK, Oj.dump({ last_update: AsgLatestUpdate.last_update.to_datetime.rfc3339(9) }, mode: :compat)]
77
end
88
end
99
end

‎app/controllers/internal/packages_controller.rb‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ class PackagesController < RestController::BaseController
88

99
patch '/internal/v4/packages/:guid', :update
1010
def update(guid)
11-
payload = MultiJson.load(body)
11+
begin
12+
payload = Oj.load(body)
13+
rescue StandardError => e
14+
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', e.message)
15+
end
16+
1217
message = ::VCAP::CloudController::InternalPackageUpdateMessage.new(payload)
1318
unprocessable!(message.errors.full_messages) unless message.valid?
1419

@@ -20,8 +25,6 @@ def update(guid)
2025
HTTP::NO_CONTENT
2126
rescue InternalPackageUpdate::InvalidPackage => e
2227
unprocessable!(e.message)
23-
rescue MultiJson::ParseError => e
24-
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', e.message)
2528
end
2629

2730
private

‎app/controllers/internal/staging_completion_controller.rb‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def parse_bbs_task_callback(staging_response)
9494
logger.error('diego.staging.completion-controller.staging-failed', staging_response)
9595
result[:error] = Diego::FailureReasonSanitizer.sanitize(staging_response[:failure_reason])
9696
else
97-
result[:result] = MultiJson.load(staging_response[:result], symbolize_keys: true)
97+
result[:result] = Oj.load(staging_response[:result], symbol_keys: true)
9898
end
9999
result
100100
end
@@ -121,16 +121,11 @@ def prometheus_updater
121121
attr_reader :stagers
122122

123123
def read_body
124-
staging_response = {}
125-
begin
126-
payload = body.read
127-
staging_response = MultiJson.load(payload, symbolize_keys: true)
128-
rescue MultiJson::ParseError => e
129-
logger.error('diego.staging.parse-error', payload: payload, error: e.to_s)
130-
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
131-
end
132-
133-
staging_response
124+
payload = body.read
125+
Oj.load(payload, symbol_keys: true)
126+
rescue StandardError => e
127+
logger.error('diego.staging.parse-error', payload: payload, error: e.to_s)
128+
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload)
134129
end
135130
end
136131
end

0 commit comments

Comments
 (0)