You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/08/06 15:56:54 UTC
(grails-core) 14/17: fix: make AstUtils.copyAnnotations dedup opt-in instead of global
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch feat/gorm-registry-consolidated
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit fda9664613ae002f5952ab18c1ed186ad7d7bfdd
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Thu Aug 6 02:28:29 2026 -0500
fix: make AstUtils.copyAnnotations dedup opt-in instead of global
copyAnnotations silently skipped copying an annotation whenever the target
node already carried one of the same type - unconditionally, for every
caller. AstUtils is a general-purpose utility in grails-datastore-core, not
scoped to any one caller's needs, and that default silently broke the
contract for any legitimately-repeatable annotation (e.g.
jakarta.validation.constraints.Pattern): a second occurrence would be
dropped rather than copied.
Confirmed AstUtils.copyAnnotations has exactly one real caller in the whole
repo - ServiceTransformation.groovy. (A grep hit on
GrailsASTUtils.copyAnnotations in grails-controllers was a false positive -
a different class, org.grails.compiler.injection.GrailsASTUtils, in a
different module entirely.)
Added a 5th boolean skipExisting parameter, defaulting to false via the
2-arg and 4-arg convenience overloads - repeatable annotations now copy
correctly by default. ServiceTransformation's two call sites explicitly
pass skipExisting=true, preserving its own original behavior: it writes
onto a node it may have already annotated itself (e.g.
@NotTransactional/@ReadOnly) and needs to avoid double-adding those
specifically.
Updated AstUtilsSpec: replaced the test asserting the old (now-wrong)
default with 4 cases - repeatable annotations copy fully by default,
skipExisting=true dedupes, and omitting skipExisting keeps both the
original and the copy.
Verified: grails-datastore-core 143/0, grails-datamapping-core (incl. all
ServiceTransform* specs) + grails-data-simple 773/0, H5 813/0, H7 3012/0,
Mongo 646/0. codeStyle clean.
Co-Authored-By: Claude Fable 5 <no...@anthropic.com>
---
.../transform/ServiceTransformation.groovy | 4 +--
.../datastore/mapping/reflect/AstUtils.groovy | 25 +++++++++++++---
.../datastore/mapping/reflect/AstUtilsSpec.groovy | 34 ++++++++++++++++++++--
3 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/transform/ServiceTransformation.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/transform/ServiceTransformation.groovy
index ba6c3806dc..52ba202aa0 100644
--- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/transform/ServiceTransformation.groovy
+++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/services/transform/ServiceTransformation.groovy
@@ -243,7 +243,7 @@ class ServiceTransformation extends AbstractTraitApplyingGormASTTransformation i
addDatastoreMethods(impl, datastoreType, targetDomainClass, propertiesFields)
- copyAnnotations(classNode, impl)
+ copyAnnotations(classNode, impl, null, null, true)
AnnotationNode serviceAnnotation = findAnnotation((AnnotatedNode)impl, Service)
if (serviceAnnotation != null && serviceAnnotation.getMember('name') == null) {
@@ -326,7 +326,7 @@ class ServiceTransformation extends AbstractTraitApplyingGormASTTransformation i
// Copy annotations after implement() so that @Query GString values are
// already replaced with constX(IMPLEMENTED) before being copied to methodImpl.
- copyAnnotations(method, methodImpl)
+ copyAnnotations(method, methodImpl, null, null, true)
if (!Modifier.isProtected(method.modifiers)) {
if (!TransactionalTransform.hasTransactionalAnnotation(methodImpl)) {
diff --git a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/AstUtils.groovy b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/AstUtils.groovy
index 30a27d010d..6b04d55de2 100644
--- a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/AstUtils.groovy
+++ b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/AstUtils.groovy
@@ -357,19 +357,36 @@ class AstUtils {
}
static void copyAnnotations(final AnnotatedNode from, final AnnotatedNode to) {
- copyAnnotations(from, to, null, null)
+ copyAnnotations(from, to, null, null, false)
}
static void copyAnnotations(final AnnotatedNode from, final AnnotatedNode to, final Set<String> included, final Set<String> excluded) {
+ copyAnnotations(from, to, included, excluded, false)
+ }
+
+ /**
+ * Copies the annotations of one node onto another.
+ *
+ * @param from The node to copy the annotations from
+ * @param to The node to copy the annotations to
+ * @param included If non-null, only annotations whose class name appears here are copied
+ * @param excluded If non-null, annotations whose class name appears here are not copied
+ * @param skipExisting When {@code true} an annotation is not copied if {@code to} already carries one of the
+ * same type. Callers that may write onto a node they have already annotated themselves should opt in.
+ * The default is {@code false}, because repeatable annotations legitimately occur more than once on the
+ * same node and skipping them would silently produce an incomplete set.
+ */
+ static void copyAnnotations(final AnnotatedNode from, final AnnotatedNode to, final Set<String> included, final Set<String> excluded, final boolean skipExisting) {
final List<AnnotationNode> annotationsToCopy = from.getAnnotations()
for (final AnnotationNode node : annotationsToCopy) {
String annotationClassName = node.getClassNode().getName()
if ((excluded == null || !excluded.contains(annotationClassName)) &&
(included == null || included.contains(annotationClassName))) {
- if (to.getAnnotations(node.getClassNode()).isEmpty()) {
- final AnnotationNode copyOfAnnotationNode = cloneAnnotation(node)
- to.addAnnotation(copyOfAnnotationNode)
+ if (skipExisting && !to.getAnnotations(node.getClassNode()).isEmpty()) {
+ continue
}
+ final AnnotationNode copyOfAnnotationNode = cloneAnnotation(node)
+ to.addAnnotation(copyOfAnnotationNode)
}
}
}
diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/AstUtilsSpec.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/AstUtilsSpec.groovy
index 84e1e6b58d..ab6ba51379 100644
--- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/AstUtilsSpec.groovy
+++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/AstUtilsSpec.groovy
@@ -57,7 +57,22 @@ class AstUtilsSpec extends Specification {
to.getAnnotations(annotationType).size() == 1
}
- void "copyAnnotations does not add a second annotation of a type the target already carries"() {
+ void "copyAnnotations copies repeatable annotations by default"() {
+ given: "a source carrying the same annotation type twice, as a repeatable annotation legitimately does"
+ def from = newMethodNode()
+ def to = newMethodNode()
+ def annotationType = ClassHelper.make("jakarta.validation.constraints.Pattern")
+ from.addAnnotation(new AnnotationNode(annotationType))
+ from.addAnnotation(new AnnotationNode(annotationType))
+
+ when:
+ AstUtils.copyAnnotations(from, to)
+
+ then: "both are copied - the default must not silently drop all but the first"
+ to.getAnnotations(annotationType).size() == 2
+ }
+
+ void "copyAnnotations with skipExisting does not add a type the target already carries"() {
given: "a target that a caller has already annotated directly, e.g. to avoid double-adding @NotTransactional/@ReadOnly"
def from = newMethodNode()
def to = newMethodNode()
@@ -66,12 +81,27 @@ class AstUtilsSpec extends Specification {
to.addAnnotation(new AnnotationNode(annotationType))
when:
- AstUtils.copyAnnotations(from, to)
+ AstUtils.copyAnnotations(from, to, null, null, true)
then: "the target keeps its own single instance rather than gaining a duplicate, which Groovy's compiler rejects"
to.getAnnotations(annotationType).size() == 1
}
+ void "copyAnnotations without skipExisting keeps the pre-existing annotation and the copy"() {
+ given:
+ def from = newMethodNode()
+ def to = newMethodNode()
+ def annotationType = ClassHelper.make("grails.gorm.transactions.NotTransactional")
+ from.addAnnotation(new AnnotationNode(annotationType))
+ to.addAnnotation(new AnnotationNode(annotationType))
+
+ when:
+ AstUtils.copyAnnotations(from, to)
+
+ then: "the default is unchanged from the general-purpose contract - callers opt in to deduplication"
+ to.getAnnotations(annotationType).size() == 2
+ }
+
private static MethodNode newMethodNode() {
new MethodNode("test", 0, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)
}