Copilot commented on code in PR #16034:
URL: https://github.com/apache/grails-core/pull/16034#discussion_r3626003258


##########
grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/transform/AstPropertyResolveUtilsSpec.groovy:
##########
@@ -0,0 +1,101 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.datastore.gorm.transform
+
+import java.lang.reflect.Modifier
+
+import org.codehaus.groovy.ast.ClassHelper
+import org.codehaus.groovy.ast.ClassNode
+import spock.lang.Specification
+
+/**
+ * {@link AstPropertyResolveUtils} caches resolved property metadata in a 
static, process-wide
+ * map keyed by {@link ClassNode}. Two distinct compilations (e.g. the same 
source parsed in two
+ * different {@code GroovyClassLoader}s, as happens for dynamically-generated 
sources and in
+ * tests) produce distinct {@code ClassNode} instances that can legitimately 
share the exact same
+ * name - {@code ClassNode#equals(Object)} compares by name, so a naive name- 
or equals()-based
+ * cache key would conflate them, corrupting the resolved properties of one 
class with those of
+ * an unrelated class that happens to share its name. This spec proves the 
cache keys strictly by
+ * {@code ClassNode} identity, so same-named-but-distinct class nodes never 
contaminate each
+ * other's cached property data.
+ */
+class AstPropertyResolveUtilsSpec extends Specification {
+
+    void "property lookups for two same-named ClassNodes in different packages 
do not corrupt each other"() {
+        given: 'two distinct ClassNodes with the same simple name declared in 
different packages'
+        ClassNode first = new ClassNode('org.example.one.Widget', 
Modifier.PUBLIC, ClassHelper.OBJECT_TYPE)
+        first.addProperty('color', Modifier.PUBLIC, ClassHelper.STRING_TYPE, 
null, null, null)
+
+        ClassNode second = new ClassNode('org.example.two.Widget', 
Modifier.PUBLIC, ClassHelper.OBJECT_TYPE)
+        second.addProperty('weight', Modifier.PUBLIC, 
ClassHelper.Integer_TYPE, null, null, null)

Review Comment:
   This test currently uses two different fully-qualified class names 
(different packages), which would not have collided in the previous name-keyed 
cache and therefore doesn’t exercise the regression mechanism described in the 
PR (distinct ClassNode instances with the exact same name). Consider updating 
it to use the same fully-qualified name for both ClassNodes so it fails under 
the old implementation and protects the intended fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to