8000 Powertools method of identifying request handler method doesn't work with Handler class hierarchies · Issue #1056 · aws-powertools/powertools-lambda-java · GitHub
[go: up one dir, main page]

Skip to content
Powertools method of identifying request handler method doesn't work with Handler class hierarchies #1056
Closed
@humanzz

Description

@humanzz

My code base has several Lambda handler classes. I'm implementing an abstract base handler class with some common behaviours. I then implement a lambda specific handler extending the abstract base handler.

...
import software.amazon.codeguruprofilerjavaagent.RequestHandlerWithProfiling;
...

@Log4j2
public abstract class RequestHandler<I, O> extends RequestHandlerWithProfiling<I, O> {

    static {
        // Override the default dimensions to no dimensions by setting the defaultDimensions to an empty list
        MetricsUtils.defaultDimensions();
    }

    @Override
    @Tracing(segmentName = "Handler")
    @Logging(clearState = true)
    @Metrics(captureColdStart = false) // Disable coldStart metric as it requires non-empty dimensions
    public O requestHandler(I i, Context context) {
        try {
            return handle(i, context);
        } catch (Exception e) {
            ....
        }


       public abstract O handle(I i, Context context);
}

What were you trying to accomplish?

In a concrete handler, extending the abstract RequestHandler above, I wanted to use the @Validation utility

public class ConcreteHandler extends RequestHandler<SQSEvent, Void> {
     ...
    @Override
    @Validation(inboundSchema = ..., envelope = ...)
    public Void handle(SQSEvent event, Context context) {
        ...
        return null;
    }
    ...
}

Expected Behavior

The @Validation to work.

Current Behavior

@Validation did not result in validating the request. When debugging, I found the following

  1. ValidationAspect leverage's core's LambdaHandlerProcessor to identify handler methods https://github.com/awslabs/aws-lambda-powertools-java/blob/master/powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/internal/ValidationAspect.java#L58 and skips validation if the method is identified as a handler
  2. LambdaHandlerProcessor makes some strong assumptions about handler method name https://github.com/awslabs/aws-lambda-powertools-java/blob/master/powertools-core/src/main/java/software/amazon/lambda/powertools/core/internal/LambdaHandlerProcessor.java#L39

The end result, is that @Validation under my setup becomes a no-op.

Possible Solution

I'm reporting the above with an example of @Validation annotation, but I believe most other Powertools utilities do share a similar logic in their *Aspect.java classes to leverage LambdaHandlerProcessor for identifying handler methods.

A possible solution is to maybe simply rely on method signature only, rather than forcing a name as already implemented in LambdaHandlerProcessor.placedOnRequestHandler() and LambdaHandlerProcessor.placedOnStreamHandler() and to drop the LambdaHandlerProcessor.isHandlerMethod() check completely.

Steps to Reproduce (for bugs)

Environment

  • Powertools version used: 1.14.0
  • Packaging format (Layers, Maven/Gradle):
  • AWS Lambda function runtime:
  • Debugging logs

How to enable debug mode**

# paste logs here

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0