rest_framework 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 207b074408e0f95dd2d289abdd7d048f66f44d38f795b45b51245af28376a1c4
4
- data.tar.gz: 67729758c21dd7627419248801d85f929188c64f2b20dba9095938a9650259da
3
+ metadata.gz: 58fa867a9eed508be6ef78687d445cb5f3c0dc8cf0c004d8a8eab7f003f622cb
4
+ data.tar.gz: da285af9bf75273dcd864362d57ada399cc876e0e28767e546a89d1a8f280d28
5
5
  SHA512:
6
- metadata.gz: 8dd2c8c6209b04ffa77fe1506906332c9beb3e87ee60bad2f348ab5331258b46fe66fc3ced5048b66472c424b84132746fbca650b82faa87415c1a36b41c16c2
7
- data.tar.gz: d04ceada0800c54161cfaeccb3d43251c3ae3bd5c629a937da058c0be640c0287453ac3338744d36e0290aea536b4987f56e76b0ace999580bd90adbd14d85c1
6
+ metadata.gz: 480e49357afb571538b0f5ec730eff04a0dc326ca4d9e93e9bbba7a60571c3dc2b09ddd3cbfa4ca2dec734e4de5de6faec542e5d06d5e9674e2c492575d2b450
7
+ data.tar.gz: 138e532ede2fdd207a557edf0cc8fd89f24eceb782b251adf107c3e445ad2e0a32d69fa66475d1215321f6085a953cce98f586284e39d207d453727e66f8a714
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.6.4
@@ -51,6 +51,10 @@ module RESTFramework::BaseControllerMixin
51
51
  serialize_to_xml: true,
52
52
  singleton_controller: nil,
53
53
  skip_actions: nil,
54
+
55
+ # Helper to disable serializer adapters by default, mainly introduced because Active Model
56
+ # Serializers will do things like serialize `[]` into `{"":[]}`.
57
+ disable_adapters_by_default: true,
54
58
  }.each do |a, default|
55
59
  next if base.respond_to?(a)
56
60
 
@@ -138,6 +142,11 @@ module RESTFramework::BaseControllerMixin
138
142
  json_kwargs = kwargs.delete(:json_kwargs) || {}
139
143
  xml_kwargs = kwargs.delete(:xml_kwargs) || {}
140
144
 
145
+ # Do not use any adapters by default, if configured.
146
+ if self.class.disable_adapters_by_default && !kwargs.key?(:adapter)
147
+ kwargs[:adapter] = nil
148
+ end
149
+
141
150
  # Raise helpful error if payload is nil. Usually this happens when a record is not found (e.g.,
142
151
  # when passing something like `User.find_by(id: some_id)` to `api_response`). The caller should
143
152
  # actually be calling `find_by!` to raise ActiveRecord::RecordNotFound and allowing the REST
@@ -101,53 +101,48 @@ class RESTFramework::NativeSerializer < RESTFramework::BaseSerializer
101
101
  return controller_serializer || @controller.class.try(:native_serializer_config)
102
102
  end
103
103
 
104
- # Helper to filter (mutate) a single subconfig for specific keys.
105
- def self.filter_subcfg(subcfg, except: nil, only: nil, additive: false)
106
- return subcfg unless except || only
107
- return subcfg unless subcfg || additive
108
- raise "Cannot pass `only` and `additive` to filter_subcfg." if only && additive
104
+ # Helper to filter a single subconfig for specific keys. By default, keys from `fields` are
105
+ # removed from the provided `subcfg`. There are two (mutually exclusive) options to adjust the
106
+ # behavior:
107
+ #
108
+ # `add`: Add any `fields` to the `subcfg` which aren't already in the `subcfg`.
109
+ # `only`: Remove any values found in the `subcfg` not in `fields`.
110
+ def self.filter_subcfg(subcfg, fields:, add: false, only: false)
111
+ raise "`add` and `only` conflict with one another" if add && only
112
+
113
+ # Don't process nil `subcfg`s.
114
+ return subcfg unless subcfg
109
115
 
110
116
  if subcfg.is_a?(Array)
111
- subcfg = subcfg.map(&:to_sym)
112
- if except
113
- if additive
114
- # Only add fields which are not already included.
115
- subcfg += except - subcfg
116
- else
117
- subcfg -= except
118
- end
117
+ subcfg = subcfg.transform_values(&:to_sym)
118
+
119
+ if add
120
+ # Only add fields which are not already included.
121
+ subcfg += fields - subcfg
119
122
  elsif only
120
- # Ignore `additive` in an `only` context, since it could causing data leaking.
121
- unless additive
122
- subcfg.select! { |c| c.in?(only) }
123
- end
123
+ subcfg.select! { |c| c.in?(fields) }
124
+ else
125
+ subcfg -= fields
124
126
  end
125
127
  elsif subcfg.is_a?(Hash)
126
- # Additive doesn't make sense in a hash context since we wouldn't know the values.
127
- unless additive
128
- if except
129
- subcfg.symbolize_keys!
130
- subcfg.reject! { |k, _v| k.in?(except) }
131
- elsif only
132
- subcfg.symbolize_keys!
133
- subcfg.select! { |k, _v| k.in?(only) }
134
- end
135
- end
136
- elsif !subcfg
137
- if additive && except
138
- subcfg = except
128
+ subcfg = subcfg.symbolize_keys
129
+
130
+ if add
131
+ # Add doesn't make sense in a hash context since we wouldn't know the values.
132
+ elsif only
133
+ subcfg.select! { |k, _v| k.in?(fields) }
134
+ else
135
+ subcfg.reject! { |k, _v| k.in?(fields) }
139
136
  end
140
137
  else # Subcfg is a single element (assume string/symbol).
141
138
  subcfg = subcfg.to_sym
142
139
 
143
- if except
144
- if subcfg.in?(except)
145
- subcfg = [] unless additive
146
- elsif additive
147
- subcfg = [subcfg, *except]
148
- end
149
- elsif only && !additive && !subcfg.in?(only) # Protect only/additive data-leaking.
150
- subcfg = []
140
+ if add
141
+ subcfg = subcfg.in?(fields) ? fields : [subcfg, *fields]
142
+ elsif only
143
+ subcfg = subcfg.in?(fields) ? subcfg : []
144
+ else
145
+ subcfg = subcfg.in?(fields) ? [] : subcfg
151
146
  end
152
147
  end
153
148
 
@@ -166,33 +161,38 @@ class RESTFramework::NativeSerializer < RESTFramework::BaseSerializer
166
161
  unless except.empty?
167
162
  # Filter `only`, `except` (additive), `include`, `methods`, and `serializer_methods`.
168
163
  if cfg[:only]
169
- cfg[:only] = self.class.filter_subcfg(cfg[:only], except: except)
164
+ cfg[:only] = self.class.filter_subcfg(cfg[:only], fields: except)
165
+ elsif cfg[:except]
166
+ cfg[:except] = self.class.filter_subcfg(cfg[:except], fields: except, add: true)
170
167
  else
171
- cfg[:except] = self.class.filter_subcfg(cfg[:except], except: except, additive: true)
168
+ cfg[:except] = except
172
169
  end
173
- cfg[:include] = self.class.filter_subcfg(cfg[:include], except: except)
174
- cfg[:methods] = self.class.filter_subcfg(cfg[:methods], except: except)
170
+ cfg[:include] = self.class.filter_subcfg(cfg[:include], fields: except)
171
+ cfg[:methods] = self.class.filter_subcfg(cfg[:methods], fields: except)
175
172
  cfg[:serializer_methods] = self.class.filter_subcfg(
176
- cfg[:serializer_methods], except: except
173
+ cfg[:serializer_methods], fields: except
177
174
  )
178
175
  end
179
176
  elsif only_param && only = @controller.request.query_parameters[only_param].presence
180
177
  only = only.split(",").map(&:strip).map(&:to_sym)
181
178
 
182
179
  unless only.empty?
183
- # For the `except` part of the serializer, we need to append any columns not in `only`.
184
- model = @controller.get_model
185
- except_cols = model&.column_names&.map(&:to_sym)&.reject { |c| c.in?(only) }
186
-
187
180
  # Filter `only`, `except` (additive), `include`, and `methods`.
188
181
  if cfg[:only]
189
- cfg[:only] = self.class.filter_subcfg(cfg[:only], only: only)
182
+ cfg[:only] = self.class.filter_subcfg(cfg[:only], fields: only, only: true)
183
+ elsif cfg[:except]
184
+ # For the `except` part of the serializer, we need to append any columns not in `only`.
185
+ model = @controller.get_model
186
+ except_cols = model&.column_names&.map(&:to_sym)&.reject { |c| c.in?(only) }
187
+ cfg[:except] = self.class.filter_subcfg(cfg[:except], fields: except_cols, add: true)
190
188
  else
191
- cfg[:except] = self.class.filter_subcfg(cfg[:except], except: except_cols, additive: true)
189
+ cfg[:only] = only
192
190
  end
193
- cfg[:include] = self.class.filter_subcfg(cfg[:include], only: only)
194
- cfg[:methods] = self.class.filter_subcfg(cfg[:methods], only: only)
195
- cfg[:serializer_methods] = self.class.filter_subcfg(cfg[:serializer_methods], only: only)
191
+ cfg[:include] = self.class.filter_subcfg(cfg[:include], fields: only, only: true)
192
+ cfg[:methods] = self.class.filter_subcfg(cfg[:methods], fields: only, only: true)
193
+ cfg[:serializer_methods] = self.class.filter_subcfg(
194
+ cfg[:serializer_methods], fields: only, only: true
195
+ )
196
196
  end
197
197
  end
198
198
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory N. Schmit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-15 00:00:00.000000000 Z
11
+ date: 2022-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails