Copilot commented on code in PR #15395:
URL: https://github.com/apache/grails-core/pull/15395#discussion_r2824385696
##########
grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/implementers/DeleteImplementer.groovy:
##########
@@ -80,7 +81,15 @@ class DeleteImplementer extends
AbstractDetachedCriteriaServiceImplementor imple
void implementById(ClassNode domainClassNode, MethodNode
abstractMethodNode, MethodNode newMethodNode, ClassNode targetClassNode,
BlockStatement body, Expression byIdLookup) {
boolean isVoidReturnType =
ClassHelper.VOID_TYPE.equals(newMethodNode.returnType)
VariableExpression obj = varX('$obj')
- Statement deleteStatement = stmt(callX(obj, 'delete'))
+ Expression connectionId = findConnectionId(abstractMethodNode)
+ Statement deleteStatement
+ if (connectionId != null) {
+ // Route delete through the instance API for the specified
connection
+ deleteStatement =
stmt(callX(buildInstanceApiLookup(domainClassNode, connectionId), 'delete',
args(obj)))
+ }
+ else {
+ deleteStatement = stmt(callX(obj, 'delete'))
+ }
Review Comment:
`findConnectionId` is computed from `abstractMethodNode` here. For abstract
services that implement an interface, the unimplemented method node can come
from the interface, so class-level `@Transactional(connection=...)` on the
abstract service (copied to the generated implementation) won’t be detected and
the delete will still route via `obj.delete()` (default connection). Prefer
`findConnectionId(newMethodNode)` to pick up annotations copied onto the
generated impl class (consistent with
`AbstractDetachedCriteriaServiceImplementor`).
##########
grails-datamapping-core/src/test/groovy/grails/gorm/services/ConnectionRoutingServiceTransformSpec.groovy:
##########
@@ -0,0 +1,327 @@
+/*
+ * 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 grails.gorm.services
+
+import grails.gorm.transactions.Transactional
+import org.grails.datastore.gorm.services.Implemented
+import org.grails.datastore.gorm.services.implementers.DeleteImplementer
+import org.grails.datastore.gorm.services.implementers.FindAndDeleteImplementer
+import org.grails.datastore.gorm.services.implementers.FindOneImplementer
+import org.grails.datastore.gorm.services.implementers.SaveImplementer
+import spock.lang.Specification
+
+/**
+ * Tests that auto-implemented Data Service methods correctly route through
+ * connection-aware GormEnhancer APIs when @Transactional(connection=...) is
specified.
+ *
+ * Covers the fix for: save (single-entity), delete (by-id), find-and-delete
(by-id),
+ * and get/find (by-id) which previously bypassed connection routing and
always hit
+ * the default datasource.
+ *
+ * @see org.grails.datastore.gorm.services.implementers.SaveImplementer
+ * @see org.grails.datastore.gorm.services.implementers.DeleteImplementer
+ * @see
org.grails.datastore.gorm.services.implementers.FindAndDeleteImplementer
+ * @see
org.grails.datastore.gorm.services.implementers.AbstractDetachedCriteriaServiceImplementor
+ */
+class ConnectionRoutingServiceTransformSpec extends Specification {
+
+ void "test save with @Transactional(connection) routes through
connection-aware API"() {
+ when: "an abstract data service with
@Transactional(connection='secondary') declares save(Foo)"
+ Class service = new GroovyClassLoader().parseClass('''
+import grails.gorm.services.Service
+import grails.gorm.annotation.Entity
+import grails.gorm.transactions.Transactional
+
+@Service(Foo)
+@Transactional(connection = 'secondary')
+abstract class FooService {
+
+ abstract Foo save(Foo foo)
+
+ abstract Foo saveFoo(String title)
+}
+
+@Entity
+class Foo {
+ String title
+}
+''')
Review Comment:
Test coverage doesn’t currently exercise the reported “abstract class
implements separate interface” pattern where `@Transactional(connection=...)`
is on the abstract class but CRUD methods are only declared on the interface.
Adding a spec case for that pattern would prevent regressions and would catch
issues where implementers accidentally consult `abstractMethodNode` (interface)
instead of the generated impl/class annotations.
##########
grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/implementers/SaveImplementer.groovy:
##########
@@ -63,9 +65,18 @@ class SaveImplementer extends AbstractSaveImplementer
implements SingleResultSer
Parameter[] parameters = newMethodNode.parameters
int parameterCount = parameters.length
if (parameterCount == 1 && AstUtils.isDomainClass(parameters[0].type))
{
- body.addStatement(
- returnS(callX(varX(parameters[0]), 'save',
namedArgs(failOnError: ConstantExpression.TRUE)))
- )
+ Expression connectionId = findConnectionId(abstractMethodNode)
+ if (connectionId != null) {
+ // Route save through the instance API for the specified
connection
+ body.addStatement(
+ returnS(callX(buildInstanceApiLookup(domainClassNode,
connectionId), 'save', args(varX(parameters[0]), namedArgs(failOnError:
ConstantExpression.TRUE))))
+ )
+ }
+ else {
+ body.addStatement(
+ returnS(callX(varX(parameters[0]), 'save',
namedArgs(failOnError: ConstantExpression.TRUE)))
+ )
+ }
Review Comment:
`findConnectionId` is being called with `abstractMethodNode`, but when the
service is an abstract class that implements an interface (with CRUD methods
only declared on the interface) the `abstractMethodNode.declaringClass` may be
the interface, not the annotated service class. In that case
`@Transactional(connection=...)` copied onto the generated implementation class
won’t be seen and routing will still fall back to the default datasource. Use
`findConnectionId(newMethodNode)` (or otherwise ensure class-level annotations
on the generated impl are consulted) for the single-entity save path; consider
aligning the multi-arg save path as well since it currently passes
`abstractMethodNode` into `bindParametersAndSave`.
##########
grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/implementers/FindAndDeleteImplementer.groovy:
##########
@@ -71,14 +74,27 @@ class FindAndDeleteImplementer extends FindOneImplementer
implements SingleResul
@Override
protected Statement buildReturnStatement(ClassNode targetDomainClass,
MethodNode abstractMethodNode, Expression queryMethodCall, Expression args,
MethodNode newMethodNode) {
VariableExpression var = varX('$obj', targetDomainClass)
- MethodCallExpression deleteCall = args != null ? callX(var, 'delete',
args) : callX(var, 'delete')
-
- deleteCall.setSafe(true) // null safe
- block(
- declS(var, queryMethodCall),
- stmt(deleteCall),
- returnS(var)
- )
+ Expression connectionId = findConnectionId(newMethodNode)
+ if (connectionId != null) {
+ // Route delete through the instance API for the specified
connection
+ block(
+ declS(var, queryMethodCall),
+ ifS(
+ notNullX(var),
+ stmt(callX(buildInstanceApiLookup(targetDomainClass,
connectionId), 'delete', new ArgumentListExpression(var)))
Review Comment:
In the connection-aware branch, the generated delete call drops the optional
`args` map entirely. Previously, when an `args` map is present, the generated
code calls `obj.delete(args)` (allowing options like `flush: true`); the
instance API also supports `delete(instance, Map)`. Preserve behavior by
passing `args` through when it’s non-null (and still guarding with the null
check).
```suggestion
Expression instanceApi =
buildInstanceApiLookup(targetDomainClass, connectionId)
Expression deleteCall = args != null ?
callX(instanceApi, 'delete', new
ArgumentListExpression(var, args)) :
callX(instanceApi, 'delete', new
ArgumentListExpression(var))
block(
declS(var, queryMethodCall),
ifS(
notNullX(var),
stmt(deleteCall)
```
--
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]