Skip to content

DATACMNS-1827 - Consider Qualifier meta-annotated Sort method parameters. #474

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import java.util.Optional;
import java.util.function.Consumer;

import javax.annotation.Nullable;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.web.SortDefault.SortDefaults;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand All @@ -41,6 +42,7 @@
* @see SortHandlerMethodArgumentResolver
* @see ReactiveSortHandlerMethodArgumentResolver
* @author Mark Paluch
* @author Vedran Pavic
*/
public abstract class SortHandlerMethodArgumentResolverSupport {

Expand Down Expand Up @@ -184,10 +186,10 @@ protected String getSortParameter(@Nullable MethodParameter parameter) {

StringBuilder builder = new StringBuilder();

Qualifier qualifier = parameter != null ? parameter.getParameterAnnotation(Qualifier.class) : null;
String value = getQualifier(parameter);

if (qualifier != null && StringUtils.hasLength(qualifier.value())) {
builder.append(qualifier.value());
if (StringUtils.hasLength(value)) {
builder.append(value);
builder.append(qualifierDelimiter);
}

Expand Down Expand Up @@ -293,6 +295,19 @@ static boolean notOnlyDots(String source) {
return StringUtils.hasText(source.replace(".", ""));
}

@Nullable
private static String getQualifier(@Nullable MethodParameter parameter) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mp911de, since this piece of code duplicates the equally named method from PageableHandlerMethodArgumentResolverSupport, we could consider moving it to some utility class.

Not sure whether SpringDataAnnotationUtils is the right place, due to its (possibly outdated) javadoc?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it makes sense. I'll take care of this during the merge.


if (parameter == null) {
return null;
}

MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
MergedAnnotation<Qualifier> qualifier = annotations.get(Qualifier.class);

return qualifier.isPresent() ? qualifier.getString("value") : null;
}

/**
* Helper to easily build request parameter expressions for {@link Sort} instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ void emptyQualifierIsUsedInParameterLookup() throws Exception {
assertSupportedAndResult(parameter, PageRequest.of(2, 10), request);
}

@Test // DATACMNS-1827
void mergedQualifierIsUsedInParameterLookup() throws Exception {

MethodParameter parameter = new MethodParameter(Sample.class.getMethod("mergedQualifier", Pageable.class), 0);

MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("merged_page", "2");
request.addParameter("merged_size", "10");

assertSupportedAndResult(parameter, PageRequest.of(2, 10), request);
}

@Override
protected PageableHandlerMethodArgumentResolver getResolver() {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
Expand Down Expand Up @@ -298,5 +310,7 @@ void simpleDefaultWithContaineredExternalSort(@PageableDefault(size = PAGE_SIZE,
void noQualifiers(Pageable first, Pageable second);

void emptyQualifier(@Qualifier Pageable pageable);

void mergedQualifier(@TestQualifier Pageable pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ void emptyQualifierIsUsedInParameterLookup() {
assertSupportedAndResolvedTo(getRequestWithSort(reference, ""), parameter, reference);
}

@Test // DATACMNS-1827
void mergedQualifierIsUsedInParameterLookup() {

MethodParameter parameter = getParameterOfMethod("mergedQualifier");
Sort reference = Sort.by("bar", "foo");

assertSupportedAndResolvedTo(getRequestWithSort(reference, "merged"), parameter, reference);
}

private static Sort resolveSort(HttpServletRequest request, MethodParameter parameter) throws Exception {

SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
Expand Down Expand Up @@ -318,5 +327,7 @@ void simpleDefaultWithDirectionCaseInsensitive(
void invalid(@SortDefaults(@SortDefault({ "foo", "bar" })) @SortDefault({ "bar", "foo" }) Sort sort);

void emptyQualifier(@Qualifier Sort sort);

void mergedQualifier(@TestQualifier Sort sort);
}
}
37 changes: 37 additions & 0 deletions src/test/java/org/springframework/data/web/TestQualifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.web;

import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Vedran Pavic
* @see PageableHandlerMethodArgumentResolverUnitTests
* @see SortHandlerMethodArgumentResolverUnitTests
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@Qualifier("merged")
@interface TestQualifier {

}