jpie 4.1.7 → 4.1.9

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: 6693361f8a5fb2ff7720d1488fa54e184fa046b35c61b1c1e731d4bb453fc266
4
- data.tar.gz: 44d36ba7dda52e19709a625a40fb3ae9f3023c76f1fe48ac680b78b701ca17da
3
+ metadata.gz: 315aaaaed71bdf4807a63a407205327316ad7739e269c1179a27e0d801b51483
4
+ data.tar.gz: 829e98cab64b8b6794a3fa389f62d1a179746685f2af3803efd40e215e15024e
5
5
  SHA512:
6
- metadata.gz: 6ac33754f0475988e064b32941997d9b49e6611b6e99372a7dfd7d130ad1566314655b5fe7572d5de4f72498460f44f42c06d9367a6a76b2012b1e3844e28fd2
7
- data.tar.gz: 4a8edb0644bda6e9a6e8fa40916fb26c4a5608c13d1c3344d4206b3c19675a3dfeb7b7eedd1eb55ff0b1f0afa5f90d9d6486fa82e12d6838a860b8a8d7ef065e
6
+ metadata.gz: aa206b22a633084e2b8585bcc1f7154e6753bb9f46cd2439b703df8451eb51e2de7db1fc70120fa65f1c7293054d751f6ea5694f7e247ded43f1b6cc33250629
7
+ data.tar.gz: 025d1cef8a720347a0f453f5ec2c73c785c276996b92574f1785bed0316b22d77c9077b5aaa24dad78ceb08b0bdda4465ffb242eb9f0c99e6cb5053e62863e8b
data/CHANGELOG.md CHANGED
@@ -7,6 +7,35 @@ at release time.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [4.1.9] - 2026-11-20
11
+
12
+ ### Fixed
13
+
14
+ - **Refuse a write body that names a hidden target.** In 4.1.8 a write body that
15
+ named a target hidden by `authorization_scope` set the key to nil. An update
16
+ then cleared the stored relationship and answered 200. The write now stops
17
+ with 400, as a relationship route does for a hidden target or a missing
18
+ target. Regression spec: `authorization_scope_body_write_target_spec.rb`.
19
+
20
+ ## [4.1.8] - 2026-11-20
21
+
22
+ ### Fixed
23
+
24
+ - **Resolve relationship write targets through `authorization_scope`.** A
25
+ relationship write named its target with the bare model relation. A write
26
+ body named its target with the raw key. Both now use the
27
+ `authorization_scope` hook. A target that the hook hides gets the same answer
28
+ as a target that does not exist. A write body that names a target costs one
29
+ more query. Regression specs:
30
+ `authorization_scope_relationship_write_target_spec.rb` and
31
+ `authorization_scope_body_write_target_spec.rb`.
32
+ - **Keep the relationship skip after a controller rebuild.** A rebuild loaded
33
+ the controllers before the app base class held the concerns. Rails then lost
34
+ the relationship controller's skip of the id and type checks, and a second
35
+ relationship PATCH in one process answered 409. The app base class now gets
36
+ the concerns before the controllers load. Regression spec:
37
+ `relationship_update_after_controller_reload_spec.rb`.
38
+
10
39
  ## [4.1.7] - 2026-11-18
11
40
 
12
41
  ### 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.reject_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)
@@ -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,36 @@ 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 fail like a target that does not exist, as it does on a
39
+ # relationship route. The write stops, so it never clears the stored relationship.
40
+ def reject_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
+ id = attrs[association.foreign_key.to_s]
47
+ next if id.nil? || visible_target?(association, id, controller)
48
+
49
+ raise ArgumentError, "Related resource not found: #{association.name} with id #{id}"
50
+ end
51
+ attrs
52
+ end
53
+
54
+ def visible_target?(association, id, controller)
55
+ related_class = association.klass
56
+ scope = authorized_related_scope(target_records(related_class), related_class, controller)
57
+ scope.exists?(association.association_primary_key => id)
58
+ end
59
+
60
+ def target_records(related_class)
61
+ JSONAPI::ResourceLoader.find_for_model(related_class).records
62
+ rescue JSONAPI::ResourceLoader::MissingResourceClass
63
+ related_class.all
33
64
  end
34
65
 
35
66
  def validate_relationship_type!(type, association, definition = nil)
@@ -56,16 +87,23 @@ module JSONAPI
56
87
  end
57
88
  end
58
89
 
59
- def find_related_record(type, id, association, is_polymorphic)
90
+ def find_related_record(type, id, association, is_polymorphic, controller: nil)
60
91
  related_model_class = resolve_related_model_class(type, association, is_polymorphic)
61
92
  resource_class = JSONAPI::ResourceLoader.find_for_model(related_model_class)
62
- resource_class.records.find(id)
93
+ authorized_related_scope(resource_class.records, related_model_class, controller).find(id)
63
94
  rescue ActiveRecord::RecordNotFound
64
95
  raise ArgumentError, "Related resource not found: #{type} with id #{id}"
65
96
  rescue NameError
66
97
  raise ArgumentError, "Invalid relationship type: #{type} does not correspond to a valid model class"
67
98
  end
68
99
 
100
+ def authorized_related_scope(scope, model_class, controller)
101
+ handler = JSONAPI.configuration.authorization_scope
102
+ return scope unless controller && handler
103
+
104
+ handler.call(controller:, scope:, action: :index, model_class:)
105
+ end
106
+
69
107
  def resolve_related_model_class(type, association, is_polymorphic)
70
108
  return TypeConversion.type_to_class_name(type).constantize if is_polymorphic
71
109
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONAPI
4
- VERSION = "4.1.7"
4
+ VERSION = "4.1.9"
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
4
+ version: 4.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp