Skip to content

Commit 2c957c6

Browse files
committed
Update Date handling.
1 parent e533b5f commit 2c957c6

7 files changed

Lines changed: 31 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Method parameters may be any of the following types:
106106
* `java.time.LocalDateTime`
107107
* `java.time.Duration`
108108
* `java.time.Period`
109+
* `java.util.Date`
109110
* `java.util.UUID`
110111

111112
The following multi-value types are also supported:
@@ -118,9 +119,7 @@ Additionally, `java.util.Map`, bean, record, and `org.w3c.dom.Document` types ar
118119

119120
The `FormData` annotation can be used to indicate that a handler method accepts [form data](https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4). Arguments of type `jakarta.servlet.http.Part` may be used with requests submitted as [multi-part](https://jakarta.ee/specifications/servlet/6.1/jakarta-servlet-spec-6.1#_MultipartConfig) form data.
120121

121-
Argument values are parsed from their string representations. Unspecified values are automatically converted to 0, `false`, or the null character for primitive types.
122-
123-
`List`, `Set`, and array elements are converted to their declared types. If no values are provided for a list, set, or array parameter, an empty instance (not `null`) will be passed to the method.
122+
Unspecified values are automatically converted to 0, `false`, or the null character for primitive types. `List`, `Set`, and array elements are converted to their declared types. If no values are provided for a list, set, or array parameter, an empty instance (not `null`) will be passed to the method.
124123

125124
If a provided value cannot be coerced to the expected type, an HTTP 403 (forbidden) response will be returned. If no method is found that matches the provided arguments, HTTP 405 (method not allowed) will be returned.
126125

kilo-client/src/main/java/org/httprpc/kilo/beans/BeanAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ private static Object toRawType(Object value, Class<?> type) {
822822
if (value instanceof Number number) {
823823
return new Date(number.longValue());
824824
} else {
825-
throw new IllegalArgumentException("Value is not a number.");
825+
return new Date(Long.parseLong(value.toString()));
826826
}
827827
} else if (type == Instant.class) {
828828
if (value instanceof Number number) {

kilo-client/src/main/java/org/httprpc/kilo/io/JSONEncoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ private void encode(Map<?, ?> map, Writer writer) throws IOException {
177177
throw new IllegalArgumentException("Missing key.");
178178
}
179179

180+
if (key instanceof Date date) {
181+
key = date.getTime();
182+
}
183+
180184
if (i > 0) {
181185
writer.write(",");
182186
}

kilo-client/src/test/java/org/httprpc/kilo/io/JSONEncoderTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,26 @@ public void testUUID() throws IOException {
169169

170170
@Test
171171
public void testKeys() throws IOException {
172-
var instant = Instant.now();
172+
var date = new Date();
173+
var instant = date.toInstant();
173174
var localDate = LocalDate.now();
174175
var localTime = LocalTime.now();
175176
var localDateTime = LocalDateTime.now();
176177

177178
var expected = "{\n"
178-
+ " \"" + instant + "\": 1,\n"
179-
+ " \"" + localDate + "\": 2,\n"
180-
+ " \"" + localTime + "\": 3,\n"
181-
+ " \"" + localDateTime + "\": 4\n"
179+
+ " \"" + date.getTime() + "\": 1,\n"
180+
+ " \"" + instant + "\": 2,\n"
181+
+ " \"" + localDate + "\": 3,\n"
182+
+ " \"" + localTime + "\": 4,\n"
183+
+ " \"" + localDateTime + "\": 5\n"
182184
+ "}";
183185

184186
var map = mapOf(
185-
entry(instant, 1),
186-
entry(localDate, 2),
187-
entry(localTime, 3),
188-
entry(localDateTime, 4)
187+
entry(date, 1),
188+
entry(instant, 2),
189+
entry(localDate, 3),
190+
entry(localTime, 4),
191+
entry(localDateTime, 5)
189192
);
190193

191194
var actual = encode(map);

kilo-test/src/main/java/org/httprpc/kilo/test/TestService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.time.Period;
4141
import java.util.ArrayList;
4242
import java.util.Arrays;
43+
import java.util.Date;
4344
import java.util.HashMap;
4445
import java.util.HashSet;
4546
import java.util.Iterator;
@@ -102,8 +103,8 @@ public interface Response {
102103
boolean getFlag();
103104
char getCharacter();
104105
DayOfWeek getDayOfWeek();
105-
Long getDate();
106-
List<Long> getDates();
106+
Date getDate();
107+
List<Date> getDates();
107108
Instant getInstant();
108109
LocalDate getLocalDate();
109110
LocalTime getLocalTime();
@@ -182,7 +183,7 @@ public Number next() {
182183
@RequestMethod("GET")
183184
public Response testGet(@Required String string, List<String> strings,
184185
Integer number, Set<Integer> numbers, boolean flag, char character, DayOfWeek dayOfWeek,
185-
Long date, List<Long> dates,
186+
Date date, List<Date> dates,
186187
Instant instant, LocalDate localDate, LocalTime localTime, LocalDateTime localDateTime,
187188
Duration duration, Period period,
188189
UUID uuid) {
@@ -317,7 +318,7 @@ public Coordinates testPostCoordinates(Coordinates coordinates) {
317318
@ResourcePath("form-data")
318319
@FormData
319320
public Map<String, Object> testPostFormData(@Required String string, List<String> strings,
320-
Integer number, Long date,
321+
Integer number, Date date,
321322
Part file, List<Part> files) {
322323
var fileSize = 0L;
323324

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public void testGet() throws IOException {
102102
assertTrue(result.getFlag());
103103
assertEquals('a', result.getCharacter());
104104
assertEquals(dayOfWeek, result.getDayOfWeek());
105-
assertEquals(date.getTime(), result.getDate());
106-
assertEquals(listOf(date.getTime()), result.getDates());
105+
assertEquals(date, result.getDate());
106+
assertEquals(listOf(date), result.getDates());
107107
assertEquals(instant, result.getInstant());
108108
assertEquals(localDate, result.getLocalDate());
109109
assertEquals(localTime, result.getLocalTime());

kilo-test/src/test/resources/org/httprpc/kilo/test/test.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"iterable": false,
147147
"keyType": null,
148148
"map": false,
149-
"name": "Long",
149+
"name": "Date",
150150
"valueType": null
151151
}
152152
},
@@ -161,7 +161,7 @@
161161
"iterable": false,
162162
"keyType": null,
163163
"map": false,
164-
"name": "Long",
164+
"name": "Date",
165165
"valueType": null
166166
},
167167
"intrinsic": true,
@@ -1099,7 +1099,7 @@
10991099
"iterable": false,
11001100
"keyType": null,
11011101
"map": false,
1102-
"name": "Long",
1102+
"name": "Date",
11031103
"valueType": null
11041104
}
11051105
},
@@ -2133,7 +2133,7 @@
21332133
"iterable": false,
21342134
"keyType": null,
21352135
"map": false,
2136-
"name": "Long",
2136+
"name": "Date",
21372137
"valueType": null
21382138
}
21392139
},
@@ -2149,7 +2149,7 @@
21492149
"iterable": false,
21502150
"keyType": null,
21512151
"map": false,
2152-
"name": "Long",
2152+
"name": "Date",
21532153
"valueType": null
21542154
},
21552155
"intrinsic": true,

0 commit comments

Comments
 (0)