model_driven_api 3.17.0 → 3.19.0
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 +4 -4
- data/README.md +4 -0
- data/app/controllers/api/v2/application_controller.rb +3 -1
- data/app/controllers/api/v3/application_controller.rb +6 -5
- data/config/initializers/auto_include_json.rb +15 -9
- data/config/initializers/request_json_attrs.rb +5 -0
- data/lib/api/hidden_secrets.rb +44 -0
- data/lib/api/open_api/v3.rb +2 -2
- data/lib/api/resource_attribute_set.rb +3 -3
- data/lib/api/v2/request_json_attrs.rb +189 -0
- data/lib/concerns/model_driven_api_application_record.rb +3 -1
- data/lib/model_driven_api/current.rb +7 -0
- data/lib/model_driven_api/version.rb +1 -1
- data/lib/model_driven_api.rb +3 -0
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 758e0e3a9837dd6f424ab2c36d77e51204bfa357ea27fe7d667d4889a3e0e0a7
|
|
4
|
+
data.tar.gz: 86a1e32e866e87ef080601458a0c1347da2c592faeca0e8e2e8c4d6f124cc047
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 30e10a13688e1d8aefa04a7072e2516e58ddb53c34db778c745271f3ae48e6f6056f4b818faab5b1e184c11e785baf45f18f67317a73e0714790ae4b351fd5f8
|
|
7
|
+
data.tar.gz: 6acdee803752c29121791d732b4e4a5688b80a6cc37c245e092afc3cead0cd97a3abfcd39ed10ba5c44f78b68f48ce546230a9bd0768ff888cb5c036b1dcf4ba
|
data/README.md
CHANGED
|
@@ -195,6 +195,8 @@ Parameters work identically in query string (GET) and JSON body (POST search).
|
|
|
195
195
|
}
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
+
The client's options follow conventions (ADR 0006): `include` takes real associations only; secrets the model hides (a secret-named column left out of its default JSON, its `json_attrs[:except]` or `write_only_attributes`, e.g. Devise's `encrypted_password`) never come back, in v2, v3 or Ransack filters and sorts; `methods` serves what the model declares in `json_attrs[:methods]`, columns, associations, `<association>_ids`, and public methods written in the app or a thecore gem (not ending in `!`/`=`, no required arguments), which run read-only. Anything else is dropped from the response and logged; the response stays 200.
|
|
199
|
+
|
|
198
200
|
#### Ransack filtering (`q`)
|
|
199
201
|
|
|
200
202
|
```
|
|
@@ -269,6 +271,8 @@ GET /api/v3/articles?sort=-created_at # descending
|
|
|
269
271
|
GET /api/v3/articles?sort=status,-title # multi-field
|
|
270
272
|
```
|
|
271
273
|
|
|
274
|
+
Fields are validated against the model's `ransortable_attributes` (by default its `ransackable_attributes`, which leave out its hidden secrets). Unknown fields are silently ignored.
|
|
275
|
+
|
|
272
276
|
### Pagination
|
|
273
277
|
|
|
274
278
|
```
|
|
@@ -273,9 +273,11 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
273
273
|
# In order of importance: if you send the configuration via querystring you are ok
|
|
274
274
|
# has precedence over if you have setup the json_attrs in the model concern
|
|
275
275
|
# (JSON_ATTRS_PARAMS order: json_attrs wins over a; reject_non_hash_json_attrs ran first).
|
|
276
|
+
# Options from the request follow the conventions of Api::V2::RequestJsonAttrs (host Odoo #6285).
|
|
276
277
|
from_params = nil
|
|
277
278
|
JSON_ATTRS_PARAMS.each { |key| from_params = params[key].deep_symbolize_keys if params[key].present? }
|
|
278
|
-
|
|
279
|
+
return Api::V2::RequestJsonAttrs.from_params(from_params) if from_params.present?
|
|
280
|
+
@json_attrs.presence || @model.json_attrs.presence || {} rescue {}
|
|
279
281
|
end
|
|
280
282
|
|
|
281
283
|
def extract_model
|
|
@@ -87,14 +87,15 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
|
|
|
87
87
|
scope
|
|
88
88
|
end
|
|
89
89
|
|
|
90
|
+
# Sorts only by fields the model lets Ransack sort by, like `filter`: ordering by a hidden secret
|
|
91
|
+
# would reveal it (host Odoo #6313). Other fields are ignored.
|
|
90
92
|
def apply_sorting(scope)
|
|
91
93
|
return scope if params[:sort].blank?
|
|
94
|
+
sortable = @model.ransortable_attributes & @model.column_names # a ransacker is no column to order by
|
|
92
95
|
params[:sort].to_s.split(",").each do |field|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
scope = scope.order(field => :asc)
|
|
97
|
-
end
|
|
96
|
+
name, direction = field.start_with?("-") ? [field[1..], :desc] : [field, :asc]
|
|
97
|
+
next unless sortable.include?(name)
|
|
98
|
+
scope = scope.order(name => direction)
|
|
98
99
|
end
|
|
99
100
|
scope
|
|
100
101
|
end
|
|
@@ -27,6 +27,17 @@ module AutoIncludeJson
|
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
# What to preload and the options to serialize with. Options from the request (a / json_attrs)
|
|
31
|
+
# preload only real associations of +klass+ and keep their marks (Api::V2::RequestJsonAttrs);
|
|
32
|
+
# the code's own options go through build_includes / sanitize_includes as before.
|
|
33
|
+
def includes_and_options(klass, options)
|
|
34
|
+
if Api::V2::RequestJsonAttrs.marked?(options)
|
|
35
|
+
[Api::V2::RequestJsonAttrs.preloads(klass, options[:include]), options]
|
|
36
|
+
else
|
|
37
|
+
[build_includes(options[:include]), options.merge(include: sanitize_includes(options[:include]))]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
30
41
|
# Recursively sanitize include options to ensure only, except, methods propagate
|
|
31
42
|
def sanitize_includes(include_option)
|
|
32
43
|
case include_option
|
|
@@ -60,9 +71,7 @@ module AutoIncludeRelationJson
|
|
|
60
71
|
Rails.logger.debug "AutoIncludeJson - Relation JSON to_json from #{caller_locations(1,1).first.label}"
|
|
61
72
|
return super unless options.is_a?(Hash) && options[:include]
|
|
62
73
|
|
|
63
|
-
includes_hash =
|
|
64
|
-
sanitized_includes = sanitize_includes(options[:include])
|
|
65
|
-
options = options.merge(include: sanitized_includes)
|
|
74
|
+
includes_hash, options = includes_and_options(klass, options)
|
|
66
75
|
|
|
67
76
|
self.includes(includes_hash).load.to_a.to_json(options)
|
|
68
77
|
end
|
|
@@ -71,9 +80,7 @@ module AutoIncludeRelationJson
|
|
|
71
80
|
Rails.logger.debug "AutoIncludeJson - Relation JSON as_json from #{caller_locations(1,1).first.label}"
|
|
72
81
|
return super unless options.is_a?(Hash) && options[:include]
|
|
73
82
|
|
|
74
|
-
includes_hash =
|
|
75
|
-
sanitized_includes = sanitize_includes(options[:include])
|
|
76
|
-
options = options.merge(include: sanitized_includes)
|
|
83
|
+
includes_hash, options = includes_and_options(klass, options)
|
|
77
84
|
|
|
78
85
|
self.includes(includes_hash).load.to_a.as_json(options)
|
|
79
86
|
end
|
|
@@ -99,15 +106,14 @@ module AutoIncludeInstanceJson
|
|
|
99
106
|
Rails.logger.debug "AutoIncludeJson - Instance JSON preload_and_sanitize from #{caller_locations(1,1).first.label}"
|
|
100
107
|
return options unless options.is_a?(Hash) && options[:include]
|
|
101
108
|
|
|
102
|
-
includes_hash =
|
|
103
|
-
sanitized_includes = sanitize_includes(options[:include])
|
|
109
|
+
includes_hash, options = includes_and_options(self.class, options)
|
|
104
110
|
|
|
105
111
|
ActiveRecord::Associations::Preloader.new(
|
|
106
112
|
records: [self],
|
|
107
113
|
associations: includes_hash
|
|
108
114
|
).call
|
|
109
115
|
|
|
110
|
-
options
|
|
116
|
+
options
|
|
111
117
|
end
|
|
112
118
|
|
|
113
119
|
end
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Serialization options sent by the client (a / json_attrs) follow the conventions of
|
|
2
|
+
# Api::V2::RequestJsonAttrs (host Odoo #6285); options built by the code are untouched.
|
|
3
|
+
# Prepended to the module (Ruby >= 3.0 reaches the classes that already include it): records and
|
|
4
|
+
# the ActiveModel objects a custom action returns.
|
|
5
|
+
ActiveModel::Serialization.prepend(Api::V2::RequestJsonAttrs::Serialization)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module Api
|
|
2
|
+
# The API-hidden secrets of a model (host Odoo #6313, ADR 0006): its columns named like a secret
|
|
3
|
+
# (Rails' filter_parameters convention) that the model already hides in some way: left out of its
|
|
4
|
+
# default serializable_hash (Devise's encrypted_password), listed in its own json_attrs[:except]
|
|
5
|
+
# (thecore_spot_overrides' encrypted_access_token), or declared in write_only_attributes. They are
|
|
6
|
+
# never readable through the API: not in v3 attributes, not through v2 request options, not
|
|
7
|
+
# through Ransack filters or sorts. A secret-named column the model doesn't hide stays readable.
|
|
8
|
+
module HiddenSecrets
|
|
9
|
+
# Names Rails treats as secrets, on top of the host's own filter_parameters.
|
|
10
|
+
SECRET_NAMES = %i[passw secret token _key crypt salt certificate otp ssn cvv cvc].freeze
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# Column names (Strings), memoized per class.
|
|
15
|
+
def for(klass)
|
|
16
|
+
return [] unless klass.is_a?(Class) && klass < ActiveRecord::Base && !klass.abstract_class?
|
|
17
|
+
|
|
18
|
+
(@hidden ||= Concurrent::Map.new).compute_if_absent(klass) do
|
|
19
|
+
write_only = klass.respond_to?(:write_only_attributes) ? Array(klass.write_only_attributes).map(&:to_s) : []
|
|
20
|
+
hidden_columns = left_out_by_default(klass) | excepted_by_json_attrs(klass)
|
|
21
|
+
# write_only_attributes are secrets by declaration, whatever their name.
|
|
22
|
+
(hidden_columns.select { |column| secret_name?(column) } | write_only).freeze
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def secret_name?(name)
|
|
27
|
+
@secret_filter ||= ActiveSupport::ParameterFilter.new(SECRET_NAMES + Rails.application.config.filter_parameters)
|
|
28
|
+
@secret_filter.filter(name.to_s => "x")[name.to_s] != "x"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def left_out_by_default(klass)
|
|
32
|
+
klass.column_names - klass.new.serializable_hash.keys
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
Rails.logger.warn("[model_driven_api] HiddenSecrets: #{klass.name}.new failed (#{e.class}), " \
|
|
35
|
+
"secrets hidden by its serializable_hash are not detected")
|
|
36
|
+
[]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def excepted_by_json_attrs(klass)
|
|
40
|
+
attrs = klass.respond_to?(:json_attrs) ? klass.json_attrs : nil
|
|
41
|
+
attrs.is_a?(Hash) ? Array(attrs[:except] || attrs["except"]).map(&:to_s) : []
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/api/open_api/v3.rb
CHANGED
|
@@ -232,7 +232,7 @@ module Api
|
|
|
232
232
|
end
|
|
233
233
|
|
|
234
234
|
id_param = { "name" => "id", "in" => "path", "required" => true, "schema" => { "type" => "integer" } }
|
|
235
|
-
sort_param = { "name" => "sort", "in" => "query", "schema" => { "type" => "string" }, "description" => "field or -field (descending), comma-separated" }
|
|
235
|
+
sort_param = { "name" => "sort", "in" => "query", "schema" => { "type" => "string" }, "description" => "field or -field (descending), comma-separated; only the model's ransortable attributes, others are ignored" }
|
|
236
236
|
page_params = [
|
|
237
237
|
{ "name" => "page[number]", "in" => "query", "schema" => { "type" => "integer" } },
|
|
238
238
|
{ "name" => "page[size]", "in" => "query", "schema" => { "type" => "integer" } },
|
|
@@ -336,7 +336,7 @@ module Api
|
|
|
336
336
|
|
|
337
337
|
### Sorting
|
|
338
338
|
|
|
339
|
-
`?sort=field` (asc) · `?sort=-field` (desc) · `?sort=field1,-field2` (multi-field).
|
|
339
|
+
`?sort=field` (asc) · `?sort=-field` (desc) · `?sort=field1,-field2` (multi-field) — validated against each model's `ransortable_attributes`; other fields are ignored.
|
|
340
340
|
|
|
341
341
|
### Pagination
|
|
342
342
|
|
|
@@ -7,9 +7,9 @@ module Api
|
|
|
7
7
|
except = Array(jattrs[:except]).map(&:to_sym)
|
|
8
8
|
only = model_class.column_names.map(&:to_sym) - except
|
|
9
9
|
end
|
|
10
|
-
# A model's
|
|
11
|
-
# json_attrs asks (host Odoo #6084).
|
|
12
|
-
only -=
|
|
10
|
+
# A model's API-hidden secrets (Devise's encrypted_password, write_only_attributes such as
|
|
11
|
+
# LdapServer#admin_password) are never serialized, whatever json_attrs asks (host Odoo #6084, #6313).
|
|
12
|
+
only -= Api::HiddenSecrets.for(model_class).map(&:to_sym)
|
|
13
13
|
new(only.reject { |a| a == :id }, Array(jattrs[:methods]).map(&:to_sym), jattrs[:include])
|
|
14
14
|
end
|
|
15
15
|
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
module Api
|
|
2
|
+
module V2
|
|
3
|
+
# Serialization options a client sends in the query string (`a`, alias `json_attrs`) replace the
|
|
4
|
+
# model's own json_attrs, and Rails runs `send` on every `methods` name and `include` key. These
|
|
5
|
+
# options follow conventions instead of lists (ADR 0006, host Odoo #6285):
|
|
6
|
+
#
|
|
7
|
+
# - `include` keeps only real associations of each record's class;
|
|
8
|
+
# - attributes the class hides by default (column_names minus a new record's default
|
|
9
|
+
# serializable_hash, e.g. Devise's encrypted_password, LdapServer#admin_password) never come back;
|
|
10
|
+
# - a `methods` name declared in the class's json_attrs[:methods] runs as before (a developer
|
|
11
|
+
# exposed it); a column or association reader (or `<assoc>_ids`) is a plain read; any other
|
|
12
|
+
# name must be public, end in neither `!` nor `=`, take no required argument and be written in
|
|
13
|
+
# the host or in a thecore gem (a gem named thecore* or depending on one), and it runs under
|
|
14
|
+
# the read-only guard: if it writes, or raises, only that field is dropped.
|
|
15
|
+
#
|
|
16
|
+
# A dropped name is logged once per request and the response stays 200. Options built by the
|
|
17
|
+
# code (a model's json_attrs, a custom action's @json_attrs) carry no MARK and are untouched.
|
|
18
|
+
module RequestJsonAttrs
|
|
19
|
+
MARK = :_from_request
|
|
20
|
+
|
|
21
|
+
module_function
|
|
22
|
+
|
|
23
|
+
# The client's options, with MARK at every level the serializer will read.
|
|
24
|
+
def from_params(options)
|
|
25
|
+
mark(options.deep_symbolize_keys)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def mark(options)
|
|
29
|
+
options = options.merge(MARK => true)
|
|
30
|
+
includes = normalize_includes(options[:include])
|
|
31
|
+
options[:include] = includes.transform_values { |nested| mark(nested) } if options.key?(:include)
|
|
32
|
+
options
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def marked?(options)
|
|
36
|
+
options.is_a?(Hash) && options[MARK]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# `include` as { assoc => options }, whatever form the client sent (symbol, string, array,
|
|
40
|
+
# hash). Not ResourceAttributeSet#parsed_includes: that one reads the model's own json_attrs
|
|
41
|
+
# (symbols, nil specs); request params arrive as strings and must come out as option hashes.
|
|
42
|
+
def normalize_includes(include_option)
|
|
43
|
+
case include_option
|
|
44
|
+
when Hash then include_option.to_h { |k, v| [k.to_sym, v.is_a?(Hash) ? v.deep_symbolize_keys : {}] }
|
|
45
|
+
when Array then include_option.each_with_object({}) { |i, h| h.merge!(normalize_includes(i)) }
|
|
46
|
+
when String, Symbol then {include_option.to_sym => {}}
|
|
47
|
+
else {}
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The options Rails should serialize a record of +klass+ with, and the methods to evaluate
|
|
52
|
+
# under the read-only guard.
|
|
53
|
+
def for_class(klass, options)
|
|
54
|
+
hidden = Api::HiddenSecrets.for(klass)
|
|
55
|
+
rewritten = options.except(MARK)
|
|
56
|
+
rewritten[:only] = Array(options[:only]).map(&:to_s) - hidden if options.key?(:only)
|
|
57
|
+
rewritten[:except] = Array(options[:except]).map(&:to_s) | hidden
|
|
58
|
+
if options.key?(:include)
|
|
59
|
+
rewritten[:include] = normalize_includes(options[:include]).select do |name, _|
|
|
60
|
+
next true if active_record?(klass) && klass.reflect_on_association(name)
|
|
61
|
+
|
|
62
|
+
drop(klass, name, "not an association")
|
|
63
|
+
false
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
plain, guarded = [], []
|
|
67
|
+
Array(options[:methods]).map(&:to_s).uniq.each do |name|
|
|
68
|
+
case (kind = method_kind(klass, name))
|
|
69
|
+
when :declared, :reader then plain << name
|
|
70
|
+
when :guarded then guarded << name
|
|
71
|
+
else drop(klass, name, kind)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
rewritten[:methods] = plain
|
|
75
|
+
[rewritten, guarded]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# :declared, :reader, :guarded, or the reason it is refused (a String). Cached per class and
|
|
79
|
+
# name, but only for names the class defines: the client chooses the names, so caching any
|
|
80
|
+
# name would let it grow memory without bound.
|
|
81
|
+
def method_kind(klass, name)
|
|
82
|
+
return "not a public method" unless klass.method_defined?(name) || klass.private_method_defined?(name)
|
|
83
|
+
|
|
84
|
+
(@method_kinds ||= Concurrent::Map.new).compute_if_absent([klass, name]) { classify(klass, name) }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def classify(klass, name)
|
|
88
|
+
return "hidden attribute" if Api::HiddenSecrets.for(klass).include?(name)
|
|
89
|
+
return :declared if declared_methods(klass).include?(name)
|
|
90
|
+
return :reader if reader?(klass, name)
|
|
91
|
+
return "ends in ! or =" if name.end_with?("!", "=")
|
|
92
|
+
return "not a public method" unless klass.public_method_defined?(name)
|
|
93
|
+
|
|
94
|
+
method = klass.instance_method(name)
|
|
95
|
+
return "takes required arguments" if method.parameters.any? { |type, _| %i[req keyreq].include?(type) }
|
|
96
|
+
return "not written in the app or a thecore gem" unless our_code?(method.source_location&.first)
|
|
97
|
+
|
|
98
|
+
:guarded
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def declared_methods(klass)
|
|
102
|
+
return [] unless klass.respond_to?(:json_attrs) && klass.json_attrs.is_a?(Hash)
|
|
103
|
+
|
|
104
|
+
Api::ResourceAttributeSet.for(klass).methods_list.map(&:to_s)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def active_record?(klass) = klass < ActiveRecord::Base
|
|
108
|
+
|
|
109
|
+
def reader?(klass, name)
|
|
110
|
+
return false unless active_record?(klass)
|
|
111
|
+
return true if klass.column_names.include?(name)
|
|
112
|
+
return true if klass.reflect_on_association(name)
|
|
113
|
+
|
|
114
|
+
klass.reflect_on_all_associations.any? { |r| r.collection? && "#{r.name.to_s.singularize}_ids" == name }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Written in the host app or in a thecore gem. The gem that owns the file decides first:
|
|
118
|
+
# deploy images install the bundle inside the app (/app/vendor/bundle), so "under Rails.root"
|
|
119
|
+
# alone would make Rails, Devise and every other gem ours.
|
|
120
|
+
def our_code?(file)
|
|
121
|
+
return false if file.blank?
|
|
122
|
+
|
|
123
|
+
owner = Gem.loaded_specs.values
|
|
124
|
+
.select { |spec| file.start_with?(File.join(spec.full_gem_path, "")) }
|
|
125
|
+
.max_by { |spec| spec.full_gem_path.length }
|
|
126
|
+
return thecore_gem?(owner.name) if owner
|
|
127
|
+
return false if gem_dirs.any? { |dir| file.start_with?(dir) }
|
|
128
|
+
|
|
129
|
+
file.start_with?(File.join(Rails.root.to_s, ""))
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# A gem named thecore* or depending, directly or not, on one. Memoized per gem name.
|
|
133
|
+
def thecore_gem?(name, seen = Set.new)
|
|
134
|
+
cache = (@thecore_gems ||= Concurrent::Map.new)
|
|
135
|
+
return cache[name] if cache.key?(name)
|
|
136
|
+
return false unless seen.add?(name) # a dependency cycle: decided by the other branches
|
|
137
|
+
|
|
138
|
+
spec = Gem.loaded_specs[name]
|
|
139
|
+
found = !spec.nil? &&
|
|
140
|
+
(name.start_with?("thecore") || spec.runtime_dependencies.any? { |dep| thecore_gem?(dep.name, seen) })
|
|
141
|
+
cache.put_if_absent(name, found)
|
|
142
|
+
found
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Where gems live, even those no spec claims (default gems, a vendored bundle).
|
|
146
|
+
def gem_dirs
|
|
147
|
+
@gem_dirs ||= (Gem.path + [Bundler.bundle_path.to_s]).map { |dir| File.join(dir, "") }.uniq.freeze
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# A name the client asked for that is not served: logged once per request.
|
|
151
|
+
def drop(klass, name, reason)
|
|
152
|
+
seen = (ModelDrivenApi::Current.dropped_json_attrs ||= Set.new)
|
|
153
|
+
return unless seen.add?([klass.name, name, reason])
|
|
154
|
+
|
|
155
|
+
Rails.logger.warn("[model_driven_api] a/json_attrs: #{klass.name}##{name} dropped (#{reason})")
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Prepended to ActiveModel::Serialization (records and the ActiveModel objects a custom action
|
|
159
|
+
# may return): applies the conventions to marked options only.
|
|
160
|
+
module Serialization
|
|
161
|
+
def serializable_hash(options = nil)
|
|
162
|
+
return super unless RequestJsonAttrs.marked?(options)
|
|
163
|
+
|
|
164
|
+
rewritten, guarded = RequestJsonAttrs.for_class(self.class, options)
|
|
165
|
+
hash = super(rewritten)
|
|
166
|
+
guarded.each do |name|
|
|
167
|
+
# A refused write may already have changed the record in memory (update_column does):
|
|
168
|
+
# restore it so neither the next field nor the caller sees a value that was never saved.
|
|
169
|
+
snapshot = @attributes.deep_dup if is_a?(ActiveRecord::Base)
|
|
170
|
+
hash[name] = ActiveRecord::Base.while_preventing_writes { send(name) }
|
|
171
|
+
rescue StandardError => e
|
|
172
|
+
@attributes = snapshot if is_a?(ActiveRecord::Base)
|
|
173
|
+
RequestJsonAttrs.drop(self.class, name, "raised #{e.class}")
|
|
174
|
+
end
|
|
175
|
+
hash
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# The associations of +klass+ worth preloading for marked options: real ones only, nested
|
|
180
|
+
# through non-polymorphic reflections.
|
|
181
|
+
def preloads(klass, include_option)
|
|
182
|
+
normalize_includes(include_option).each_with_object({}) do |(name, nested), out|
|
|
183
|
+
reflection = klass.reflect_on_association(name) or next
|
|
184
|
+
out[name] = (reflection.polymorphic? || !nested.key?(:include)) ? {} : preloads(reflection.klass, nested[:include])
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
@@ -3,8 +3,10 @@ module ModelDrivenApiApplicationRecord
|
|
|
3
3
|
|
|
4
4
|
# Fixes: https://stackoverflow.com/a/76781489
|
|
5
5
|
included do
|
|
6
|
+
# Every column but the API-hidden secrets (Api::HiddenSecrets, host Odoo #6313): filtering or
|
|
7
|
+
# sorting on a hash would reveal it a character at a time.
|
|
6
8
|
def self.ransackable_attributes(auth_object = nil)
|
|
7
|
-
column_names + _ransackers.keys
|
|
9
|
+
column_names + _ransackers.keys - Api::HiddenSecrets.for(self)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
def self.ransackable_associations(auth_object = nil)
|
data/lib/model_driven_api.rb
CHANGED
|
@@ -22,10 +22,13 @@ require 'concerns/api_exception_management'
|
|
|
22
22
|
require 'deep_merge/rails_compat'
|
|
23
23
|
|
|
24
24
|
require "model_driven_api/engine"
|
|
25
|
+
require "model_driven_api/current"
|
|
25
26
|
|
|
26
27
|
require "safe_sql_executor"
|
|
27
28
|
require "raw_sql_grants"
|
|
29
|
+
require "api/hidden_secrets"
|
|
28
30
|
require "api/resource_attribute_set"
|
|
31
|
+
require "api/v2/request_json_attrs"
|
|
29
32
|
require "api/model_resolver"
|
|
30
33
|
require "api/custom_action_dispatcher"
|
|
31
34
|
require "api/open_api/base"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: model_driven_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.19.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: thecore_backend_commons
|
|
@@ -238,6 +238,7 @@ files:
|
|
|
238
238
|
- config/initializers/cors_api_thecore.rb
|
|
239
239
|
- config/initializers/default_json_attrs_registration.rb
|
|
240
240
|
- config/initializers/knock.rb
|
|
241
|
+
- config/initializers/request_json_attrs.rb
|
|
241
242
|
- config/initializers/time_with_zone.rb
|
|
242
243
|
- config/initializers/wrap_parameters.rb
|
|
243
244
|
- config/routes.rb
|
|
@@ -245,11 +246,13 @@ files:
|
|
|
245
246
|
- db/migrate/20210528111450_rename_valid_to_is_valid_in_used_token.rb
|
|
246
247
|
- db/migrate/20261125120000_apply_thecore_raw_grants.rb
|
|
247
248
|
- lib/api/custom_action_dispatcher.rb
|
|
249
|
+
- lib/api/hidden_secrets.rb
|
|
248
250
|
- lib/api/model_resolver.rb
|
|
249
251
|
- lib/api/open_api/base.rb
|
|
250
252
|
- lib/api/open_api/v2.rb
|
|
251
253
|
- lib/api/open_api/v3.rb
|
|
252
254
|
- lib/api/resource_attribute_set.rb
|
|
255
|
+
- lib/api/v2/request_json_attrs.rb
|
|
253
256
|
- lib/api/v3/serializer_factory.rb
|
|
254
257
|
- lib/concerns/api_exception_management.rb
|
|
255
258
|
- lib/concerns/model_driven_api_application_record.rb
|
|
@@ -261,6 +264,7 @@ files:
|
|
|
261
264
|
- lib/endpoint_validation_error.rb
|
|
262
265
|
- lib/json_web_token.rb
|
|
263
266
|
- lib/model_driven_api.rb
|
|
267
|
+
- lib/model_driven_api/current.rb
|
|
264
268
|
- lib/model_driven_api/engine.rb
|
|
265
269
|
- lib/model_driven_api/version.rb
|
|
266
270
|
- lib/non_crud_endpoints.rb
|