Skip to content

Custom condition in request mapping handler does not work as an exclusion filter [SPR-9134] #13773

@spring-projects-issues

Description

@spring-projects-issues

Mikhail Moussikhine opened SPR-9134 and commented

Use case:

@RequestMapping(...)
@CustomMapping(...)
String something() {
}

, where @CustomMapping is some custom annotation to add custom request mapping condition in addition to @RequestMapping.

Create a custom condition using RequestMappingHandlerMapping subclass and override:

@Override
protected RequestCondition<?> getCustomMethodCondition(Method method) {
  CustomMapping methodAnnotation = AnnotationUtils.findAnnotation(method, CustomMapping.class);
  if (methodAnnotation != null)
    return new CustomMappingCondition(methodAnnotation.value());
  return null;
}

This works great as an inclusion filter. If custom condition succeeds, then function something() would be chosen as a matching candidate in RequestMappingHandlerMapping.lookupHandlerMethod().

The problem arises, when custom condition wants to exclude a function (condition failed). Normally (at least that is what built-in conditions do), if condition failed, it would return NULL from RequestCondition.getMatchingCondition(), which would cause RequestMappingInfo.getMatchingCondition() to return NULL, which would exclude the current mapping from possible matches.

However, in the case of the custom condition, the logic is somewhat different. RequestConditionHolder.getMatchingCondition() is called, which in turn calls condition.getMatchingCondition(). Even if condition.getMatchingCondition() returned NULL (which would indicate that condition has failed, right?), RequestConditionHolder.getMatchingCondition() would always return either itself or a new RequestConditionHolder(). This means that "custom" in:

RequestMappingInfo::getMatchingCondition() {
...
RequestConditionHolder custom = customConditionHolder.getMatchingCondition(request);
if (custom == null) {
  return null;
}
...
}

is never going to be NULL and would never cause and exclusion of the match based on the custom condition.


Affects: 3.1.1

Attachments:

Sub-tasks:

Referenced from: commits 8faa2e6, 64ee5e5

1 votes, 2 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions