Skip to content

Commit d7a8d8b

Browse files
authored
update: Support request bodies on all methods (#148)
1 parent 084c8c5 commit d7a8d8b

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

Diff for: 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,12 +155,6 @@ 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-
164158
try {
165159
return StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
166160
} catch (IOException e) {

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.getyourguide.openapi.validation.api.model.OpenApiViolation;
1313
import java.util.List;
1414
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.params.ParameterizedTest;
16+
import org.junit.jupiter.params.provider.ValueSource;
1517
import org.springframework.web.server.ResponseStatusException;
1618

1719
class OpenApiValidationInterceptorTest extends BaseFilterTest {
@@ -104,15 +106,16 @@ public void testShouldFailOnRequestViolationWithViolation() {
104106
verifyNoResponseValidation();
105107
}
106108

107-
@Test
108-
public void testShouldIgnoreBodyOnGetRequests() {
109-
var mockData = mockSetup(MockConfiguration.builder().requestBody("{\"field\": 1}}").requestMethod("GET").build());
109+
@ParameterizedTest
110+
@ValueSource(strings = {"GET", "POST", "PUT", "PATCH", "DELETE"})
111+
public void testShouldSupportBodyOnGetRequests(String requestMethod) {
112+
var mockData = mockSetup(MockConfiguration.builder().requestBody("{\"field\": 1}}").requestMethod(requestMethod).build());
110113

111114
httpInterceptor.preHandle(mockData.request(), mockData.response(), new Object());
112115
httpInterceptor.postHandle(mockData.request(), mockData.response(), new Object(), null);
113116
httpInterceptor.afterCompletion(mockData.request(), mockData.response(), new Object(), null);
114117

115-
verifyRequestValidatedAsync(mockData, null);
118+
verifyRequestValidatedAsync(mockData, "{\"field\": 1}}");
116119
verifyResponseValidatedAsync(mockData);
117120
}
118121

0 commit comments

Comments
 (0)