Skip to content

Commit dfbe6d6

Browse files
committed
Ensure that body content is adapted before being serialized.
1 parent c848e79 commit dfbe6d6

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

httprpc-client/src/main/java/org/httprpc/WebServiceProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public String getContentType() {
482482
public void encodeRequest(OutputStream outputStream) throws IOException {
483483
JSONEncoder jsonEncoder = new JSONEncoder();
484484

485-
jsonEncoder.write(body, outputStream);
485+
jsonEncoder.write(BeanAdapter.adapt(body), outputStream);
486486
}
487487
};
488488
} else if (method.equalsIgnoreCase("POST") && this.requestHandler == null) {

httprpc-test/src/test/java/org/httprpc/WebServiceProxyTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.httprpc;
1616

17+
import org.httprpc.beans.BeanAdapter;
1718
import org.junit.jupiter.api.Test;
1819

1920
import javax.imageio.ImageIO;
@@ -59,6 +60,13 @@ interface AttachmentInfo {
5960
int getChecksum();
6061
}
6162

63+
interface Body {
64+
String getString();
65+
List<String> getStrings();
66+
int getNumber();
67+
boolean getFlag();
68+
}
69+
6270
@RequestMethod("GET")
6371
@ResourcePath("a/?:a/b/?:b/c/?:c/d/?:d")
6472
Map<String, ?> testGetKeys() throws IOException;
@@ -73,7 +81,7 @@ Response testMultipartPost(String string, List<String> strings, int number, bool
7381
List<URL> attachments) throws IOException;
7482

7583
@RequestMethod("POST")
76-
Map<String, ?> testBodyPost(int id) throws IOException;
84+
Body testBodyPost(int id) throws IOException;
7785

7886
@RequestMethod("GET")
7987
@ResourcePath("headers")
@@ -252,25 +260,25 @@ public void testMultipartPost() throws IOException {
252260

253261
@Test
254262
public void testCustomBodyPost() throws IOException {
255-
Map<String, ?> content = mapOf(
263+
TestService.Body body = BeanAdapter.adapt(mapOf(
256264
entry("string", "héllo&gøod+bye?"),
257265
entry("strings", listOf("a", "b", "c")),
258266
entry("number", 123L),
259267
entry("flag", true)
260-
);
268+
), TestService.Body.class);
261269

262270
TestService testService = WebServiceProxy.adapt(new URL(serverURL, "test/"), TestService.class,
263271
(resourcePath) -> null, (method, url) -> {
264272
WebServiceProxy webServiceProxy = new WebServiceProxy(method, url);
265273

266-
webServiceProxy.setBody(content);
274+
webServiceProxy.setBody(body);
267275

268276
return webServiceProxy;
269277
});
270278

271-
Map<String, ?> result = testService.testBodyPost(101);
279+
TestService.Body result = testService.testBodyPost(101);
272280

273-
assertEquals(content, result);
281+
assertEquals(body, result);
274282
}
275283

276284
@Test

0 commit comments

Comments
 (0)