Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ignore @Cacheable Annotation on Controller Methods #209

Open
wants to merge 1 commit into
base: 7.0.x
Choose a base branch
from

Conversation

jsolas
Copy link

@jsolas jsolas commented Jun 22, 2024

I have reviewed and confirmed that this problem also occurs in version 7 of the plugin.
Related Issue:
This PR addresses the issue described in #168

Summary
This PR addresses an issue where the @Cacheable annotation on controller methods does not behave as expected in the plugin since version 4.0.0. Instead of ignoring the annotation, the method is executed only the first time as if it were being cached, causing inconsistencies when the controller action receives parameters.

Solution
To solve this issue, I have modified the buildDelegatingMethodCall method to check if the class is a controller and not a service. If it is a controller, the original method call is returned, effectively ignoring the @Cacheable annotation.

Here is the relevant code snippet:

@Override
protected Expression buildDelegatingMethodCall(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, MethodNode methodNode, MethodCallExpression originalMethodCallExpr, BlockStatement newMethodBody) {
    boolean isControllerClass = classNode.name.endsWith("Controller")
    boolean isServiceClass = classNode.name.endsWith("Service")

    if (isControllerClass && !isServiceClass) {
        return originalMethodCallExpr
    }

    // Existing logic for handling caching
}

currently when caching a method in a controller the parameter is ignored and returns the same result for all calls
boolean isControllerClass = classNode.name.endsWith('Controller')
boolean isServiceClass = classNode.name.endsWith('Service')

if (isControllerClass && !isServiceClass) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused about the if (isControllerClass && !isServiceClass) clause. I think that says if the class name ends in 'Controller' and doesn't end in 'Service', then return the original method. What is the point of checking if the class name ends in 'Service' if the condition only applies if the class name ends in 'Controller'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your observation. You are right, the condition if (isControllerClass && !isServiceClass) is redundant. Does it make sense to you to simplify it to just if (isControllerClass)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants