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

Support binding RouteMatch in server filter methods #10961

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

yawkat
Copy link
Member

@yawkat yawkat commented Jul 9, 2024

This PR adds support for RouteMatch parameters to server filter methods.

Unfortunately, binding was previously fixed for the server and client. This code had no access to RouteMatch (http does not depend on router). I refactored the code to move the arg binding into BaseFilterProcessor. This is then overridden in the server implementation to add RouteMatch binding support.

I also moved ServerHttpRequest binding there while I was at it.

I've tried to keep the move limited. Most of the arg binding code is still in MethodFilter. This avoids having to expose a dozen private classes.

Fixes #10942

This PR adds support for RouteMatch parameters to server filter methods.

Unfortunately, binding was previously fixed for the server and client. This code had no access to RouteMatch (http does not depend on router). I refactored the code to move the arg binding into BaseFilterProcessor. This is then overridden in the server implementation to add RouteMatch binding support.

I also moved ServerHttpRequest binding there while I was at it.

I've tried to keep the move limited. Most of the arg binding code is still in MethodFilter. This avoids having to expose a dozen private classes.

Fixes #10942
@yawkat yawkat added the type: improvement A minor improvement to an existing feature label Jul 9, 2024
@yawkat yawkat added this to the 4.6.0 milestone Jul 9, 2024
@yawkat yawkat requested review from graemerocher and dstepanov July 9, 2024 09:26
@dstepanov
Copy link
Contributor

Looks overcomplicated. We can simple remove restriction: if (annotationMetadata.hasStereotype(Bindable.class)) because it's being ignored in BaseFilterProcessor anyway. And add some inherited method in BaseFilterProcessor.

@yawkat
Copy link
Member Author

yawkat commented Jul 9, 2024

@dstepanov it's not possible to skip filter execution for non-routematch requests that way. it'd have to involve some changes to the binding logic that i'd rather avoid

}
if (argument.isAssignableFrom(RouteMatch.class)) {
if (!argument.isNullable()) {
builder.addFilterCondition(ctx -> ctx.request().getAttribute(HttpAttributes.ROUTE_MATCH).isPresent());
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this always be conditional? I mean why would you want an error from the filter if there is not RouteMatch? Also this seems to indicate that null will never be passed.

Copy link
Member Author

Choose a reason for hiding this comment

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

nullable -> no condition -> filter is executed even for static resources (with null as the arg)

not nullable -> condition -> filter is not executed for static resources (orElse is never hit)

if (!argument.isNullable()) {
builder.addFilterCondition(ctx -> ctx.request().getAttribute(HttpAttributes.ROUTE_MATCH).isPresent());
}
return (FilterArgBinder) ctx -> ctx.request().getAttribute(HttpAttributes.ROUTE_MATCH).orElse(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

should probably be orElseThrow() and some internal server error or something

return null;
};
}
} else if (argumentType == FilterContinuation.class) {
Copy link
Contributor

Choose a reason for hiding this comment

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

might be good to cleanup all of this if/else into individual binders

@dstepanov
Copy link
Contributor

@yawkat Just create a new interface

interface FilterPredicate {

boolean test(MutablePropagatedContext mutablePropagatedContext,
        HttpRequest<?> request,
        @Nullable HttpResponse<?> response,
        @Nullable Throwable failure)

}

If ArgumentBinder implements FilterPredicate then convert it to Predicate<FilterMethodContext>

@yawkat
Copy link
Member Author

yawkat commented Jul 16, 2024

got a bit more ugly because i have to check for nullability as well, but it works

Copy link

@yawkat yawkat requested a review from graemerocher July 17, 2024 15:16
@graemerocher
Copy link
Contributor

@yawkat can you add documentation?

@yawkat
Copy link
Member Author

yawkat commented Jul 19, 2024

@graemerocher it's documented in RequestFilter and ResponseFilter where we also document all the other supported argument types

@graemerocher graemerocher merged commit 436cf06 into 4.6.x Jul 19, 2024
17 checks passed
@graemerocher graemerocher deleted the filter-route-match branch July 19, 2024 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Expose AnnotationMetadata of the route match in server filters
3 participants