Skip to content

Commit f82cd61

Browse files
committed
Fix bug with traits with @action that take parameters not working correctly when implemented by controllers
1 parent c043013 commit f82cd61

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

grails-plugin-controllers/src/main/groovy/org/grails/compiler/web/ControllerActionTransformer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
9292
import org.codehaus.groovy.syntax.Token;
9393
import org.codehaus.groovy.syntax.Types;
94+
import org.codehaus.groovy.transform.trait.Traits;
9495
import org.grails.compiler.injection.GrailsASTUtils;
9596
import org.grails.core.DefaultGrailsControllerClass;
9697
import org.grails.core.artefact.ControllerArtefactHandler;
@@ -293,12 +294,22 @@ public Object call(Object object) {
293294
* @return true if the method should be configured as a controller action, false otherwise
294295
*/
295296
protected boolean methodShouldBeConfiguredAsControllerAction(final MethodNode method) {
297+
int minLineNumber = 0;
298+
// Methods inherited from traits marked with @Action that take a parameter:
299+
// Remove the @Action annotation so they will be processed to create the no-arg method
300+
if (method.getAnnotations(ClassHelper.make(Traits.TraitBridge.class)).size() > 0) {
301+
List<AnnotationNode> annotations = method.getAnnotations(ACTION_ANNOTATION_NODE.getClassNode());
302+
if (annotations.size() > 0 && method.getParameters().length > 0) {
303+
method.getAnnotations().removeAll(annotations);
304+
--minLineNumber;
305+
}
306+
}
296307
return !method.isStatic() &&
297308
method.isPublic() &&
298309
!method.isAbstract() &&
299310
method.getAnnotations(ACTION_ANNOTATION_NODE.getClassNode()).isEmpty() &&
300311
method.getAnnotations(new ClassNode(ControllerMethod.class)).isEmpty() &&
301-
method.getLineNumber() >= 0 &&
312+
method.getLineNumber() >= minLineNumber &&
302313
!method.getName().startsWith("$") &&
303314
!method.getReturnType().getName().equals(VOID_TYPE) &&
304315
!isExceptionHandlingMethod(method);

grails-plugin-controllers/src/test/groovy/org/codehaus/groovy/grails/compiler/web/ControllerActionTransformerSpec.groovy

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,36 @@ class ControllerActionTransformerSpec extends Specification {
169169
42 == model.paramValue
170170
}
171171

172-
/* void "Test annotated controllers"() {
173-
when:
172+
173+
void "test controller with trait action with params"() {
174+
given:
174175
def cls = gcl.parseClass('''
175-
class AnnotatedControllerTransformer1Controller {
176-
def action = {
176+
@grails.artefact.Artefact('Controller')
177+
class TestTraitActionToController implements ShowMethod {
178+
179+
180+
}
181+
class MyCommandWithArg implements grails.validation.Validateable {
182+
183+
}
184+
trait ShowMethod {
185+
186+
@grails.web.Action
187+
def show(MyCommandWithArg myCommandWithArg) {
188+
!myCommandWithArg.hasErrors()
177189
}
190+
178191
}
179192
''')
180-
181193
def controller = cls.newInstance()
182194

183-
then:
184-
controller
185-
controller.getClass().getMethod("action", [] as Class[]) != null
195+
when:
196+
Boolean valid = controller.show()
186197

198+
then:
199+
valid
187200
}
188-
*/
201+
189202

190203
def cleanup() {
191204
RequestContextHolder.resetRequestAttributes()

0 commit comments

Comments
 (0)