media_types-serialization 1.3.2 → 1.3.4

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: b21177c2ffb12e53fc1c077dc76e3228d79db6a26c9dbf8f84a0f8703e79d31f
4
- data.tar.gz: 61355285dbc6025e54c7fb24ccb53e453a036135515baa5abac76e88344d2b99
3
+ metadata.gz: 737a38bc10cd0c3aff8fa9bc33c3c192098709eb331f7c35706840410ff3d280
4
+ data.tar.gz: d6311d9a1b27329e4164cb7bf3b8e839ce96419b05e9a9e42d033d7ba2942a50
5
5
  SHA512:
6
- metadata.gz: c00d90c86d30ff29980942d443d93ba4368ec69c161e1d0da28cd9b7dff9b1616662e035ee42c702c8d49d2b31ec9dd2b98e2bfc9a419044d244e66430c6b71a
7
- data.tar.gz: 6792550a393bb078940e3db00407881ca698e439f9b3387ea2e04a007c3f17efd00a57f344b2ec2f3dd698e57001185262a858dad53fed0cd10055717beabcb3
6
+ metadata.gz: c6fad7b09ecc516290fb5e0393ee5a790c128a75402cdb977c396b6dac01b30748b63f5631b001b7eaa9d9c1b8007e814200d9be7766afa757d1c93785bc34f8
7
+ data.tar.gz: ea95bb41414b2125ac3af7efe42ff9c5e521f36a7bae2930760dd52fdee3e58e1175e5aaa8f03d3eb27bf5022d089f0542f4bc51b418cee05ae3724ba3756779
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.4
4
+
5
+ - Same as `1.3.3`
6
+
7
+ ## 1.3.3
8
+
9
+ - 🐛 Fix override suffix not being picked up correctly
10
+ - 🐛 Fix inability to override suffix for aliases
11
+ - 🐛 Fix inability to override suffix for raw
12
+ - 🐛 Fix default suffix for raw
13
+
3
14
  ## 1.3.2
4
15
 
5
16
  - 🐛 Fix override suffix not returning self or new
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types-serialization (1.3.2)
4
+ media_types-serialization (1.3.4)
5
5
  actionpack (>= 4.0.0)
6
6
  activesupport (>= 4.0.0)
7
- media_types (>= 2.0.0, < 3.0.0)
7
+ media_types (>= 2.1.0, < 3.0.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -56,26 +56,26 @@ GEM
56
56
  concurrent-ruby (1.1.9)
57
57
  crass (1.0.6)
58
58
  erubi (1.10.0)
59
- globalid (0.4.2)
60
- activesupport (>= 4.2.0)
59
+ globalid (0.5.2)
60
+ activesupport (>= 5.0)
61
61
  i18n (1.8.10)
62
62
  concurrent-ruby (~> 1.0)
63
- loofah (2.10.0)
63
+ loofah (2.12.0)
64
64
  crass (~> 1.0.2)
65
65
  nokogiri (>= 1.5.9)
66
66
  mail (2.7.1)
67
67
  mini_mime (>= 0.1.1)
68
68
  marcel (1.0.1)
69
- media_types (2.0.1)
69
+ media_types (2.1.0)
70
70
  method_source (1.0.0)
71
71
  mini_mime (1.1.0)
72
72
  minitest (5.14.4)
73
- nio4r (2.5.7)
74
- nokogiri (1.11.7-x64-mingw32)
73
+ nio4r (2.5.8)
74
+ nokogiri (1.12.3-x64-mingw32)
75
75
  racc (~> 1.4)
76
- nokogiri (1.11.7-x86_64-linux)
76
+ nokogiri (1.12.3-x86_64-linux)
77
77
  racc (~> 1.4)
78
- oj (3.12.3)
78
+ oj (3.13.1)
79
79
  racc (1.5.2)
80
80
  rack (2.2.3)
81
81
  rack-test (1.1.0)
data/README.md CHANGED
@@ -578,7 +578,7 @@ Custom attributes can be added using the `problem_output.attribute(name, value)`
578
578
 
579
579
  ## API
580
580
 
581
- ### Serializer definition
581
+ ### Serializer class definition
582
582
 
583
583
  These methods become available during class definition if you inherit from `MediaTypes::Serialization::Base`.
584
584
 
@@ -597,6 +597,7 @@ Either validator or unvalidated must be used while defining a serializer.
597
597
  #### `output( view:, version:, versions: ) do |obj, version, context|`
598
598
 
599
599
  Defines a serialization block. Either version or versions can be set.
600
+ `nil` is allowed for unversioned.
600
601
  View should be a symbol or unset.
601
602
 
602
603
  Obj is the object to be serialized, version is the negotiated version and context is the context passed in from the serialize function.
@@ -604,26 +605,30 @@ When using the controller integration, context is the current controller.
604
605
 
605
606
  The block should return an object to convert into JSON.
606
607
 
607
- #### `output_raw( view:, version:, versions: ) do |obj, version, context|`
608
+ #### `output_raw( view:, version:, versions:, suffix: ) do |obj, version, context|`
608
609
 
609
610
  This has the same behavior as `output` but should return a string instead of an object.
610
611
  Output is not validated.
612
+ By default, `input_raw` is expected to _not_ be JSON.
613
+ Override `suffix` with `:json` if it _is_ JSON.
611
614
 
612
- #### `output_alias( media_type_identifier, view:, hide_variant: false )`
615
+ #### `output_alias( media_type_identifier, view:, hide_variant: false, suffix: '~' )`
613
616
 
614
617
  Defines a legacy mapping. This will make the deserializer parse the media type `media_type_identifier` as if it was version `nil` of the specified view.
615
618
  If `view` is undefined it will use the output serializer without a view defined.
619
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
616
620
 
617
621
  Response will have a content type equal to `[media_type_identifier]; variant=[mapped_media_type_identifier]`.
618
622
  If `hide_variant:` is true, the content type emitted will only be `[media_type_identifier]`.
619
623
 
620
624
  > You cannot alias a _versioned_ media type, otherwise it would be easy to later break the definition by changing the version it aliases.
621
625
 
622
- #### `output_alias_optional( media_type_identifier, view:, hide_variant: false )`
626
+ #### `output_alias_optional( media_type_identifier, view:, hide_variant: false, suffix: '~' )`
623
627
 
624
628
  Has the same behavior as `output_alias` but can be used by multiple serializers.
625
629
  The serializer that is loaded last in the controller 'wins' control over this media type identifier.
626
630
  If any of the serializers have an `output_alias` defined with the same media type identifier that one will win instead.
631
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
627
632
 
628
633
  Response will have a content type equal to `[media_type_identifier]; variant=[mapped_media_type_identifier]`. If `hide_variant:` is true, the content type emitted will only be `[media_type_identifier]`.
629
634
 
@@ -638,29 +643,34 @@ When using the controller integration, context is the current controller.
638
643
  The block should return the internal representation of the object.
639
644
  Best practise is to make sure not to change state in this function but to leave that up to the controller.
640
645
 
641
- #### `input_raw( view:, version:, versions: ) do |bytes, version, context|`
646
+ #### `input_raw( view:, version:, versions:, suffix: nil ) do |bytes, version, context|`
642
647
 
643
- This has the same behavior as `input` but takes in raw data. Input is not validated.
648
+ This has the same behavior as `input` but takes in raw data.
649
+ Input is not validated.
650
+ By default, `input_raw` is expected to _not_ be JSON.
651
+ Override `suffix` with `:json` if it _is_ JSON.
644
652
 
645
- #### `input_alias( media_type_identifier, view: )`
653
+ #### `input_alias( media_type_identifier, view:, suffix: '~' )`
646
654
 
647
655
  Defines a legacy mapping.
648
656
  This will make the serializer parse the media type `media_type_identifier` as if it was version `nil` of the specified view.
649
657
  If view is undefined it will use the input serializer without a view defined.
658
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
650
659
 
651
660
  > You cannot alias a _versioned_ media type, otherwise it would be easy to later break the definition by changing the version it aliases.
652
661
 
653
- #### `input_alias_optional( media_type_identifier, view: )`
662
+ #### `input_alias_optional( media_type_identifier, view:, suffix: '~' )`
654
663
 
655
664
  Has the same behavior as `input_alias` but can be used by multiple serializers.
656
665
  The serializer that is loaded last in the controller 'wins' control over this media type identifier.
657
666
  If any of the serializers have an `input_alias` defined with the same media type identifier that one will win instead.
667
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
658
668
 
659
669
  #### `disable_wildcards`
660
670
 
661
671
  Disables registering wildcard media types.
662
672
 
663
- ### Serializer definition
673
+ ### Serializer output definition
664
674
 
665
675
  The following methods are available within an `output ... do` block.
666
676
 
@@ -682,12 +692,16 @@ Returns the built up context so far.
682
692
 
683
693
  #### `index( array, serializer, version:, view: nil )`
684
694
 
695
+ > Not the same as a validator `collection`.
696
+
685
697
  Adds an `_index` block to the current context. Uses the self links of the specified view to construct an index of urls to the child objects.
686
698
 
687
699
  Returns the built up context so far.
688
700
 
689
701
  #### `collection( array, serializer, version:, view: nil )`
690
702
 
703
+ > Not the same as a validator `collection`.
704
+
691
705
  Adds an `_embedded` block to the current context. Uses the specified serializer to embed the child objects.
692
706
  Optionally a block can be used to modify the output from the child serializer.
693
707
 
@@ -711,9 +725,25 @@ Returns the built up context so far.
711
725
 
712
726
  Runs a block in a new context and returns the result
713
727
 
728
+ > Most common use-case is emitting from an enumerable.
729
+ >
730
+ > ```ruby
731
+ > results = [item, item, item].map do |current_item|
732
+ > object do
733
+ > attribute :foo, current_item.bar
734
+ > end
735
+ > end
736
+ >
737
+ > attribute :items, results
738
+ > ```
739
+
714
740
  #### `render_view( view, context:, **args)`
715
741
 
716
- Can be used to render a view. You can set local variables in the view by assigning a hash to the `assigns:` parameter.
742
+ Can be used to render a view.
743
+ You can set local variables in the view by assigning a hash to the `assigns:` parameter.
744
+ Returns a `string`
745
+
746
+ > When possible, prefer `output_raw` with `context.class.render(params)`
717
747
 
718
748
  ### Controller definition
719
749
 
@@ -345,7 +345,10 @@ module MediaTypes
345
345
 
346
346
  MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED = ::Object.new
347
347
 
348
- def render_media(obj = MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED, serializers: nil, not_acceptable_serializer: nil, **options, &block)
348
+ def render_media(obj = MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED, **options, &block)
349
+ serializers = options.delete(:serializers)
350
+ not_acceptable_serializer = options.delete(:not_acceptable_serializer)
351
+
349
352
  if obj == MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED && options.keys.any? && !block
350
353
  # options is too greedy :(
351
354
  obj = options
@@ -44,35 +44,76 @@ module MediaTypes
44
44
  validator = serializer_validator.view(view).version(v)
45
45
  validator.override_suffix(:json) unless serializer_validated
46
46
 
47
- serializer_output_registration.register_block(self, validator, v, block, false, wildcards: !self.serializer_disable_wildcards)
47
+ serializer_output_registration.register_block(
48
+ self,
49
+ validator,
50
+ v,
51
+ block,
52
+ false,
53
+ wildcards:
54
+ !serializer_disable_wildcards
55
+ )
48
56
  end
49
57
  end
50
58
 
51
- def output_raw(view: nil, version: nil, versions: nil, &block)
59
+ def output_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
52
60
  versions = [version] if versions.nil?
53
61
  raise VersionsNotAnArrayError unless versions.is_a? Array
54
62
 
55
63
  raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?
56
64
 
57
65
  versions.each do |v|
58
- validator = serializer_validator.view(view).version(v).override_suffix('')
59
-
60
- serializer_output_registration.register_block(self, validator, v, block, true, wildcards: !self.serializer_disable_wildcards)
66
+ validator = serializer_validator.view(view)
67
+ .version(v)
68
+ .override_suffix(suffix)
69
+
70
+ serializer_output_registration.register_block(
71
+ self,
72
+ validator,
73
+ v,
74
+ block,
75
+ true,
76
+ wildcards: !serializer_disable_wildcards
77
+ )
61
78
  end
62
79
  end
63
80
 
64
- def output_alias(media_type_identifier, view: nil, hide_variant: false)
65
- validator = serializer_validator.view(view)
81
+ def output_alias(
82
+ media_type_identifier,
83
+ view: nil,
84
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
85
+ hide_variant: false
86
+ )
87
+ validator = serializer_validator.view(view).override_suffix(suffix)
66
88
  victim_identifier = validator.identifier
67
89
 
68
- serializer_output_registration.register_alias(self, media_type_identifier, victim_identifier, false, hide_variant, wildcards: !self.serializer_disable_wildcards)
69
- end
70
-
71
- def output_alias_optional(media_type_identifier, view: nil, hide_variant: false)
72
- validator = serializer_validator.view(view)
90
+ serializer_output_registration.register_alias(
91
+ self,
92
+ media_type_identifier,
93
+ victim_identifier,
94
+ false,
95
+ hide_variant,
96
+ wildcards: !serializer_disable_wildcards
97
+ )
98
+ end
99
+
100
+ def output_alias_optional(
101
+ media_type_identifier,
102
+ view: nil,
103
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
104
+ hide_variant: false
105
+ )
106
+ validator = serializer_validator.view(view).override_suffix(suffix)
73
107
  victim_identifier = validator.identifier
74
108
 
75
- serializer_output_registration.register_alias(self, media_type_identifier, victim_identifier, true, hide_variant, wildcards: !self.serializer_disable_wildcards)
109
+ serializer_output_registration.register_alias(
110
+ self,
111
+ media_type_identifier,
112
+ victim_identifier,
113
+ true,
114
+ hide_variant,
115
+ wildcards: !serializer_disable_wildcards
116
+ )
76
117
  end
77
118
 
78
119
  def input(view: nil, version: nil, versions: nil, &block)
@@ -89,31 +130,53 @@ module MediaTypes
89
130
  end
90
131
  end
91
132
 
92
- def input_raw(view: nil, version: nil, versions: nil, &block)
133
+ def input_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
93
134
  versions = [version] if versions.nil?
94
135
  raise VersionsNotAnArrayError unless versions.is_a? Array
95
136
 
96
137
  raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?
97
138
 
98
139
  versions.each do |v|
99
- validator = serializer_validator.view(view).version(v)
140
+ validator = serializer_validator.view(view).version(v).override_suffix(suffix)
100
141
 
101
142
  serializer_input_registration.register_block(self, validator, v, block, true)
102
143
  end
103
144
  end
104
145
 
105
- def input_alias(media_type_identifier, view: nil)
106
- validator = serializer_validator.view(view)
146
+ def input_alias(
147
+ media_type_identifier,
148
+ view: nil,
149
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
150
+ )
151
+ validator = serializer_validator.view(view).override_suffix(suffix)
107
152
  victim_identifier = validator.identifier
108
153
 
109
- serializer_input_registration.register_alias(self, media_type_identifier, victim_identifier, false, true, wildcards: false)
110
- end
111
-
112
- def input_alias_optional(media_type_identifier, view: nil)
113
- validator = serializer_validator.view(view)
154
+ serializer_input_registration.register_alias(
155
+ self,
156
+ media_type_identifier,
157
+ victim_identifier,
158
+ false,
159
+ true,
160
+ wildcards: false
161
+ )
162
+ end
163
+
164
+ def input_alias_optional(
165
+ media_type_identifier,
166
+ view: nil,
167
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
168
+ )
169
+ validator = serializer_validator.view(view).override_suffix(suffix)
114
170
  victim_identifier = validator.identifier
115
171
 
116
- serializer_input_registration.register_alias(self, media_type_identifier, victim_identifier, true, true, wildcards: false)
172
+ serializer_input_registration.register_alias(
173
+ self,
174
+ media_type_identifier,
175
+ victim_identifier,
176
+ true,
177
+ true,
178
+ wildcards: false
179
+ )
117
180
  end
118
181
 
119
182
  def serialize(victim, media_type_identifier, context:, dsl: nil, raw: nil)
@@ -135,6 +198,8 @@ module MediaTypes
135
198
  end
136
199
 
137
200
  def self.inherited(subclass)
201
+ super
202
+
138
203
  subclass.extend(ClassMethods)
139
204
  subclass.instance_eval do
140
205
  class << self
@@ -36,7 +36,7 @@ module MediaTypes
36
36
 
37
37
  unless registrations.key? target_identifier
38
38
  potential_match = registrations.keys.find do |k|
39
- k.starts_with? target_identifier
39
+ k.start_with? target_identifier
40
40
  end
41
41
  raise VersionedAliasDefinitionError.new(target_identifier, inout, potential_match) unless potential_match.nil?
42
42
  raise UnbackedAliasDefinitionError.new(target_identifier, inout)
@@ -152,7 +152,7 @@ module MediaTypes
152
152
  self.raw = raw
153
153
  super(serializer, inout, validator, display_identifier)
154
154
  end
155
-
155
+
156
156
  def merge(other)
157
157
  return nil unless other.is_a?(SerializationAliasRegistration)
158
158
 
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Serialization
3
- VERSION = '1.3.2'.freeze
3
+ VERSION = '1.3.4'.freeze
4
4
  end
5
5
  end
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
37
37
 
38
38
  spec.add_dependency 'actionpack', '>= 4.0.0'
39
39
  spec.add_dependency 'activesupport', '>= 4.0.0'
40
- spec.add_dependency 'media_types', '>= 2.0.0', '< 3.0.0'
40
+ spec.add_dependency 'media_types', '>= 2.1.0', '< 3.0.0'
41
41
 
42
42
  spec.add_development_dependency 'awesome_print'
43
43
  spec.add_development_dependency 'bundler'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_types-serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-08-11 00:00:00.000000000 Z
12
+ date: 2021-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -45,7 +45,7 @@ dependencies:
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: 2.0.0
48
+ version: 2.1.0
49
49
  - - "<"
50
50
  - !ruby/object:Gem::Version
51
51
  version: 3.0.0
@@ -55,7 +55,7 @@ dependencies:
55
55
  requirements:
56
56
  - - ">="
57
57
  - !ruby/object:Gem::Version
58
- version: 2.0.0
58
+ version: 2.1.0
59
59
  - - "<"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.0.0