File tree Expand file tree Collapse file tree 4 files changed +25
-2
lines changed
main/kotlin/moe/yuuta/server/formprocessor
test/java/moe/yuuta/server/formprocessor Expand file tree Collapse file tree 4 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -5,4 +5,7 @@ package moe.yuuta.server.formprocessor
55@Retention(AnnotationRetention .RUNTIME )
66annotation 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 )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments