Prevent modification of attribute defaults#175
Open
Conversation
Either freeze them, in the case of Strings or empty Hash/Array,
or call a proc to return a unique instance of a mutable object
(mostly relevant for the 'principals' attribute of PermissionTarget).
Receiving a FrozenError in this case may be unexpected for users,
but it will force them into the preferred method of setting attributes
(i.e. using the "#{name}=" setter).
Fixes issue chef#174.
Signed-off-by: Matthew Blythe <mblythester+git@gmail.com>
mblythe86
commented
Nov 26, 2024
|
|
||
| # Based on https://github.com/JFrogDev/build-info/blob/master/README.md#build-info-json-format | ||
| attribute :properties, {} | ||
| attribute :properties, {}.freeze |
Author
There was a problem hiding this comment.
Why freeze these instead of creating a Proc that would return an empty Hash?
Because the Proc approach could silently drop data. For example, if a user did this:
build = Artifactory::Resource::Build.new
build.properties['key'] = 'value'
puts build.propertiesIt would print {} instead of {"key" => "value} like a user would expect. This happens because the user modified the mutable default returned by the getter, not an object stored in the object's attribute hash.
If this method of accessing and modifying attributes is common, then the getter could be enhanced to store that default value into the object's attribute hash before returning it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Either freeze the default attribute values (in the case of
Stringor emptyHash/Array) or call aProcto return a unique instance of a mutable object (mostly relevant for theprincipalsattribute ofPermissionTarget). Add a check in theattributemethod to enforce this.Receiving a
FrozenErrorin this case may be unexpected for users, but it will force them into the preferred method of setting attributes (i.e. using the#{name}=setter).Related Issue
Fixes issue #174.
Types of changes
#{name}=setters and relies on this undocumented behavior.)Checklist:
Gemfile.lockhas changed, I have used--conservativeto do it and included the full output in the Description above. (No changes toGemfile.lock)