Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent CurrentAttributes defaults from leaking #50714

Merged
merged 1 commit into from
Jan 11, 2024

Conversation

jonathanhefner
Copy link
Member

Follow-up to #50677.

Prior to this commit, all ActiveSupport::CurrentAttributes subclasses stored their default values in the same Hash, causing default values to leak between classes. This commit ensures each subclass maintains a separate Hash.

This commit also simplifies the resolution of default values, replacing the merge_defaults! method with resolve_defaults.

Follow-up to rails#50677.

Prior to this commit, all `ActiveSupport::CurrentAttributes` subclasses
stored their default values in the same `Hash`, causing default values
to leak between classes.  This commit ensures each subclass maintains a
separate `Hash`.

This commit also simplifies the resolution of default values, replacing
the `merge_defaults!` method with `resolve_defaults`.
@jonathanhefner jonathanhefner merged commit 41a57cf into rails:main Jan 11, 2024
3 of 4 checks passed
@@ -188,12 +188,12 @@ def method_added(name)
end
end

class_attribute :defaults, instance_writer: false, default: {}
class_attribute :defaults, instance_writer: false, default: {}.freeze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for following this up!

I mistook the guidance provided by the class_attribute documentation as clone-on-inherit instead of mutation-free overrides:

Declare a class-level attribute whose value is inheritable by subclasses. Subclasses can change their own value and it will not impact parent class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants