Skip to content

Commit 76780ad

Browse files
puneetbehlgraemerocher
authored andcommitted
#1217 Fixes No Session found error (#1244)
* Send null instead of ZERO_PARAMETER, otherwise the closure will be created with `it`. * Fix withoutTenant closure handling, if closure is call with 0 arguments then `callable.call()`.
1 parent 18fe11d commit 76780ad

File tree

3 files changed

+106
-12
lines changed

3 files changed

+106
-12
lines changed

grails-datastore-gorm/src/main/groovy/grails/gorm/multitenancy/Tenants.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class Tenants {
199199
if (multiTenantCapableDatastore.getMultiTenancyMode().isSharedConnection()) {
200200
def i = callable.parameterTypes.length
201201
if(i == 0 ) {
202-
throw new IllegalArgumentException("Provided closure accepts too many arguments")
202+
return callable.call()
203203
} else {
204204
return multiTenantCapableDatastore.withSession { session ->
205205
return callable.call(session)
@@ -272,7 +272,7 @@ class Tenants {
272272

273273
}
274274
}
275-
}
275+
} as T
276276
}
277277

278278
/**

grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/multitenancy/transform/TenantTransform.groovy

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE
4141
import static org.codehaus.groovy.ast.ClassHelper.make
4242
import static org.codehaus.groovy.ast.tools.GeneralUtils.*
4343
import static org.grails.datastore.gorm.transform.AstMethodDispatchUtils.callD
44-
import static org.grails.datastore.mapping.reflect.AstUtils.ZERO_PARAMETERS
4544
import static org.grails.datastore.mapping.reflect.AstUtils.copyParameters
4645
import static org.grails.datastore.mapping.reflect.AstUtils.varThis
4746

@@ -70,6 +69,8 @@ class TenantTransform extends AbstractDatastoreMethodDecoratingTransformation im
7069
*/
7170
public static final int POSITION = TransactionalTransform.POSITION - 100
7271

72+
private static final Parameter[] N0_PARAMETER = null
73+
7374
@Override
7475
protected String getRenamedMethodPrefix() {
7576
return RENAMED_METHOD_PREFIX
@@ -87,13 +88,11 @@ class TenantTransform extends AbstractDatastoreMethodDecoratingTransformation im
8788

8889
ClassNode serializableClassNode = make(Serializable)
8990
ClassNode annotationClassNode = annotationNode.classNode
90-
if(CURRENT_TENANT_ANNOTATION_TYPE.equals(annotationClassNode)) {
91-
return makeDelegatingClosureCall( tenantServiceVar, "withCurrent", params( param(serializableClassNode, VAR_TENANT_ID)), originalMethodCallExpr, variableScope)
92-
}
93-
else if(WITHOUT_TENANT_ANNOTATION_TYPE.equals(annotationClassNode)) {
94-
return makeDelegatingClosureCall( tenantServiceVar, "withoutId", ZERO_PARAMETERS, originalMethodCallExpr, variableScope)
95-
}
96-
else {
91+
if (CURRENT_TENANT_ANNOTATION_TYPE.equals(annotationClassNode)) {
92+
return makeDelegatingClosureCall(tenantServiceVar, "withCurrent", params(param(serializableClassNode, VAR_TENANT_ID)), originalMethodCallExpr, variableScope)
93+
} else if (WITHOUT_TENANT_ANNOTATION_TYPE.equals(annotationClassNode)) {
94+
return makeDelegatingClosureCall(tenantServiceVar, "withoutId", N0_PARAMETER, originalMethodCallExpr, variableScope)
95+
} else {
9796
// must be @Tenant
9897
Expression annValue = annotationNode.getMember("value")
9998
if(annValue instanceof ClosureExpression) {

grails-datastore-gorm/src/test/groovy/grails/gorm/annotation/multitenancy/CurrentTenantTransformSpec.groovy

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package grails.gorm.annotation.multitenancy
22

3-
import grails.gorm.multitenancy.TenantService
4-
import org.grails.datastore.mapping.core.Datastore
53
import spock.lang.Specification
64

75
/**
@@ -21,6 +19,103 @@ class BookService {
2119
}
2220
new BookService()
2321
22+
''')
23+
when:"the list books method is invoked"
24+
def result = bookService.listBooks()
25+
26+
then:"An exception was thrown because GORM is not setup"
27+
thrown(IllegalStateException)
28+
29+
}
30+
31+
void "test @CurrentTenant transforms a service class and makes a method in current tenant handling"() {
32+
given:"A service with @CurrentTenant applied as the class level"
33+
def bookService = new GroovyShell().evaluate('''
34+
import grails.gorm.multitenancy.CurrentTenant
35+
36+
@CurrentTenant
37+
class BookService {
38+
39+
List listBooks() {
40+
return ["The Stand"]
41+
}
42+
}
43+
new BookService()
44+
45+
''')
46+
when:"the list books method is invoked"
47+
def result = bookService.listBooks()
48+
49+
then:"An exception was thrown because GORM is not setup"
50+
thrown(IllegalStateException)
51+
52+
}
53+
54+
void "test @CurrentTenant transforms a service class and a method marked with @WithoutTenant in no tenant handling"() {
55+
given:"A service with @CurrentTenant applied as the class level"
56+
def bookService = new GroovyShell().evaluate('''
57+
import grails.gorm.multitenancy.CurrentTenant
58+
import grails.gorm.multitenancy.WithoutTenant
59+
60+
@CurrentTenant
61+
class BookService {
62+
63+
@WithoutTenant
64+
List listBooks() {
65+
return ["The Stand"]
66+
}
67+
}
68+
new BookService()
69+
70+
''')
71+
when:"the list books method is invoked"
72+
def result = bookService.listBooks()
73+
74+
then:"An exception was thrown because GORM is not setup"
75+
thrown(IllegalStateException)
76+
77+
}
78+
79+
void "test @WithoutTenant transforms a service class and makes a method that is wrapped in without tenant handling"() {
80+
given:"A service with @CurrentTenant applied as the class level"
81+
def bookService = new GroovyShell().evaluate('''
82+
import grails.gorm.multitenancy.CurrentTenant
83+
import grails.gorm.multitenancy.WithoutTenant
84+
85+
@WithoutTenant
86+
class BookService {
87+
88+
List listBooks() {
89+
return ["The Stand"]
90+
}
91+
}
92+
new BookService()
93+
94+
''')
95+
when:"the list books method is invoked"
96+
def result = bookService.listBooks()
97+
98+
then:"An exception was thrown because GORM is not setup"
99+
thrown(IllegalStateException)
100+
101+
}
102+
103+
void "test @WithoutTenant transforms a service class and a method marked with @CurrentTenant in current tenant handling"() {
104+
given:"A service with @CurrentTenant applied as the class level"
105+
def bookService = new GroovyShell().evaluate('''
106+
import grails.gorm.multitenancy.CurrentTenant
107+
import grails.gorm.multitenancy.WithoutTenant
108+
109+
@WithoutTenant
110+
class BookService {
111+
112+
@CurrentTenant
113+
List listBooks() {
114+
return ["The Stand"]
115+
}
116+
}
117+
new BookService()
118+
24119
''')
25120
when:"the list books method is invoked"
26121
def result = bookService.listBooks()

0 commit comments

Comments
 (0)