From d42199e3a2fd98b7443c5a2aee88ad2e0f22cb8f Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Fri, 10 May 2024 12:45:49 +0200 Subject: [PATCH] Drop `multijson` dependency (#385) * Replace Multijson by Grape::Entity::Json Add require spec_helper in rspec file Add spec * Fix rubocop --- .rspec | 1 + grape-entity.gemspec | 2 -- lib/grape_entity/entity.rb | 4 ++-- lib/grape_entity/json.rb | 12 ++++++++++++ spec/grape_entity/entity_spec.rb | 11 ++++++++++- .../nesting_exposure/nested_exposures_spec.rb | 2 -- .../grape_entity/exposure/represent_exposure_spec.rb | 2 -- spec/grape_entity/exposure_spec.rb | 2 -- spec/grape_entity/hash_spec.rb | 2 -- spec/grape_entity/json_spec.rb | 7 +++++++ spec/grape_entity/options_spec.rb | 2 -- 11 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 lib/grape_entity/json.rb create mode 100644 spec/grape_entity/json_spec.rb diff --git a/.rspec b/.rspec index c2e29743..c9ede74d 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,4 @@ +--require spec_helper --color --profile --format documentation diff --git a/grape-entity.gemspec b/grape-entity.gemspec index f6fc2bed..66e0f007 100644 --- a/grape-entity.gemspec +++ b/grape-entity.gemspec @@ -17,8 +17,6 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.add_runtime_dependency 'activesupport', '>= 3.0.0' - # FIXME: remove dependecy - s.add_runtime_dependency 'multi_json', '>= 1.3.2' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec}/*`.split("\n") diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 2728b9fb..796720a8 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'multi_json' +require 'grape_entity/json' module Grape # An Entity is a lightweight structure that allows you to easily @@ -558,7 +558,7 @@ def is_defined_in_entity?(attribute) def to_json(options = {}) options = options.to_h if options&.respond_to?(:to_h) - MultiJson.dump(serializable_hash(options)) + Grape::Entity::Json.dump(serializable_hash(options)) end def to_xml(options = {}) diff --git a/lib/grape_entity/json.rb b/lib/grape_entity/json.rb new file mode 100644 index 00000000..c4f89078 --- /dev/null +++ b/lib/grape_entity/json.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Grape + class Entity + if defined?(::MultiJson) + Json = ::MultiJson + else + Json = ::JSON + Json::ParseError = Json::ParserError + end + end +end diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 466e4d77..4b037706 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' require 'ostruct' describe Grape::Entity do @@ -1755,6 +1754,16 @@ class NoPathCharacterEntity < Grape::Entity end end + describe '#to_json' do + before do + fresh_class.expose :name + end + + it 'returns a json' do + expect(fresh_class.new(model).to_json).to eq(JSON.generate(attributes.slice(:name))) + end + end + describe '#inspect' do before do fresh_class.class_eval do diff --git a/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb b/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb index f5e5ff3a..86e1649d 100644 --- a/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb +++ b/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Grape::Entity::Exposure::NestingExposure::NestedExposures do subject(:nested_exposures) { described_class.new([]) } diff --git a/spec/grape_entity/exposure/represent_exposure_spec.rb b/spec/grape_entity/exposure/represent_exposure_spec.rb index 95847cbf..88c57e9c 100644 --- a/spec/grape_entity/exposure/represent_exposure_spec.rb +++ b/spec/grape_entity/exposure/represent_exposure_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Grape::Entity::Exposure::RepresentExposure do subject(:exposure) { described_class.new(:foo, {}, {}, double, double) } diff --git a/spec/grape_entity/exposure_spec.rb b/spec/grape_entity/exposure_spec.rb index b8d6b252..9528cc90 100644 --- a/spec/grape_entity/exposure_spec.rb +++ b/spec/grape_entity/exposure_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Grape::Entity::Exposure do let(:fresh_class) { Class.new(Grape::Entity) } let(:model) { double(attributes) } diff --git a/spec/grape_entity/hash_spec.rb b/spec/grape_entity/hash_spec.rb index ef21744e..59ce4f83 100644 --- a/spec/grape_entity/hash_spec.rb +++ b/spec/grape_entity/hash_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Grape::Entity do it 'except option for nested entity', :aggregate_failures do module EntitySpec diff --git a/spec/grape_entity/json_spec.rb b/spec/grape_entity/json_spec.rb new file mode 100644 index 00000000..b57825f4 --- /dev/null +++ b/spec/grape_entity/json_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +describe Grape::Entity::Json do + subject { described_class } + + it { is_expected.to eq(JSON) } +end diff --git a/spec/grape_entity/options_spec.rb b/spec/grape_entity/options_spec.rb index 403b45ca..4de31423 100644 --- a/spec/grape_entity/options_spec.rb +++ b/spec/grape_entity/options_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'spec_helper' - describe Grape::Entity::Options do module EntitySpec class Crystalline