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/23 16:04:34 UTC
(grails-core) 01/03: Consolidate GraphQLEntityHelper.getMapping's multi-branch reassignment
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch chore/graphql-root-cleanup
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 52d4082f6762baaade94124a428f716b50e5cb17
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sun Aug 23 09:46:48 2026 -0500
Consolidate GraphQLEntityHelper.getMapping's multi-branch reassignment
mapping was declared null then conditionally reassigned across four
if/else-if branches before being read - the same shape flagged
elsewhere in this cleanup as a false "assignment is not used" warning
once IntelliJ's flow analysis loses track of a variable reassigned
across many branches. Extracted the reflection lookup
(findGraphqlProperty), the graphql-value-to-GraphQLMapping conversion
(toGraphQLMapping, with each branch returning directly), and the
overall build/verify sequence (buildMapping) into their own methods,
so getMapping itself is now a single containsKey-check plus a
single build+cache assignment.
Added direct unit coverage for getMapping via a Stub(PersistentEntity)
(no static graphql property, a Boolean true, an unsupported property
type that should throw IllegalMappingException, and cache identity
across repeated calls) - previously it was only exercised indirectly
through getDescription and other specs' Closure-based mappings.
Co-Authored-By: Claude Sonnet 5 <no...@anthropic.com>
---
.../grails/gorm/graphql/GraphQLEntityHelper.groovy | 59 +++++++++++++---------
.../gorm/graphql/GraphQLEntityHelperSpec.groovy | 58 +++++++++++++++++++++
2 files changed, 94 insertions(+), 23 deletions(-)
diff --git a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/GraphQLEntityHelper.groovy b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/GraphQLEntityHelper.groovy
index a2a2cce9ac..f003af6ed7 100644
--- a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/GraphQLEntityHelper.groovy
+++ b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/GraphQLEntityHelper.groovy
@@ -73,35 +73,48 @@ class GraphQLEntityHelper {
if (mappings.containsKey(entity)) {
return mappings.get(entity)
}
- Object graphql
+ GraphQLMapping mapping = buildMapping(entity)
+ mappings.put(entity, mapping)
+ mapping
+ }
+
+ private static GraphQLMapping buildMapping(PersistentEntity entity) {
+ Object graphql = findGraphqlProperty(entity)
+ if (graphql == null) {
+ return null
+ }
+
+ GraphQLMapping mapping = toGraphQLMapping(graphql)
+ if (!(mapping instanceof GraphQLMapping)) {
+ throw new IllegalMappingException("The static graphql property on ${entity.name} is not a Boolean, Closure, or GraphQLMapping")
+ }
+ verifyMapping(mapping, entity)
+ mapping
+ }
+
+ private static Object findGraphqlProperty(PersistentEntity entity) {
for (Method method: entity.javaClass.declaredMethods) {
if (Modifier.isStatic(method.modifiers) && method.name.equalsIgnoreCase('getgraphql')) {
- graphql = method.invoke(null)
- break
+ return method.invoke(null)
}
}
- GraphQLMapping mapping = null
- if (graphql != null) {
- if (graphql == Boolean.TRUE) {
- mapping = new GraphQLMapping()
- }
- else if (graphql instanceof Closure) {
- mapping = new GraphQLMapping().build((Closure)graphql)
- }
- else if (graphql instanceof LazyGraphQLMapping) {
- mapping = ((LazyGraphQLMapping)graphql).initialize()
- }
- else if (graphql instanceof GraphQLMapping) {
- mapping = (GraphQLMapping)graphql
- }
+ null
+ }
- if (!(mapping instanceof GraphQLMapping)) {
- throw new IllegalMappingException("The static graphql property on ${entity.name} is not a Boolean, Closure, or GraphQLMapping")
- }
- verifyMapping(mapping, entity)
+ private static GraphQLMapping toGraphQLMapping(Object graphql) {
+ if (graphql == Boolean.TRUE) {
+ return new GraphQLMapping()
}
- mappings.put(entity, mapping)
- mapping
+ if (graphql instanceof Closure) {
+ return new GraphQLMapping().build((Closure) graphql)
+ }
+ if (graphql instanceof LazyGraphQLMapping) {
+ return ((LazyGraphQLMapping) graphql).initialize()
+ }
+ if (graphql instanceof GraphQLMapping) {
+ return (GraphQLMapping) graphql
+ }
+ null
}
static void verifyMapping(GraphQLMapping mapping, PersistentEntity entity) {
diff --git a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/GraphQLEntityHelperSpec.groovy b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/GraphQLEntityHelperSpec.groovy
index f28f14d1e4..ebff7518e5 100644
--- a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/GraphQLEntityHelperSpec.groovy
+++ b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/GraphQLEntityHelperSpec.groovy
@@ -19,10 +19,12 @@
package org.grails.gorm.graphql
+import org.grails.datastore.mapping.model.IllegalMappingException
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.gorm.graphql.domain.general.description.Annotation
import org.grails.gorm.graphql.domain.hibernate.description.MappingComment
import org.grails.gorm.graphql.domain.general.description.MappingDescription
+import org.grails.gorm.graphql.entity.dsl.GraphQLMapping
/**
* Created by jameskleeh on 7/25/17.
@@ -56,4 +58,60 @@ class GraphQLEntityHelperSpec extends HibernateSpec {
expect:
GraphQLEntityHelper.getDescription(entity) == 'MappingDescription class'
}
+
+ void "test getMapping returns null when no static graphql property is present"() {
+ given:
+ PersistentEntity entity = Stub(PersistentEntity) {
+ getJavaClass() >> NoGraphqlMapping
+ }
+
+ expect:
+ GraphQLEntityHelper.getMapping(entity) == null
+ }
+
+ void "test getMapping builds a default mapping from a Boolean true"() {
+ given:
+ PersistentEntity entity = Stub(PersistentEntity) {
+ getJavaClass() >> BooleanGraphqlMapping
+ }
+
+ expect:
+ GraphQLEntityHelper.getMapping(entity) instanceof GraphQLMapping
+ }
+
+ void "test getMapping throws for an unsupported graphql property type"() {
+ given:
+ PersistentEntity entity = Stub(PersistentEntity) {
+ getJavaClass() >> InvalidGraphqlMapping
+ getName() >> 'InvalidGraphqlMapping'
+ }
+
+ when:
+ GraphQLEntityHelper.getMapping(entity)
+
+ then:
+ IllegalMappingException ex = thrown(IllegalMappingException)
+ ex.message.contains('InvalidGraphqlMapping')
+ }
+
+ void "test getMapping caches the result for repeated calls with the same entity"() {
+ given:
+ PersistentEntity entity = Stub(PersistentEntity) {
+ getJavaClass() >> BooleanGraphqlMapping
+ }
+
+ expect:
+ GraphQLEntityHelper.getMapping(entity).is(GraphQLEntityHelper.getMapping(entity))
+ }
+
+ static class NoGraphqlMapping {
+ }
+
+ static class BooleanGraphqlMapping {
+ static graphql = true
+ }
+
+ static class InvalidGraphqlMapping {
+ static graphql = 42
+ }
}