jdaugherty commented on code in PR #16627:
URL: https://github.com/apache/grails-core/pull/16627#discussion_r4265178814


##########
grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/access/TraitPropertyFieldAccessSpec.groovy:
##########
@@ -0,0 +1,55 @@
+/*
+ *  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.orm.hibernate.access
+
+import org.hibernate.property.access.spi.PropertyAccess
+import spock.lang.Issue
+import spock.lang.Specification
+
+/**
+ * Regression test for private trait-field access through {@link 
TraitPropertyAccessStrategy}.
+ *
+ * Groovy implements trait properties by generating a private, name-mangled 
field on the
+ * implementing class (for example {@code org_example_MyTrait__value}) plus 
public bridge
+ * accessors. {@link TraitPropertyAccessStrategy} resolves the property to 
that generated
+ * field and builds a field-based {@code Getter}/{@code Setter} over it, so 
the field must
+ * be made accessible before it is used.
+ */
+class TraitPropertyFieldAccessSpec extends Specification {
+
+    @Issue('https://github.com/apache/grails-core/issues/16615')
+    void "field-based access to a trait property reads back what the setter 
wrote"() {

Review Comment:
   Same ask as on the Hibernate 5 spec: add the runtime connection-source test; 
on this module the property needs `accessType: AccessType.FIELD` for the 
strategy to be selected at all. Optionally fold this round-trip feature into 
the existing `TraitPropertyAccessStrategySpec`, which already exercises this 
class directly but only checks the accessor types and never performs a get or 
set.



##########
grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/access/TraitPropertyFieldAccessSpec.groovy:
##########
@@ -0,0 +1,55 @@
+/*
+ *  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.orm.hibernate.access
+
+import org.hibernate.property.access.spi.PropertyAccess
+import spock.lang.Issue
+import spock.lang.Specification
+
+/**
+ * Regression test for private trait-field access through {@link 
TraitPropertyAccessStrategy}.
+ *
+ * Groovy implements trait properties by generating a private, name-mangled 
field on the
+ * implementing class (for example {@code org_example_MyTrait__value}) plus 
public bridge
+ * accessors. {@link TraitPropertyAccessStrategy} resolves the property to 
that generated
+ * field and builds a field-based {@code Getter}/{@code Setter} over it, so 
the field must
+ * be made accessible before it is used.
+ */
+class TraitPropertyFieldAccessSpec extends Specification {
+
+    @Issue('https://github.com/apache/grails-core/issues/16615')
+    void "field-based access to a trait property reads back what the setter 
wrote"() {

Review Comment:
   Calling the strategy directly is the only way to see the failure at startup, 
so this is fine as the unit-level check, but it bypasses every public API. 
There is a path applications can hit: Spring clears its reflection cache at the 
end of context refresh (`AbstractApplicationContext.resetCommonCaches`), and a 
SessionFactory built after that, such as for a connection source added at 
runtime, gets a fresh inaccessible `Field`. This fails on the base commit and 
passes with your change on both modules:
   
   ```groovy
   @Shared @AutoCleanup HibernateDatastore datastore = new 
HibernateDatastore(DatastoreUtils.createPropertyResolver([
           'dataSource.url': 'jdbc:h2:mem:traitFieldRuntime;LOCK_TIMEOUT=10000',
           'dataSource.dbCreate': 'create-drop',
           'hibernate.hbm2ddl.auto': 'create-drop']), RuntimeTraitEntity)
   
   void "a trait property is readable on a connection source added after Spring 
cleared its reflection cache"() {
       given: 'what Spring does at the end of every context refresh'
       ReflectionUtils.clearCache()
   
       when: 'a data source added at runtime builds a new SessionFactory'
       datastore.connectionSources.addConnectionSource('later', [pooled: true, 
dbCreate: 'create-drop',
               url: 'jdbc:h2:mem:traitFieldLater;LOCK_TIMEOUT=10000'])
       Long id = null
       RuntimeTraitEntity.later.withTransaction {
           def entity = new RuntimeTraitEntity(name: 'n', bar: 'b')
           GormEnhancer.findInstanceApi(RuntimeTraitEntity, 
'later').save(entity, [flush: true, failOnError: true])
           id = entity.id
       }
       String bar = null
       RuntimeTraitEntity.later.withNewSession { bar = 
RuntimeTraitEntity.later.get(id).bar }
   
       then:
       bar == 'b'
   }
   
   trait RuntimeTrait {
       String bar
   }
   
   @Entity
   class RuntimeTraitEntity implements RuntimeTrait {
       String name
       static mapping = {
           datasource 'ALL'
           bar accessType: AccessType.FIELD   // needed on Hibernate 7, where 
the default access type is PROPERTY
       }
   }
   ```
   
   (imports: `org.grails.datastore.gorm.GormEnhancer`, 
`org.grails.datastore.mapping.core.DatastoreUtils`, 
`org.springframework.util.ReflectionUtils`, `jakarta.persistence.AccessType`.) 
Without the fix the first read fails with `PropertyAccessException` caused by 
`IllegalAccessException: class 
org.hibernate.property.access.spi.GetterFieldImpl cannot access a member of 
class ...RuntimeTraitEntity with modifiers "private"`. Putting it under 
`grails.gorm.tests.traits` next to `TraitPropertySpec` keeps it with the other 
end-to-end trait coverage.



-- 
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