Skip to content

Commit

Permalink
SliceableHandlerMethodArgumentResolver でもチェックを行う
Browse files Browse the repository at this point in the history
  • Loading branch information
katagiri-kazumune committed Mar 31, 2020
1 parent 47dcbd6 commit 1d55963
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
8 changes: 7 additions & 1 deletion spar-wings-spring-data-chunk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ Repository の Sliceable パラメータで自動で設定するのは、

先頭から offset までの読み飛ばし件数が多くなることで API のパフォーマンス劣化を引き起こす可能性が高くなる為、
部分集合の先頭から 2000 件を超えて取得できないように制限します。
具体的には Sliceable パラメータの内容が `page_number * size + size > 2000` の場合、不正リクエストとみなし、InvalidSliceableException を throw します。
具体的には Sliceable パラメータの内容が `page_number * size + size > 2000` の場合、不正リクエストとみなし、

* Controller の引数に Sliceable を設定した時には HttpBadRequestException
* Repository メソッド呼び出し時には InvalidSliceableException
* Controller の引数に Sliceable を設定しない場合はハンドリングをしてください

を throw します。

### INDEX 設計

Expand Down
1 change: 1 addition & 0 deletions spar-wings-spring-data-chunk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
compile "org.springframework:spring-web"
compile "org.springframework:spring-context"
compileOnly "org.springframework:spring-tx"
compile project(":spar-wings-httpexceptions")

testCompile "com.fasterxml.jackson.core:jackson-databind"
testCompile "com.jayway.jsonpath:json-path-assert:2.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import jp.xet.sparwings.spring.data.exceptions.InvalidSliceableException;
import jp.xet.sparwings.spring.data.slice.SliceRequest;
import jp.xet.sparwings.spring.data.slice.Sliceable;
import jp.xet.sparwings.spring.web.httpexceptions.HttpBadRequestException;

/**
* Extracts paging information from web requests and thus allows injecting {@link Sliceable} instances into controller
Expand Down Expand Up @@ -171,7 +173,14 @@ public Object resolveArgument(MethodParameter methodParameter, ModelAndViewConta
log.trace("invalid page number: {}", pageNumberString);
}
}
return new SliceRequest(pageNumber, direction, pageSize);

Sliceable sliceable = new SliceRequest(pageNumber, direction, pageSize);
try {
sliceable.validate();
} catch (InvalidSliceableException e) {
throw new HttpBadRequestException(e.getMessage(), e);
}
return sliceable;
}

private Sliceable getDefaultFromAnnotationOrFallback(MethodParameter methodParameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package jp.xet.sparwings.spring.data.web;

import static org.assertj.core.api.Assertions.catchThrowable;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
Expand All @@ -36,6 +37,7 @@

import jp.xet.sparwings.spring.data.slice.SliceRequest;
import jp.xet.sparwings.spring.data.slice.Sliceable;
import jp.xet.sparwings.spring.web.httpexceptions.HttpBadRequestException;

/**
* Test for {@link SliceableHandlerMethodArgumentResolver}.
Expand Down Expand Up @@ -343,7 +345,7 @@ public void testDefaultHandlerWithValueWithPageNumberParameter() throws Exceptio
MethodParameter methodParametere = new MethodParameter(method, 0);
ModelAndViewContainer mavContainer = mock(ModelAndViewContainer.class);
NativeWebRequest webRequest = mock(NativeWebRequest.class);
when(webRequest.getParameter(eq("page_number"))).thenReturn("100");
when(webRequest.getParameter(eq("page_number"))).thenReturn("0");
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);

// exercise
Expand All @@ -354,10 +356,30 @@ public void testDefaultHandlerWithValueWithPageNumberParameter() throws Exceptio
assertThat(actual, is(instanceOf(Sliceable.class)));
Sliceable actualSliceable = (Sliceable) actual;

Sliceable expected = new SliceRequest(100, null, 2000);
Sliceable expected = new SliceRequest(0, null, 2000);
assertThat(actualSliceable, is(expected));
}

@Test
public void testDefaultHandlerWithValueWithPageNumberParameter_Over() throws Exception {
// setup
Method method = getClass().getMethod("simpleHandler", Sliceable.class);
MethodParameter methodParametere = new MethodParameter(method, 0);
ModelAndViewContainer mavContainer = mock(ModelAndViewContainer.class);
NativeWebRequest webRequest = mock(NativeWebRequest.class);
when(webRequest.getParameter(eq("page_number"))).thenReturn("1"); // over
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);

// exercise
Throwable actual =
catchThrowable(() -> sut.resolveArgument(methodParametere, mavContainer, webRequest, binderFactory));

// verify
assertThat(actual, is(notNullValue()));
assertThat(actual, is(instanceOf(HttpBadRequestException.class)));
assertThat(actual.getMessage(), is("Cannot get elements beyond 2000."));
}

@Test
public void testDefaultHandlerWithValueWithIllegalPageNumberParameter() throws Exception {
// setup
Expand Down

0 comments on commit 1d55963

Please sign in to comment.