media_types-serialization 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +32 -32
  3. data/.github/workflows/publish-bookworm.yml +33 -0
  4. data/.github/workflows/publish-sid.yml +33 -0
  5. data/.gitignore +22 -14
  6. data/.idea/.rakeTasks +7 -7
  7. data/.idea/dictionaries/Derk_Jan.xml +6 -6
  8. data/.idea/encodings.xml +3 -3
  9. data/.idea/inspectionProfiles/Project_Default.xml +5 -5
  10. data/.idea/media_types-serialization.iml +76 -76
  11. data/.idea/misc.xml +6 -6
  12. data/.idea/modules.xml +7 -7
  13. data/.idea/runConfigurations/test.xml +19 -19
  14. data/.idea/vcs.xml +5 -5
  15. data/CHANGELOG.md +190 -182
  16. data/CODE_OF_CONDUCT.md +74 -74
  17. data/Gemfile +4 -4
  18. data/LICENSE.txt +21 -21
  19. data/README.md +1048 -1048
  20. data/Rakefile +10 -10
  21. data/bin/console +14 -14
  22. data/bin/setup +8 -8
  23. data/lib/media_types/problem.rb +67 -67
  24. data/lib/media_types/serialization/base.rb +269 -216
  25. data/lib/media_types/serialization/error.rb +193 -193
  26. data/lib/media_types/serialization/fake_validator.rb +53 -53
  27. data/lib/media_types/serialization/serialization_dsl.rb +135 -135
  28. data/lib/media_types/serialization/serialization_registration.rb +245 -245
  29. data/lib/media_types/serialization/serializers/api_viewer.rb +383 -136
  30. data/lib/media_types/serialization/serializers/common_css.rb +212 -168
  31. data/lib/media_types/serialization/serializers/endpoint_description_serializer.rb +80 -80
  32. data/lib/media_types/serialization/serializers/fallback_not_acceptable_serializer.rb +85 -85
  33. data/lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb +58 -58
  34. data/lib/media_types/serialization/serializers/input_validation_error_serializer.rb +93 -93
  35. data/lib/media_types/serialization/serializers/problem_serializer.rb +111 -111
  36. data/lib/media_types/serialization/utils/accept_header.rb +77 -77
  37. data/lib/media_types/serialization/utils/accept_language_header.rb +82 -82
  38. data/lib/media_types/serialization/version.rb +7 -7
  39. data/lib/media_types/serialization.rb +682 -675
  40. data/media_types-serialization.gemspec +48 -48
  41. metadata +9 -8
  42. data/Gemfile.lock +0 -167
@@ -1,216 +1,269 @@
1
- # frozen_string_literal: true
2
-
3
- require 'media_types/serialization/error'
4
- require 'media_types/serialization/fake_validator'
5
- require 'media_types/serialization/serialization_registration'
6
- require 'media_types/serialization/serialization_dsl'
7
-
8
- module MediaTypes
9
- module Serialization
10
- class Base
11
- module ClassMethods
12
- def unvalidated(prefix)
13
- self.serializer_validated = false
14
- self.serializer_validator = FakeValidator.new(prefix)
15
- self.serializer_input_registration = SerializationRegistration.new(:input)
16
- self.serializer_output_registration = SerializationRegistration.new(:output)
17
- end
18
-
19
- def validator(validator = nil)
20
- raise NoValidatorSetError if !defined? serializer_validator && validator.nil?
21
- return serializer_validator if validator.nil?
22
-
23
- self.serializer_validated = true
24
- self.serializer_validator = validator
25
- self.serializer_input_registration = SerializationRegistration.new(:input)
26
- self.serializer_output_registration = SerializationRegistration.new(:output)
27
- end
28
-
29
- def disable_wildcards
30
- self.serializer_disable_wildcards = true
31
- end
32
-
33
- def enable_wildcards
34
- self.serializer_disable_wildcards = false
35
- end
36
-
37
- def output(view: nil, version: nil, versions: nil, &block)
38
- versions = [version] if versions.nil?
39
- raise VersionsNotAnArrayError unless versions.is_a? Array
40
-
41
- raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?
42
-
43
- versions.each do |v|
44
- validator = serializer_validator.view(view).version(v)
45
- validator.override_suffix(:json) unless serializer_validated
46
-
47
- serializer_output_registration.register_block(
48
- self,
49
- validator,
50
- v,
51
- block,
52
- false,
53
- wildcards:
54
- !serializer_disable_wildcards
55
- )
56
- end
57
- end
58
-
59
- def output_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
60
- versions = [version] if versions.nil?
61
- raise VersionsNotAnArrayError unless versions.is_a? Array
62
-
63
- raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?
64
-
65
- versions.each do |v|
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
- )
78
- end
79
- end
80
-
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)
88
- victim_identifier = validator.identifier
89
-
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)
107
- victim_identifier = validator.identifier
108
-
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
- )
117
- end
118
-
119
- def input(view: nil, version: nil, versions: nil, &block)
120
- versions = [version] if versions.nil?
121
- raise VersionsNotAnArrayError unless versions.is_a? Array
122
-
123
- raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?
124
-
125
- versions.each do |v|
126
- validator = serializer_validator.view(view).version(v)
127
- validator.override_suffix(:json) unless serializer_validated
128
-
129
- serializer_input_registration.register_block(self, validator, v, block, false)
130
- end
131
- end
132
-
133
- def input_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
134
- versions = [version] if versions.nil?
135
- raise VersionsNotAnArrayError unless versions.is_a? Array
136
-
137
- raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?
138
-
139
- versions.each do |v|
140
- validator = serializer_validator.view(view).version(v).override_suffix(suffix)
141
-
142
- serializer_input_registration.register_block(self, validator, v, block, true)
143
- end
144
- end
145
-
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)
152
- victim_identifier = validator.identifier
153
-
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)
170
- victim_identifier = validator.identifier
171
-
172
- serializer_input_registration.register_alias(
173
- self,
174
- media_type_identifier,
175
- victim_identifier,
176
- true,
177
- true,
178
- wildcards: false
179
- )
180
- end
181
-
182
- def serialize(victim, media_type_identifier, context:, dsl: nil, raw: nil)
183
- dsl ||= SerializationDSL.new(self, context: context)
184
- serializer_output_registration.call(victim, media_type_identifier.to_s, context, dsl: dsl, raw: raw)
185
- end
186
-
187
- def deserialize(victim, media_type_identifier, context:)
188
- serializer_input_registration.call(victim, media_type_identifier, context)
189
- end
190
-
191
- def outputs_for(views:)
192
- serializer_output_registration.filter(views: views)
193
- end
194
-
195
- def inputs_for(views:)
196
- serializer_input_registration.filter(views: views)
197
- end
198
- end
199
-
200
- def self.inherited(subclass)
201
- super
202
-
203
- subclass.extend(ClassMethods)
204
- subclass.instance_eval do
205
- class << self
206
- attr_accessor :serializer_validated
207
- attr_accessor :serializer_validator
208
- attr_accessor :serializer_input_registration
209
- attr_accessor :serializer_output_registration
210
- attr_accessor :serializer_disable_wildcards
211
- end
212
- end
213
- end
214
- end
215
- end
216
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'media_types/serialization/error'
4
+ require 'media_types/serialization/fake_validator'
5
+ require 'media_types/serialization/serialization_registration'
6
+ require 'media_types/serialization/serialization_dsl'
7
+
8
+ module MediaTypes
9
+ module Serialization
10
+ class Base
11
+ module ClassMethods
12
+ def unvalidated(prefix)
13
+ self.serializer_validated = false
14
+ self.serializer_validator = FakeValidator.new(prefix)
15
+ self.serializer_input_registrations = {}
16
+ self.serializer_output_registrations = {}
17
+ end
18
+
19
+ def validator(validator = nil)
20
+ raise NoValidatorSetError if !defined? serializer_validator && validator.nil?
21
+ return serializer_validator if validator.nil?
22
+
23
+ self.serializer_validated = true
24
+ self.serializer_validator = validator
25
+ self.serializer_input_registrations = {}
26
+ self.serializer_output_registrations = {}
27
+ end
28
+
29
+ def disable_wildcards
30
+ self.serializer_disable_wildcards = true
31
+ end
32
+
33
+ def enable_wildcards
34
+ self.serializer_disable_wildcards = false
35
+ end
36
+
37
+ def output(view: nil, version: nil, versions: nil, &block)
38
+ versions = [version] if versions.nil?
39
+ raise VersionsNotAnArrayError unless versions.is_a? Array
40
+
41
+ raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?
42
+
43
+ unless serializer_output_registrations.has_key? view
44
+ serializer_output_registrations[view] = SerializationRegistration.new(:output)
45
+ end
46
+
47
+ versions.each do |v|
48
+ validator = serializer_validator.view(view).version(v)
49
+ validator.override_suffix(:json) unless serializer_validated
50
+
51
+ serializer_output_registrations[view].register_block(
52
+ self,
53
+ validator,
54
+ v,
55
+ block,
56
+ false,
57
+ wildcards:
58
+ !serializer_disable_wildcards
59
+ )
60
+ end
61
+ end
62
+
63
+ def output_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
64
+ versions = [version] if versions.nil?
65
+ raise VersionsNotAnArrayError unless versions.is_a? Array
66
+
67
+ raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?
68
+
69
+ unless serializer_output_registrations.has_key? view
70
+ serializer_output_registrations[view] = SerializationRegistration.new(:output)
71
+ end
72
+
73
+ versions.each do |v|
74
+ validator = serializer_validator.view(view)
75
+ .version(v)
76
+ .override_suffix(suffix)
77
+
78
+ serializer_output_registrations[view].register_block(
79
+ self,
80
+ validator,
81
+ v,
82
+ block,
83
+ true,
84
+ wildcards: !serializer_disable_wildcards
85
+ )
86
+ end
87
+ end
88
+
89
+ def output_alias(
90
+ media_type_identifier,
91
+ view: nil,
92
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
93
+ hide_variant: false
94
+ )
95
+ validator = serializer_validator.view(view).override_suffix(suffix)
96
+ victim_identifier = validator.identifier
97
+
98
+ unless serializer_output_registrations.has_key? view
99
+ serializer_output_registrations[view] = SerializationRegistration.new(:output)
100
+ end
101
+
102
+ serializer_output_registrations[view].register_alias(
103
+ self,
104
+ media_type_identifier,
105
+ victim_identifier,
106
+ false,
107
+ hide_variant,
108
+ wildcards: !serializer_disable_wildcards
109
+ )
110
+ end
111
+
112
+ def output_alias_optional(
113
+ media_type_identifier,
114
+ view: nil,
115
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
116
+ hide_variant: false
117
+ )
118
+ validator = serializer_validator.view(view).override_suffix(suffix)
119
+ victim_identifier = validator.identifier
120
+
121
+ unless serializer_output_registrations.has_key? view
122
+ serializer_output_registrations[view] = SerializationRegistration.new(:output)
123
+ end
124
+
125
+ serializer_output_registrations[view].register_alias(
126
+ self,
127
+ media_type_identifier,
128
+ victim_identifier,
129
+ true,
130
+ hide_variant,
131
+ wildcards: !serializer_disable_wildcards
132
+ )
133
+ end
134
+
135
+ def input(view: nil, version: nil, versions: nil, &block)
136
+ versions = [version] if versions.nil?
137
+ raise VersionsNotAnArrayError unless versions.is_a? Array
138
+
139
+ raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?
140
+
141
+ unless serializer_input_registrations.has_key? view
142
+ serializer_input_registrations[view] = SerializationRegistration.new(:input)
143
+ end
144
+
145
+ versions.each do |v|
146
+ validator = serializer_validator.view(view).version(v)
147
+ validator.override_suffix(:json) unless serializer_validated
148
+
149
+ serializer_input_registrations[view].register_block(self, validator, v, block, false)
150
+ end
151
+ end
152
+
153
+ def input_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
154
+ versions = [version] if versions.nil?
155
+ raise VersionsNotAnArrayError unless versions.is_a? Array
156
+
157
+ raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?
158
+
159
+ unless serializer_input_registrations.has_key? view
160
+ serializer_input_registrations[view] = SerializationRegistration.new(:input)
161
+ end
162
+
163
+ versions.each do |v|
164
+ validator = serializer_validator.view(view).version(v).override_suffix(suffix)
165
+
166
+ serializer_input_registrations[view].register_block(self, validator, v, block, true)
167
+ end
168
+ end
169
+
170
+ def input_alias(
171
+ media_type_identifier,
172
+ view: nil,
173
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
174
+ )
175
+ validator = serializer_validator.view(view).override_suffix(suffix)
176
+ victim_identifier = validator.identifier
177
+
178
+ unless serializer_input_registrations.has_key? view
179
+ serializer_input_registrations[view] = SerializationRegistration.new(:input)
180
+ end
181
+
182
+ serializer_input_registrations[view].register_alias(
183
+ self,
184
+ media_type_identifier,
185
+ victim_identifier,
186
+ false,
187
+ true,
188
+ wildcards: false
189
+ )
190
+ end
191
+
192
+ def input_alias_optional(
193
+ media_type_identifier,
194
+ view: nil,
195
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
196
+ )
197
+ validator = serializer_validator.view(view).override_suffix(suffix)
198
+ victim_identifier = validator.identifier
199
+
200
+ unless serializer_input_registrations.has_key? view
201
+ serializer_input_registrations[view] = SerializationRegistration.new(:input)
202
+ end
203
+
204
+ serializer_input_registrations[view].register_alias(
205
+ self,
206
+ media_type_identifier,
207
+ victim_identifier,
208
+ true,
209
+ true,
210
+ wildcards: false
211
+ )
212
+ end
213
+
214
+ def serialize(victim, media_type_identifier, context:, dsl: nil, raw: nil)
215
+ dsl ||= SerializationDSL.new(self, context: context)
216
+
217
+ merged_output_registrations = SerializationRegistration.new(:output)
218
+ serializer_output_registrations.keys.each do |k|
219
+ merged_output_registrations = merged_output_registrations.merge(serializer_output_registrations[k])
220
+ end
221
+
222
+ merged_output_registrations.call(victim, media_type_identifier.to_s, context, dsl: dsl, raw: raw)
223
+ end
224
+
225
+ def deserialize(victim, media_type_identifier, context:)
226
+ merged_input_registrations = SerializationRegistration.new(:input)
227
+ serializer_input_registrations.keys.each do |k|
228
+ merged_input_registrations = merged_input_registrations.merge(serializer_input_registrations[k])
229
+ end
230
+
231
+ merged_input_registrations.call(victim, media_type_identifier, context)
232
+ end
233
+
234
+ def outputs_for(views:)
235
+ merged_output_registrations = SerializationRegistration.new(:output)
236
+ views.each do |k|
237
+ merged_output_registrations = merged_output_registrations.merge(serializer_output_registrations[k]) if serializer_output_registrations.has_key?(k)
238
+ end
239
+
240
+ merged_output_registrations
241
+ end
242
+
243
+ def inputs_for(views:)
244
+ merged_input_registrations = SerializationRegistration.new(:input)
245
+ views.each do |k|
246
+ merged_input_registrations = merged_input_registrations.merge(serializer_input_registrations[k]) if serializer_input_registrations.has_key?(k)
247
+ end
248
+
249
+ merged_input_registrations
250
+ end
251
+ end
252
+
253
+ def self.inherited(subclass)
254
+ super
255
+
256
+ subclass.extend(ClassMethods)
257
+ subclass.instance_eval do
258
+ class << self
259
+ attr_accessor :serializer_validated
260
+ attr_accessor :serializer_validator
261
+ attr_accessor :serializer_input_registrations
262
+ attr_accessor :serializer_output_registrations
263
+ attr_accessor :serializer_disable_wildcards
264
+ end
265
+ end
266
+ end
267
+ end
268
+ end
269
+ end