model_driven_api 3.13.1 → 3.13.3

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: 9f71f26da0653883d6bcf4aa2382a9d6d9d51104e0f5f183f79abdf6da8cf242
4
- data.tar.gz: 1cb5c008dfe9bbc44dbec5ec2bcf19670cd2c469239b0416b3a27d3ee4387387
3
+ metadata.gz: c8ede5f2efa0de88e8f2a741fe11a227781d71b93b07e00d8fbf8513c8f4aab5
4
+ data.tar.gz: 50872d9fae7444384e9bf44b4ec1c80a3c2ed1d34ebe56563686d484392ac204
5
5
  SHA512:
6
- metadata.gz: 4407253720ea30dbd1c209fe9997461564c28035ab24b7e97bb60ccdfb1614000f8405adc94f97666d06d1799a11a5aee5f5c2e498d105a8c2c88f56c32bc007
7
- data.tar.gz: 734ca5f9bc6745ebcd0ef1ef382048d3d35205e1a692bdcc6fa41ed6c2480653e590bc25bf76ad29b32b9aae27392d065ecbb45294a9da8de87e8be402114ade
6
+ metadata.gz: 16047eaa6f216ff7d30719b8c4a2abbba69c91cfe2881422953645c6b9b6e00d04351d287340910e13a86004a5bc71442313d5a4985e0228355f06344e24df64
7
+ data.tar.gz: 7acac853aa69188cf1f28fe1a06a99fa33bd339c7d0990d06e9e361d5d0b1475bfcdc6c283bed52c6bfad98d52cea12add7945b1e2de671fb46efda0ee7dcf8a
data/README.md CHANGED
@@ -461,10 +461,6 @@ simplest safe default. A model with its own `Api::ModelName` concern (or `ModelD
461
461
  freely overrides or merges on top of it via `ModelDrivenApi.smart_merge`. Only a model with no
462
462
  concern at all keeps the bare default.
463
463
 
464
- **Temporary dependency note**: this currently requires `thecore_backend_commons` from a Gemfile
465
- `git:` pin to its `release/3` branch, since the RubyGems release hasn't caught up with the
466
- registry yet — see this repo's `CLAUDE.md` for the exact commit/removal condition.
467
-
468
464
  ---
469
465
 
470
466
  ## Raw SQL endpoint
@@ -9,6 +9,9 @@ class Api::V2::ApplicationController < ActionController::API
9
9
 
10
10
  before_action :authenticate_request
11
11
  before_action :extract_model
12
+ # Before anything is saved: create, update and custom actions read json_attrs only after
13
+ # their side effects.
14
+ before_action :reject_non_hash_json_attrs, if: :reads_json_attrs?
12
15
  before_action :find_record, only: [:show, :destroy, :update, :patch]
13
16
 
14
17
  # GET :controller/
@@ -249,11 +252,29 @@ class Api::V2::ApplicationController < ActionController::API
249
252
  render json: existing.to_json(json_attrs), status: 200
250
253
  end
251
254
 
255
+ # The request parameters json_attrs reads its options from: `a` is the short alias.
256
+ JSON_ATTRS_PARAMS = %i[a json_attrs].freeze
257
+
258
+ # The actions that serialize through json_attrs: every custom action, and the CRUD ones that
259
+ # render the record(s). A new action calling json_attrs belongs here, or a scalar `a` 500s it.
260
+ def reads_json_attrs?
261
+ custom_action? || %w[index show create update patch].include?(action_name)
262
+ end
263
+
264
+ # `a` and `json_attrs` carry serialization options as a hash (?a[only][]=id); a scalar or an
265
+ # array answers 400 naming the parameter, in every environment (the ApiExceptionManagement
266
+ # handlers are production-only).
267
+ def reject_non_hash_json_attrs
268
+ name = JSON_ATTRS_PARAMS.find { |key| params[key].present? && !params[key].is_a?(Hash) }
269
+ api_error(status: 400, errors: "The #{name} parameter must be a hash, e.g. #{name}[only][]=id") if name
270
+ end
271
+
252
272
  def json_attrs
253
273
  # In order of importance: if you send the configuration via querystring you are ok
254
274
  # has precedence over if you have setup the json_attrs in the model concern
255
- from_params = params[:a].deep_symbolize_keys unless params[:a].blank?
256
- from_params = params[:json_attrs].deep_symbolize_keys unless params[:json_attrs].blank?
275
+ # (JSON_ATTRS_PARAMS order: json_attrs wins over a; reject_non_hash_json_attrs ran first).
276
+ from_params = nil
277
+ JSON_ATTRS_PARAMS.each { |key| from_params = params[key].deep_symbolize_keys if params[key].present? }
257
278
  from_params.presence || @json_attrs.presence || @model.json_attrs.presence || {} rescue {}
258
279
  end
259
280
 
@@ -65,6 +65,12 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
65
65
 
66
66
  private
67
67
 
68
+ # v3 serializes through SerializerFactory and never reads `a`/`json_attrs`; only its custom
69
+ # actions (plain JSON, via check_for_custom_action) do.
70
+ def reads_json_attrs?
71
+ custom_action?
72
+ end
73
+
68
74
  # v2's read check, with a JSON:API envelope.
69
75
  def render_existing(existing)
70
76
  authorize! :show, existing
@@ -84,29 +84,30 @@ module AutoIncludeInstanceJson
84
84
  include AutoIncludeJson
85
85
 
86
86
  def to_json(options = nil)
87
- preload_and_sanitize!(options)
88
- super
87
+ super(preload_and_sanitize(options))
89
88
  end
90
89
 
91
90
  def as_json(options = nil)
92
- preload_and_sanitize!(options)
93
- super
91
+ super(preload_and_sanitize(options))
94
92
  end
95
93
 
96
94
  private
97
95
 
98
- def preload_and_sanitize!(options)
99
- Rails.logger.debug "AutoIncludeJson - Instance JSON preload_and_sanitize! from #{caller_locations(1,1).first.label}"
100
- return unless options.is_a?(Hash) && options[:include]
96
+ # Returns a sanitized copy of the options, never mutating them: Rails 8.1's JSON encoder hands
97
+ # as_json a frozen options hash, and the caller's hash is often a model's shared json_attrs.
98
+ def preload_and_sanitize(options)
99
+ Rails.logger.debug "AutoIncludeJson - Instance JSON preload_and_sanitize from #{caller_locations(1,1).first.label}"
100
+ return options unless options.is_a?(Hash) && options[:include]
101
101
 
102
102
  includes_hash = build_includes(options[:include])
103
103
  sanitized_includes = sanitize_includes(options[:include])
104
- options[:include] = sanitized_includes
105
104
 
106
105
  ActiveRecord::Associations::Preloader.new(
107
106
  records: [self],
108
107
  associations: includes_hash
109
108
  ).call
109
+
110
+ options.merge(include: sanitized_includes)
110
111
  end
111
112
 
112
113
  end
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.13.1".freeze
2
+ VERSION = "3.13.3".freeze
3
3
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_driven_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.13.1
4
+ version: 3.13.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: thecore_backend_commons
@@ -230,6 +231,7 @@ licenses:
230
231
  - MIT
231
232
  metadata:
232
233
  allowed_push_host: https://rubygems.org
234
+ post_install_message:
233
235
  rdoc_options: []
234
236
  require_paths:
235
237
  - lib
@@ -244,7 +246,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
246
  - !ruby/object:Gem::Version
245
247
  version: '0'
246
248
  requirements: []
247
- rubygems_version: 3.6.9
249
+ rubygems_version: 3.5.22
250
+ signing_key:
248
251
  specification_version: 4
249
252
  summary: Convention based RoR engine which uses DB schema introspection to create
250
253
  REST APIs.