Skip to content

Commit 80285b9

Browse files
committed
Add note about duplicate alias check
1 parent 0bb3067 commit 80285b9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lib/jsonapi/active_relation_resource_finder/join_manager.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ def add_join_details(join_key, details, check_for_duplicate_alias = true)
123123
fail "details already set" if @join_details.has_key?(join_key)
124124
@join_details[join_key] = details
125125

126+
# Joins are being tracked as they are added to the built up relation. If the same table is added to a
127+
# relation more than once subsequent versions will be assigned an alias. Depending on the order the joins
128+
# are made the computed aliases may change. The order this library performs the joins was chosen
129+
# to prevent this. However if the relation is reordered it should result in reusing on of the earlier
130+
# aliases (in this case a plain table name). The following check will catch this an raise an exception.
131+
# An exception is appropriate because not using the correct alias could leak data due to filters and
132+
# applied permissions being performed on the wrong data.
126133
if check_for_duplicate_alias && @collected_aliases.include?(details[:alias])
127134
fail "alias '#{details[:alias]}' has already been added. Possible relation reordering"
128135
end
@@ -166,6 +173,8 @@ def perform_joins(records, options)
166173
end
167174
end
168175

176+
# We're adding the source alias with two keys. We only want the check for duplicate aliases once.
177+
# See the note in `add_join_details`.
169178
check_for_duplicate_alias = !(relationship == source_relationship)
170179
add_join_details(PathSegment::Relationship.new(relationship: relationship, resource_klass: related_resource_klass), details, check_for_duplicate_alias)
171180
end

0 commit comments

Comments
 (0)