Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix contract writability check side effect

ulferts184c0e42bec2
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rb
index 19a47d8488c..9220c6589ca 100644
--- a/app/contracts/model_contract.rb
+++ b/app/contracts/model_contract.rb
@@ -216,7 +216,13 @@ class ModelContract < Reform::Contract
def collect_ancestor_attributes_by(attribute_to_collect, combination_method, cleanup_method)
klass = self.class
- attributes = klass.send(attribute_to_collect)
+ # `dup` is very important here.
+ # As the array/hash queried for here is memoized in the class (e.g. writable_conditions) and that
+ # object is later on combined (e.g. with #concat which alters the called on object) with a
+ # similar object from the superclass, every call would alter the memoized object as an unwanted side effect.
+ # Not only would that lead to the subclass now having all the attributes of the superclass,
+ # but those attributes would also be duplicated so that performance suffers significantly.
+ attributes = klass.send(attribute_to_collect).dup
while klass.superclass != ModelContract
# Collect all the attribute_to_collect from ancestors