jsonapi-serializer 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 017a78c672880bd4e5c71443b4d89e6755c2819c2e255fe0fabc634b2eb03b6f
4
- data.tar.gz: 849735cbc53db74e8fe00b0b6e35372ee63402c04e8a74df9693d81ab4d10fee
3
+ metadata.gz: 26d8c5dcdd61fbf82798d95753f56f0ca147e9a78d865b6ccd4880fa3dd9c356
4
+ data.tar.gz: bc4d791913f8085d97ae57602175f803e322213c8f97bc8793925a33d1e314d2
5
5
  SHA512:
6
- metadata.gz: 35f5960d308078c06bdbdd9d4bd0b735ae8d409f4637b595cb3e1a51b53fea1aaaa37bc7ed871fcdff46429463a0644b30d0626156ab39894e1a0b4fe6dadf16
7
- data.tar.gz: 70d5947d310735e7faf1839db897fc7227af7b7407b185fa30ec9622214f78600b86bb419c1c9c16c838d6da5606c6104b566d1bbe6d86817e3f30b70301c647
6
+ metadata.gz: ff64f90a94a1bbdc8beecb60997c325cdbfb6d14b48f01a7f4b6e707de4fb338a2cbfa45bae5ff272988fcff2dbe7a746e2ea00e8e3a422e843c844479b836f0
7
+ data.tar.gz: 1b161b48c481dd1e8b9bfdf3c269770cbcd8fd7d6a9b823719b0b05ce1a676cae98ed2d2d377ca95dd8d27a278ab735f64d8b28ce73f825fa40ca74d9f851e44
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # JSON:API Serialization Library
2
2
 
3
+ ## :warning: :construction: [At the moment, contributions are welcome only for v3](https://github.com/jsonapi-serializer/jsonapi-serializer/pull/141)! :construction: :warning:
4
+
3
5
  A fast [JSON:API](https://jsonapi.org/) serializer for Ruby Objects.
4
6
 
5
7
  Previously this project was called **fast_jsonapi**, we forked the project
@@ -39,6 +41,8 @@ article in the `docs` folder for any questions related to methodology.
39
41
  * [Sparse Fieldsets](#sparse-fieldsets)
40
42
  * [Using helper methods](#using-helper-methods)
41
43
  * [Performance Instrumentation](#performance-instrumentation)
44
+ * [Deserialization](#deserialization)
45
+ * [Migrating from Netflix/fast_jsonapi](#migrating-from-netflixfast_jsonapi)
42
46
  * [Contributing](#contributing)
43
47
 
44
48
 
@@ -458,7 +462,7 @@ class MovieSerializer
458
462
 
459
463
  belongs_to :primary_agent do |movie, params|
460
464
  # in here, params is a hash containing the `:current_user` key
461
- params[:current_user].is_employee? ? true : false
465
+ params[:current_user]
462
466
  end
463
467
  end
464
468
 
@@ -693,6 +697,61 @@ tests. To run tests use the following command:
693
697
  rspec
694
698
  ```
695
699
 
700
+ ## Deserialization
701
+ We currently do not support deserialization, but we recommend to use any of the next gems:
702
+
703
+ ### [JSONAPI.rb](https://github.com/stas/jsonapi.rb)
704
+
705
+ This gem provides the next features alongside deserialization:
706
+ - Collection meta
707
+ - Error handling
708
+ - Includes and sparse fields
709
+ - Filtering and sorting
710
+ - Pagination
711
+
712
+ ## Migrating from Netflix/fast_jsonapi
713
+
714
+ If you come from [Netflix/fast_jsonapi](https://github.com/Netflix/fast_jsonapi), here is the instructions to switch.
715
+
716
+ ### Modify your Gemfile
717
+
718
+ ```diff
719
+ - gem 'fast_jsonapi'
720
+ + gem 'jsonapi-serializer'
721
+ ```
722
+
723
+ ### Replace all constant references
724
+
725
+ ```diff
726
+ class MovieSerializer
727
+ - include FastJsonapi::ObjectSerializer
728
+ + include JSONAPI::Serializer
729
+ end
730
+ ```
731
+
732
+ ### Replace removed methods
733
+
734
+ ```diff
735
+ - json_string = MovieSerializer.new(movie).serialized_json
736
+ + json_string = MovieSerializer.new(movie).serializable_hash.to_json
737
+ ```
738
+
739
+ ### Replace require references
740
+
741
+ ```diff
742
+ - require 'fast_jsonapi'
743
+ + require 'jsonapi/serializer'
744
+ ```
745
+
746
+ ### Update your cache options
747
+
748
+ See [docs](https://github.com/jsonapi-serializer/jsonapi-serializer#caching).
749
+
750
+ ```diff
751
+ - cache_options enabled: true, cache_length: 12.hours
752
+ + cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
753
+ ```
754
+
696
755
  ## Contributing
697
756
 
698
757
  Please follow the instructions we provide as part of the issue and
data/lib/fast_jsonapi.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'jsonapi/serializer/errors'
4
+
3
5
  module FastJsonapi
4
6
  require 'fast_jsonapi/object_serializer'
5
7
  if defined?(::Rails)
@@ -6,7 +6,16 @@ module FastJsonapi
6
6
  # @param [Array<Object>] *params any number of parameters to be passed to the Proc
7
7
  # @return [Object] the result of the Proc call with the supplied parameters
8
8
  def call_proc(proc, *params)
9
- proc.call(*params.take(proc.parameters.length))
9
+ # The parameters array for a lambda created from a symbol (&:foo) differs
10
+ # from explictly defined procs/lambdas, so we can't deduce the number of
11
+ # parameters from the array length (and differs between Ruby 2.x and 3).
12
+ # In the case of negative arity -- unlimited/unknown argument count --
13
+ # just send the object to act as the method receiver.
14
+ if proc.arity.negative?
15
+ proc.call(params.first)
16
+ else
17
+ proc.call(*params.take(proc.parameters.length))
18
+ end
10
19
  end
11
20
  end
12
21
  end
@@ -228,10 +228,10 @@ module FastJsonapi
228
228
 
229
229
  # TODO: Remove this undocumented option.
230
230
  # Delegate the caching to the serializer exclusively.
231
- if !relationship.cached
232
- uncachable_relationships_to_serialize[relationship.name] = relationship
233
- else
231
+ if relationship.cached
234
232
  cachable_relationships_to_serialize[relationship.name] = relationship
233
+ else
234
+ uncachable_relationships_to_serialize[relationship.name] = relationship
235
235
  end
236
236
  relationships_to_serialize[relationship.name] = relationship
237
237
  end
@@ -302,7 +302,7 @@ module FastJsonapi
302
302
 
303
303
  def serializer_for(name)
304
304
  namespace = self.name.gsub(/()?\w+Serializer$/, '')
305
- serializer_name = name.to_s.demodulize.classify + 'Serializer'
305
+ serializer_name = "#{name.to_s.demodulize.classify}Serializer"
306
306
  serializer_class_name = namespace + serializer_name
307
307
  begin
308
308
  serializer_class_name.constantize
@@ -340,9 +340,9 @@ module FastJsonapi
340
340
  def validate_includes!(includes)
341
341
  return if includes.blank?
342
342
 
343
- parse_includes_list(includes).keys.each do |include_item|
343
+ parse_includes_list(includes).each_key do |include_item|
344
344
  relationship_to_include = relationships_to_serialize[include_item]
345
- raise ArgumentError, "#{include_item} is not specified as a relationship on #{name}" unless relationship_to_include
345
+ raise(JSONAPI::Serializer::UnsupportedIncludeError.new(include_item, name)) unless relationship_to_include
346
346
 
347
347
  relationship_to_include.static_serializer # called for a side-effect to check for a known serializer class.
348
348
  end
@@ -12,12 +12,12 @@ module FastJsonapi
12
12
  object_block:,
13
13
  serializer:,
14
14
  relationship_type:,
15
- cached: false,
16
15
  polymorphic:,
17
16
  conditional_proc:,
18
17
  transform_method:,
19
18
  links:,
20
19
  meta:,
20
+ cached: false,
21
21
  lazy_load_data: false
22
22
  )
23
23
  @owner = owner
@@ -71,12 +71,11 @@ module FastJsonapi
71
71
  record_hash = cache_store_instance.fetch(record, **cache_opts) do
72
72
  temp_hash = id_hash(id_from_record(record, params), record_type, true)
73
73
  temp_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
74
- temp_hash[:relationships] = {}
75
74
  temp_hash[:relationships] = relationships_hash(record, cachable_relationships_to_serialize, fieldset, includes_list, params) if cachable_relationships_to_serialize.present?
76
75
  temp_hash[:links] = links_hash(record, params) if data_links.present?
77
76
  temp_hash
78
77
  end
79
- record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present?
78
+ record_hash[:relationships] = (record_hash[:relationships] || {}).merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present?
80
79
  else
81
80
  record_hash = id_hash(id_from_record(record, params), record_type, true)
82
81
  record_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONAPI
4
+ module Serializer
5
+ class Error < StandardError; end
6
+
7
+ class UnsupportedIncludeError < Error
8
+ attr_reader :include_item, :klass
9
+
10
+ def initialize(include_item, klass)
11
+ super()
12
+ @include_item = include_item
13
+ @klass = klass
14
+ end
15
+
16
+ def message
17
+ "#{include_item} is not specified as a relationship on #{klass}"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Serializer
3
- VERSION = '2.1.0'.freeze
3
+ VERSION = '2.2.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JSON:API Serializer Community
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-30 00:00:00.000000000 Z
11
+ date: 2021-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -220,6 +220,7 @@ files:
220
220
  - lib/generators/serializer/serializer_generator.rb
221
221
  - lib/generators/serializer/templates/serializer.rb.tt
222
222
  - lib/jsonapi/serializer.rb
223
+ - lib/jsonapi/serializer/errors.rb
223
224
  - lib/jsonapi/serializer/instrumentation.rb
224
225
  - lib/jsonapi/serializer/version.rb
225
226
  homepage: https://github.com/jsonapi-serializer/jsonapi-serializer
@@ -241,7 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
242
  - !ruby/object:Gem::Version
242
243
  version: '0'
243
244
  requirements: []
244
- rubygems_version: 3.1.2
245
+ rubygems_version: 3.2.3
245
246
  signing_key:
246
247
  specification_version: 4
247
248
  summary: Fast JSON:API serialization library