Skip to content

Commit

Permalink
added mapping json to pojo from multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzardo committed Oct 1, 2023
1 parent c7bd1bb commit 7c45ad6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ protected static Mapper<Request, Object> 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];
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/wizzardo/http/request/MultiPartEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7c45ad6

Please sign in to comment.