rest_framework 0.6.2 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/rest_framework/controller_mixins/base.rb +9 -0
- data/lib/rest_framework/serializers.rb +52 -52
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58fa867a9eed508be6ef78687d445cb5f3c0dc8cf0c004d8a8eab7f003f622cb
|
4
|
+
data.tar.gz: da285af9bf75273dcd864362d57ada399cc876e0e28767e546a89d1a8f280d28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 480e49357afb571538b0f5ec730eff04a0dc326ca4d9e93e9bbba7a60571c3dc2b09ddd3cbfa4ca2dec734e4de5de6faec542e5d06d5e9674e2c492575d2b450
|
7
|
+
data.tar.gz: 138e532ede2fdd207a557edf0cc8fd89f24eceb782b251adf107c3e445ad2e0a32d69fa66475d1215321f6085a953cce98f586284e39d207d453727e66f8a714
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
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
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
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.
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
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],
|
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] =
|
168
|
+
cfg[:except] = except
|
172
169
|
end
|
173
|
-
cfg[:include] = self.class.filter_subcfg(cfg[:include],
|
174
|
-
cfg[:methods] = self.class.filter_subcfg(cfg[:methods],
|
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],
|
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:
|
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[:
|
189
|
+
cfg[:only] = only
|
192
190
|
end
|
193
|
-
cfg[:include] = self.class.filter_subcfg(cfg[:include], only:
|
194
|
-
cfg[:methods] = self.class.filter_subcfg(cfg[:methods], only:
|
195
|
-
cfg[:serializer_methods] = self.class.filter_subcfg(
|
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.
|
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-
|
11
|
+
date: 2022-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|