David Costanzo created GROOVY-12024:
---------------------------------------
Summary: Subscript operator for Map has unexpected behavior when
key is 'properties'
Key: GROOVY-12024
URL: https://issues.apache.org/jira/browse/GROOVY-12024
Project: Groovy
Issue Type: Bug
Reporter: David Costanzo
The subscript operator has strange behavior for Map when the key is
'properties'. Instead of accessing the value associated with that key, it
accesses a read-only Map object.
This behavior changed between Groovy 3 and Groovy 5. My expectation is that
the subscript operator can access values for a key named 'properties', as it
did in Groovy 3.
*Steps to Reproduce:*
In groovysh 5, the subscript operator references the system-provided, read-only
map.
{code:java}
groovy> map = ['properties': 1]
groovy> map['properties']
[:]
groovy> map['properties'] = 2
ReadOnlyPropertyException: Cannot set read-only property: properties for class:
java.util.LinkedHashMap{code}
In groovysh 3, the subscript operator references keys of the user-created map.
{code:java}
groovy:000> map = ['properties': 1]
===> [properties:1]
groovy:000> map['properties']
===> 1
groovy:000> map['properties'] = 2
===> 2{code}
I don't know if this is intentional, but I couldn't find a reference to in the
documentation. On
[https://docs.groovy-lang.org/latest/html/documentation/#subscript-operator] it
says that the subscript operators is a shorthand for getAt() and setAt(). On
[https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html#getAt(java.lang.Object)]
it does not mention that "properties" can't be used as a key.
In my view, this is either a code bug (an internal implementation detail is
being exposed beyond it's intended scope) or a doc bug (the subscript operator
or Map.getAt() should mention the reserved system key named "properties").
*Impact:*
This creates the potential for hidden bugs. For example, I have a script that
manages some metadata for internal libraries using a map keyed by the library
name. The script appears to work correctly, but then I add a library named
"properties" and the script fails to work. (This is not theoretical, this is
what actually happened to me).
--
This message was sent by Atlassian Jira
(v8.20.10#820010)