Skip to content

Commit 063c295

Browse files
authored
Merge pull request #22 from MiPushFramework/canary
fix(server): ignore null strings as well when ignorable=true
2 parents 56ad17c + 7c82f43 commit 063c295

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

server/src/main/kotlin/moe/yuuta/server/formprocessor/FormData.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ package moe.yuuta.server.formprocessor
55
@Retention(AnnotationRetention.RUNTIME)
66
annotation class FormData(val name: String,
77
val urlEncode: Boolean = false,
8+
// If it is true, the fields with default values (String: "", Number: 0) will be removed.
9+
// If it is false, the fields with default values will be kept as 'key=' or 'key=0', etc.
10+
// Nulls are always removed.
811
val ignorable: Boolean = true)

server/src/main/kotlin/moe/yuuta/server/formprocessor/HttpForm.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ object HttpForm {
1717
try {
1818
var rawValue: Any? = field.get(obj)
1919
if (rawValue == null) continue
20+
if (data.ignorable && rawValue.toString() == "") continue
2021
try {
2122
if (rawValue.toString().toDouble() == "0".toDouble()) {
2223
if (data.ignorable) continue

server/src/test/java/moe/yuuta/server/formprocessor/HttpFormTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ public class HttpFormTest {
1010
public void shouldToBuffer() {
1111
assertEquals("normalString=haoye&" +
1212
"normalInteger=123&" +
13-
"encodeString=+++Ri+kk+a",
13+
"encodeString=+++Ri+kk+a&" +
14+
"shouldNotIgnored=&" +
15+
"shouldNotIgnored2=0",
1416
HttpForm.toBuffer(new SampleObject()).toString().trim());
1517
// Give the ignorable integer a value so it won't be ignored
1618
SampleObject sampleObject = new SampleObject();
1719
sampleObject.setIgnorableInteger(2333);
1820
assertEquals("normalString=haoye&" +
1921
"normalInteger=123&" +
2022
"encodeString=+++Ri+kk+a&" +
21-
"ignorableInteger=2333",
23+
"ignorableInteger=2333&" +
24+
"shouldNotIgnored=&" +
25+
"shouldNotIgnored2=0",
2226
HttpForm.toBuffer(sampleObject).toString().trim());
2327
}
2428
}

server/src/test/java/moe/yuuta/server/formprocessor/SampleObject.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ class SampleObject {
1313
@FormData(name = "ignorableInteger")
1414
private int ignorableInteger = 0;
1515

16+
@FormData(name = "shouldIgnored")
17+
private String shouldIgnored = "";
18+
19+
@FormData(name = "shouldIgnored2")
20+
private String shouldIgnored2 = null;
21+
22+
@FormData(name = "shouldIgnored3", ignorable = false)
23+
private String shouldIgnored3 = null;
24+
25+
@FormData(name = "shouldNotIgnored", ignorable = false)
26+
private String shouldNotIgnored = "";
27+
28+
@FormData(name = "shouldNotIgnored2", ignorable = false)
29+
private int shouldNotIgnored2 = 0;
30+
1631
public String getNormalString() {
1732
return normalString;
1833
}

0 commit comments

Comments
 (0)