From 7c45ad67c8530e129049b397f1914e46171768c3 Mon Sep 17 00:00:00 2001 From: Mikhail Bobrutskov Date: Sun, 1 Oct 2023 16:45:47 +0200 Subject: [PATCH] added mapping json to pojo from multipart --- .../parameters/ParametersHelper.java | 7 ++++ .../wizzardo/http/request/MultiPartEntry.java | 8 ++++ .../http/framework/ControllerHandlerTest.java | 40 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/main/java/com/wizzardo/http/framework/parameters/ParametersHelper.java b/src/main/java/com/wizzardo/http/framework/parameters/ParametersHelper.java index 3fb8c61..6d92178 100644 --- a/src/main/java/com/wizzardo/http/framework/parameters/ParametersHelper.java +++ b/src/main/java/com/wizzardo/http/framework/parameters/ParametersHelper.java @@ -380,6 +380,13 @@ protected static Mapper createPojoMapper(Class type, String par if (request.data() != null && contentType != null && contentType.toLowerCase().startsWith(Header.VALUE_APPLICATION_JSON.value)) return JsonTools.parse(request.data(), type); + if (request.isMultipart()) { + MultiPartEntry entry = request.entry(parameterName); + if (entry != null && entry.contentType().toLowerCase().startsWith(Header.VALUE_APPLICATION_JSON.value)) { + return JsonTools.parse(entry.asBytes(), type); + } + } + ParameterMapper m = customMappers.get(type); if (m != null) { Object[] ref = new Object[1]; diff --git a/src/main/java/com/wizzardo/http/request/MultiPartEntry.java b/src/main/java/com/wizzardo/http/request/MultiPartEntry.java index c231a21..dbe520e 100644 --- a/src/main/java/com/wizzardo/http/request/MultiPartEntry.java +++ b/src/main/java/com/wizzardo/http/request/MultiPartEntry.java @@ -60,4 +60,12 @@ public String asString() { public abstract InputStream inputStream() throws IOException; public abstract OutputStream outputStream() throws IOException; + + @Override + public String toString() { + return "MultiPartEntry{" + + "name='" + name + '\'' + + ", headers=" + headers + + '}'; + } } diff --git a/src/test/java/com/wizzardo/http/framework/ControllerHandlerTest.java b/src/test/java/com/wizzardo/http/framework/ControllerHandlerTest.java index a58b3ef..88a33c3 100644 --- a/src/test/java/com/wizzardo/http/framework/ControllerHandlerTest.java +++ b/src/test/java/com/wizzardo/http/framework/ControllerHandlerTest.java @@ -7,12 +7,15 @@ import com.wizzardo.http.framework.parameters.ParametersHelper; import com.wizzardo.http.framework.template.Model; import com.wizzardo.http.framework.template.Renderer; +import com.wizzardo.http.request.Request; import com.wizzardo.tools.evaluation.Config; +import com.wizzardo.tools.http.ContentType; import com.wizzardo.tools.io.FileTools; import com.wizzardo.tools.json.JsonTools; import com.wizzardo.tools.misc.With; import com.wizzardo.tools.reflection.FieldReflectionFactory; import com.wizzardo.tools.reflection.UnsafeTools; +import com.wizzardo.tools.security.MD5; import org.junit.Assert; import org.junit.Test; @@ -1133,4 +1136,41 @@ public void test_custom_types() throws IOException { checkException(() -> server.getUrlMapping().append("/integer", CustomTypesController.class, "integer"), IllegalStateException.class, "Cannot create renderer for int"); checkException(() -> server.getUrlMapping().append("/character", CustomTypesController.class, "character"), IllegalStateException.class, "Cannot create renderer for class java.lang.Character"); } + + + public static class TestMultipartController extends Controller { + + public static class TestDataInfo { + String name; + } + + public String test( + @Parameter(name = "data") byte[] data, + @Parameter(name = "info") TestDataInfo info + ) { + return info.name + ":" + MD5.create().update(data).asString(); + } + } + + @Test + public void testJsonAndBinaryParams() throws IOException { + byte[] data = new byte[10 * 1024 * 1024]; + new Random().nextBytes(data); + final String md5 = MD5.create().update(data).asString(); + + server.getUrlMapping() + .append("/", TestMultipartController.class, "test", Request.Method.POST); + + String name = "test-name"; + + TestMultipartController.TestDataInfo testDataInfo = new TestMultipartController.TestDataInfo(); + testDataInfo.name = name; + + String responseString = makeRequest("/") + .addByteArray("info", JsonTools.serializeToBytes(testDataInfo), "info", ContentType.JSON.value) + .addByteArray("data", data, "just some data") + .post().asString(); + + Assert.assertEquals(name + ":" + md5, responseString); + } }