borinquenkid commented on code in PR #16034: URL: https://github.com/apache/grails-core/pull/16034#discussion_r3626050097
########## 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: Good catch on the mechanics — you're right that this test's two ClassNodes use different fully-qualified names (`org.example.one.Widget` vs `org.example.two.Widget`), so it wouldn't have collided under the old name-/equals()-keyed cache either, and doesn't by itself guard the regression. That guard is the next test below, `"property lookups for two distinct ClassNode instances with the exact same unqualified name do not corrupt each other"` — both ClassNodes there are named plain `Widget` with no package, so they *do* compare equal (`first == second`, matching `hashCode()`) exactly as the old cache's key would, and the test asserts they still resolve independently under the new identity-keyed cache. That's the one that fails under the old implementation and passes under this fix. This first test is intentionally a different, narrower check — that two distinct, differently-named classes never get confused with each other, which is a correctness property worth keeping on its own regardless of the collision bug. Leaving both as-is: this one for general non-contamination across genuinely different classes, the next one for the actual same-name collision regression. -- 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]
