Skip to content

Commit 02e7e66

Browse files
authored
update: ignore unexpected body on GET/DELETE/OPTIONS/HEAD/TRACE (#91)
1 parent 17295d8 commit 02e7e66

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptor.java

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ private List<OpenApiViolation> validateRequest(
155155
}
156156

157157
private static String readBodyCatchingException(MultiReadContentCachingRequestWrapper request) {
158+
if (!"POST".equalsIgnoreCase(request.getMethod())
159+
&& !"PUT".equalsIgnoreCase(request.getMethod())
160+
&& !"PATCH".equalsIgnoreCase(request.getMethod())) {
161+
return null;
162+
}
163+
158164
try {
159165
return StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
160166
} catch (IOException e) {

spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/filter/BaseFilterTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private static void mockRequestAttributes(ServletRequest request, HashMap<String
5353

5454
protected MockSetupData mockSetup(MockConfiguration configuration) {
5555
var request = mock(MultiReadContentCachingRequestWrapper.class);
56+
when(request.getMethod()).thenReturn(configuration.requestMethod);
5657
var response = mock(ContentCachingResponseWrapper.class);
5758
var cachingRequest = mockContentCachingRequest(request, configuration);
5859
var cachingResponse = mockContentCachingResponse(response, configuration);
@@ -108,6 +109,7 @@ private MultiReadContentCachingRequestWrapper mockContentCachingRequest(
108109
MockConfiguration configuration
109110
) {
110111
var cachingRequest = mock(MultiReadContentCachingRequestWrapper.class);
112+
when(cachingRequest.getMethod()).thenReturn(configuration.requestMethod);
111113
when(contentCachingWrapperFactory.buildContentCachingRequestWrapper(request)).thenReturn(cachingRequest);
112114
if (configuration.requestBody != null) {
113115
try {
@@ -160,6 +162,8 @@ protected static class MockConfiguration {
160162
@Builder.Default
161163
private String requestBody = REQUEST_BODY;
162164
@Builder.Default
165+
private String requestMethod = "POST";
166+
@Builder.Default
163167
private String responseBody = RESPONSE_BODY;
164168
}
165169

spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptorTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ public void testShouldFailOnRequestViolationWithViolation() {
104104
verifyNoResponseValidation();
105105
}
106106

107+
@Test
108+
public void testShouldIgnoreBodyOnGetRequests() {
109+
var mockData = mockSetup(MockConfiguration.builder().requestBody("{\"field\": 1}}").requestMethod("GET").build());
110+
111+
httpInterceptor.preHandle(mockData.request(), mockData.response(), new Object());
112+
httpInterceptor.postHandle(mockData.request(), mockData.response(), new Object(), null);
113+
httpInterceptor.afterCompletion(mockData.request(), mockData.response(), new Object(), null);
114+
115+
verifyRequestValidatedAsync(mockData, null);
116+
verifyResponseValidatedAsync(mockData);
117+
}
118+
107119
@Test
108120
public void testShouldFailOnResponseViolationWithViolation() {
109121
var mockData = mockSetup(MockConfiguration.builder().shouldFailOnResponseViolation(true).build());
@@ -140,10 +152,14 @@ private void verifyNoResponseValidation() {
140152
}
141153

142154
private void verifyRequestValidatedAsync(MockSetupData mockData) {
155+
verifyRequestValidatedAsync(mockData, REQUEST_BODY);
156+
}
157+
158+
private void verifyRequestValidatedAsync(MockSetupData mockData, String requestBody) {
143159
verify(validator).validateRequestObjectAsync(
144160
eq(mockData.requestMetaData()),
145161
eq(mockData.responseMetaData()),
146-
eq(REQUEST_BODY),
162+
eq(requestBody),
147163
eq(openApiViolationHandler)
148164
);
149165
}

0 commit comments

Comments
 (0)