From 38d2f8937c00afb839f23bbd98888837c7bd8a37 Mon Sep 17 00:00:00 2001 From: Patrick Boos Date: Fri, 25 Oct 2024 10:34:53 +0200 Subject: [PATCH] update: Support request bodies on all methods --- .../filter/OpenApiValidationInterceptor.java | 6 ------ .../filter/OpenApiValidationInterceptorTest.java | 11 +++++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptor.java b/spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptor.java index a8847d8..df73fd6 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptor.java +++ b/spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptor.java @@ -155,12 +155,6 @@ private List validateRequest( } private static String readBodyCatchingException(MultiReadContentCachingRequestWrapper request) { - if (!"POST".equalsIgnoreCase(request.getMethod()) - && !"PUT".equalsIgnoreCase(request.getMethod()) - && !"PATCH".equalsIgnoreCase(request.getMethod())) { - return null; - } - try { return StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8); } catch (IOException e) { diff --git a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptorTest.java b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptorTest.java index 75b1189..b539402 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptorTest.java +++ b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptorTest.java @@ -12,6 +12,8 @@ import com.getyourguide.openapi.validation.api.model.OpenApiViolation; import java.util.List; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.web.server.ResponseStatusException; class OpenApiValidationInterceptorTest extends BaseFilterTest { @@ -104,15 +106,16 @@ public void testShouldFailOnRequestViolationWithViolation() { verifyNoResponseValidation(); } - @Test - public void testShouldIgnoreBodyOnGetRequests() { - var mockData = mockSetup(MockConfiguration.builder().requestBody("{\"field\": 1}}").requestMethod("GET").build()); + @ParameterizedTest + @ValueSource(strings = {"GET", "POST", "PUT", "PATCH", "DELETE"}) + public void testShouldSupportBodyOnGetRequests(String requestMethod) { + var mockData = mockSetup(MockConfiguration.builder().requestBody("{\"field\": 1}}").requestMethod(requestMethod).build()); httpInterceptor.preHandle(mockData.request(), mockData.response(), new Object()); httpInterceptor.postHandle(mockData.request(), mockData.response(), new Object(), null); httpInterceptor.afterCompletion(mockData.request(), mockData.response(), new Object(), null); - verifyRequestValidatedAsync(mockData, null); + verifyRequestValidatedAsync(mockData, "{\"field\": 1}}"); verifyResponseValidatedAsync(mockData); }