jpie 4.1.7.pre.202611160310 → 4.1.8

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: 4a8182611c87fbbd3dd1147370c1d14277a83ea0ba4d2c22584b29f1a458e8d3
4
- data.tar.gz: bd63fa39d74753b8e61fca9fdefb8f9803c664430ecafcd28fdb107ca4fd177b
3
+ metadata.gz: d42b5ce17662c2c8b8562e0948d6bba493823a9cd65e1d43ce79c2864d395a38
4
+ data.tar.gz: f968fca6f255f5949a20f1e2a99f70f34f9e3e620dd6e71dad16348e136cb48f
5
5
  SHA512:
6
- metadata.gz: 1b84821f469cb2b24613275e5114b2b0ae27bcaefd4cd87920c08e6e6fff8b5d23f8f577ea65ea8d75ac5e87834f970a287c515252da83514b45a4c69254b62e
7
- data.tar.gz: 9345855f8ea242701b3dbbe99c453dcfabbf6d14a09c24e3f1f9fe0b2d14b4c7cc5c24484426b6453cc873c4afa026666841fe6e5d9beafcadb6dc2ba3d372c8
6
+ metadata.gz: '093d9ab5bc2c337b7a8829aa7918fc6b174d7db815ec87b14b7460dbd41cd2c44e1da755d47c569314bbd4838b3b0c53b8ef9159508599035d42e1341b392ca1'
7
+ data.tar.gz: f3c12becc5f46da8061360f859d4b49e50f63ca6e3a4a2c4b720959c3969b8cb09c31c34281019f2cc5245ab9276a9bd02d7aeb954fc3325ba72fb1b49d592fa
data/CHANGELOG.md CHANGED
@@ -7,6 +7,36 @@ at release time.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [4.1.8] - 2026-11-20
11
+
12
+ ### Fixed
13
+
14
+ - **Resolve relationship write targets through `authorization_scope`.** A
15
+ relationship write named its target with the bare model relation. A write
16
+ body named its target with the raw key. Both now use the
17
+ `authorization_scope` hook. A target that the hook hides gets the same answer
18
+ as a target that does not exist. A write body that names a target costs one
19
+ more query. Regression specs:
20
+ `authorization_scope_relationship_write_target_spec.rb` and
21
+ `authorization_scope_body_write_target_spec.rb`.
22
+ - **Keep the relationship skip after a controller rebuild.** A rebuild loaded
23
+ the controllers before the app base class held the concerns. Rails then lost
24
+ the relationship controller's skip of the id and type checks, and a second
25
+ relationship PATCH in one process answered 409. The app base class now gets
26
+ the concerns before the controllers load. Regression spec:
27
+ `relationship_update_after_controller_reload_spec.rb`.
28
+
29
+ ## [4.1.7] - 2026-11-18
30
+
31
+ ### Fixed
32
+
33
+ - **Keep the owner filter on a lazily read include.** A create response reads an
34
+ include through the association scope merged with the `authorization_scope`
35
+ result. When the hook filtered the owner column with a subquery, the merge
36
+ replaced the owner filter. The include then listed every record the hook
37
+ allowed, not only the records of the parent. The owner filter now goes back on
38
+ after the merge. Regression spec: `include_scope_keeps_association_filter_spec.rb`.
39
+
10
40
  ## [4.1.6] - 2026-11-13
11
41
 
12
42
  ### Fixed
@@ -60,6 +60,7 @@ module JSONAPI
60
60
  association:,
61
61
  resource_class: @resource_class,
62
62
  relationship_name: @relationship_name,
63
+ controller: self,
63
64
  )
64
65
  return unless collection_ids.include?(resource.id)
65
66
 
@@ -62,6 +62,7 @@ module JSONAPI
62
62
  association:,
63
63
  resource_class: @resource_class,
64
64
  relationship_name: @relationship_name,
65
+ controller: self,
65
66
  )
66
67
  end
67
68
  end
@@ -63,7 +63,8 @@ module JSONAPI
63
63
  def deserialize_params(action = :update, model_class: nil, data: nil, definition: nil)
64
64
  target = model_class || self.model_class
65
65
  payload = data.nil? ? raw_jsonapi_data : symbolize_data(data)
66
- JSONAPI::Deserializer.new(payload, model_class: target, action:, definition:).to_model_attributes
66
+ attrs = JSONAPI::Deserializer.new(payload, model_class: target, action:, definition:).to_model_attributes
67
+ JSONAPI::ResourceIdentifier.withhold_hidden_targets(attrs, target, self)
67
68
  end
68
69
 
69
70
  def resource_url(resource)
@@ -142,12 +142,7 @@ module JSONAPI
142
142
  def rebuild_controllers_if_needed(base_controller_class)
143
143
  return if JSONAPI::BaseController.superclass == base_controller_class
144
144
 
145
- JSONAPI.send(:remove_const, :BaseController)
146
- load "json_api/controllers/base_controller.rb"
147
- JSONAPI.send(:remove_const, :RelationshipsController)
148
- load "json_api/controllers/relationships_controller.rb"
149
- JSONAPI.send(:remove_const, :ResourcesController)
150
- load "json_api/controllers/resources_controller.rb"
145
+ JSONAPI.rebuild_base_controllers!
151
146
  end
152
147
 
153
148
  def include_jsonapi_concerns(base_controller_class)
@@ -113,7 +113,10 @@ module JSONAPI
113
113
 
114
114
  def build_scoped_relation(related_klass, association)
115
115
  related_base_scope = ResourceLoader.find_for_model(related_klass, namespace:).records
116
- association.scope.merge(apply_include_authorization(related_base_scope, related_klass))
116
+ owned = association.scope
117
+ # A merge replaces the owner filter when the hook filters the same column, so the owner
118
+ # filter goes back on after it. Without it the include returns every record the hook allows.
119
+ owned.merge(apply_include_authorization(related_base_scope, related_klass)).where(owned.where_clause.ast)
117
120
  end
118
121
 
119
122
  # A sideloaded `?include=` is loaded through acts_as_tenant alone, bypassing the
@@ -41,12 +41,15 @@ module JSONAPI
41
41
  ResourceIdentifier.extract_type(identifier)
42
42
  end
43
43
 
44
- def resolve_and_find_related_resource(identifier, association:, resource_class:, relationship_name:)
44
+ def resolve_and_find_related_resource(
45
+ identifier, association:, resource_class:, relationship_name:, controller: nil
46
+ )
45
47
  ResourceIdentifier.resolve_and_find_related_record(
46
48
  identifier,
47
49
  association:,
48
50
  definition: resource_class,
49
51
  relationship_name:,
52
+ controller:,
50
53
  )
51
54
  end
52
55
 
@@ -21,7 +21,9 @@ module JSONAPI
21
21
  identifier[:type]
22
22
  end
23
23
 
24
- def resolve_and_find_related_record(identifier, association:, definition:, relationship_name:)
24
+ # With a controller, the target comes through the configured authorization_scope hook, as every
25
+ # read does. A target the caller's scope hides then fails like a target that does not exist.
26
+ def resolve_and_find_related_record(identifier, association:, definition:, relationship_name:, controller: nil)
25
27
  type = extract_type(identifier)
26
28
  id = extract_id(identifier)
27
29
  raise ArgumentError, "Missing type or id in relationship data" unless type && id
@@ -29,7 +31,34 @@ module JSONAPI
29
31
  is_polymorphic = polymorphic_association?(definition, relationship_name)
30
32
  validate_relationship_type!(type, association, definition) unless is_polymorphic
31
33
 
32
- find_related_record(type, id, association, is_polymorphic)
34
+ find_related_record(type, id, association, is_polymorphic, controller:)
35
+ end
36
+
37
+ # A write body assigns the raw foreign key and leaves the lookup to the model. A target the
38
+ # caller's scope hides must act like a target that does not exist. So the key becomes nil, and
39
+ # the model answers the same for a hidden id and a missing id.
40
+ def withhold_hidden_targets(attrs, model_class, controller)
41
+ return attrs unless controller && JSONAPI.configuration.authorization_scope
42
+
43
+ model_class.reflect_on_all_associations(:belongs_to).each do |association|
44
+ next if association.polymorphic?
45
+
46
+ key = association.foreign_key.to_s
47
+ attrs[key] = nil unless attrs[key].nil? || visible_target?(association, attrs[key], controller)
48
+ end
49
+ attrs
50
+ end
51
+
52
+ def visible_target?(association, id, controller)
53
+ related_class = association.klass
54
+ scope = authorized_related_scope(target_records(related_class), related_class, controller)
55
+ scope.exists?(association.association_primary_key => id)
56
+ end
57
+
58
+ def target_records(related_class)
59
+ JSONAPI::ResourceLoader.find_for_model(related_class).records
60
+ rescue JSONAPI::ResourceLoader::MissingResourceClass
61
+ related_class.all
33
62
  end
34
63
 
35
64
  def validate_relationship_type!(type, association, definition = nil)
@@ -56,16 +85,23 @@ module JSONAPI
56
85
  end
57
86
  end
58
87
 
59
- def find_related_record(type, id, association, is_polymorphic)
88
+ def find_related_record(type, id, association, is_polymorphic, controller: nil)
60
89
  related_model_class = resolve_related_model_class(type, association, is_polymorphic)
61
90
  resource_class = JSONAPI::ResourceLoader.find_for_model(related_model_class)
62
- resource_class.records.find(id)
91
+ authorized_related_scope(resource_class.records, related_model_class, controller).find(id)
63
92
  rescue ActiveRecord::RecordNotFound
64
93
  raise ArgumentError, "Related resource not found: #{type} with id #{id}"
65
94
  rescue NameError
66
95
  raise ArgumentError, "Invalid relationship type: #{type} does not correspond to a valid model class"
67
96
  end
68
97
 
98
+ def authorized_related_scope(scope, model_class, controller)
99
+ handler = JSONAPI.configuration.authorization_scope
100
+ return scope unless controller && handler
101
+
102
+ handler.call(controller:, scope:, action: :index, model_class:)
103
+ end
104
+
69
105
  def resolve_related_model_class(type, association, is_polymorphic)
70
106
  return TypeConversion.type_to_class_name(type).constantize if is_polymorphic
71
107
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONAPI
4
- VERSION = "4.1.7.pre.202611160310"
4
+ VERSION = "4.1.8"
5
5
  end
data/lib/json_api.rb CHANGED
@@ -14,6 +14,8 @@ module JSONAPI
14
14
  # Rebuild BaseController, RelationshipsController, and ResourcesController to reflect the current
15
15
  # base_controller_class configuration. Safe to call repeatedly.
16
16
  def self.rebuild_base_controllers!
17
+ include_concerns_in_app_base_controller
18
+
17
19
  remove_const(:BaseController) if const_defined?(:BaseController)
18
20
  load "json_api/controllers/base_controller.rb"
19
21
 
@@ -23,6 +25,18 @@ module JSONAPI
23
25
  remove_const(:ResourcesController) if const_defined?(:ResourcesController)
24
26
  load "json_api/controllers/resources_controller.rb"
25
27
  end
28
+
29
+ # The relationship controller skips callbacks that ResourceActions defines. Rails re-adds a
30
+ # callback that the app base class gains later, and the re-added copy loses the skip. So the
31
+ # app base class must hold the concerns before the controllers load.
32
+ def self.include_concerns_in_app_base_controller
33
+ return unless configuration.base_controller_overridden?
34
+
35
+ app_base = configuration.base_controller_class
36
+ app_base.include(ControllerHelpers)
37
+ app_base.include(ResourceActions)
38
+ end
39
+ private_class_method :include_concerns_in_app_base_controller
26
40
  end
27
41
 
28
42
  require "json_api/resources/resource"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpie
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.7.pre.202611160310
4
+ version: 4.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp