Skip to content

Commit 1ebd738

Browse files
authored
fix(api) Throw exception when constructing Date from invalid input String (#825)
1 parent 6bd5b4c commit 1ebd738

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

aws-api-appsync/src/main/java/com/amplifyframework/api/aws/TemporalDeserializers.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,51 +32,68 @@
3232
public final class TemporalDeserializers {
3333
/**
3434
* Deserializer of Temporal.Date, an extended ISO-8601 Date string, with an optional timezone offset.
35-
*
35+
* <p>
3636
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html> AWS AppSync AWSDate
3737
* scalar.</a>
3838
*/
3939
public static final class DateDeserializer implements JsonDeserializer<Temporal.Date> {
4040
@Override
41-
public Temporal.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
42-
throws JsonParseException {
43-
return new Temporal.Date(json.getAsString());
41+
public Temporal.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
42+
try {
43+
return new Temporal.Date(json.getAsString());
44+
} catch (IllegalArgumentException exception) {
45+
throw new JsonParseException("Failed to deserialize " +
46+
json.getAsString() + " as a Temporal.Date due to " + exception);
47+
}
48+
4449
}
4550
}
4651

4752
/**
4853
* Deserializer of Temporal.DateTime, an extended ISO-8601 DateTime string. Time zone offset is required.
49-
*
54+
* <p>
5055
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html>AWS AppSync AWSDateTime
5156
* scalar.</a>
5257
*/
5358
public static final class DateTimeDeserializer implements JsonDeserializer<Temporal.DateTime> {
5459
@Override
5560
public Temporal.DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
5661
throws JsonParseException {
57-
return new Temporal.DateTime(json.getAsString());
62+
try {
63+
return new Temporal.DateTime(json.getAsString());
64+
} catch (IllegalArgumentException exception) {
65+
throw new JsonParseException("Failed to deserialize " +
66+
json.getAsString() + " as a Temporal.DateTime due to " + exception);
67+
}
68+
5869
}
5970
}
6071

6172
/**
6273
* Deserializer of Temporal.Time, an extended ISO-8601 Time string, with an optional timezone offset.
63-
*
74+
* <p>
6475
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html>AWS AppSync AWSTime
6576
* scalar.</a>
6677
*/
6778
public static final class TimeDeserializer implements JsonDeserializer<Temporal.Time> {
6879
@Override
6980
public Temporal.Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
7081
throws JsonParseException {
71-
return new Temporal.Time(json.getAsString());
82+
try {
83+
return new Temporal.Time(json.getAsString());
84+
} catch (IllegalArgumentException exception) {
85+
throw new JsonParseException("Failed to deserialize " +
86+
json.getAsString() + " as a Temporal.Time due to " + exception);
87+
}
88+
7289
}
7390
}
7491

7592
/**
7693
* Deserializer of Temporal.Timestamp, a scalar type that represents the number of seconds elapsed
7794
* since 1970-01-01T00:00Z. Timestamps are serialized and deserialized as numbers. Negative values are also accepted
7895
* and these represent the number of seconds till 1970-01-01T00:00Z.
79-
*
96+
* <p>
8097
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html>AWS AppSync AWSTemporal
8198
* scalar.</a>
8299
*/

aws-api-appsync/src/main/java/com/amplifyframework/core/model/temporal/Temporal.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public Date(@NonNull java.util.Date date, int offsetInSeconds) {
8181
* with an optional timezone offset.
8282
*
8383
* @param text A valid extended ISO-8601 Date string, with an optional timezone offset
84+
* @throws IllegalArgumentException when text input is not a valid ISO-8601 Date string.
8485
*/
8586
public Date(@NonNull String text) {
8687
LocalDate localDate;
@@ -90,9 +91,13 @@ public Date(@NonNull String text) {
9091
localDate = LocalDate.from(odt);
9192
zoneOffset = ZoneOffset.from(odt);
9293
} catch (DateTimeParseException exception) {
93-
// Optional timezone offset not present
94-
localDate = LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
95-
zoneOffset = null;
94+
try {
95+
// Optional timezone offset not present
96+
localDate = LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
97+
zoneOffset = null;
98+
} catch (DateTimeParseException dateTimeParseException) {
99+
throw new IllegalArgumentException("Failed to create Temporal.Date object from " + text, exception);
100+
}
96101
}
97102
this.localDate = localDate;
98103
this.zoneOffset = zoneOffset;
@@ -211,9 +216,14 @@ public DateTime(@NonNull java.util.Date date, int offsetInSeconds) {
211216
* Constructs an {@link Temporal.DateTime} from a valid extended ISO-8601 DateTime string.
212217
*
213218
* @param text a valid extended ISO-8601 DateTime string
219+
* @throws IllegalArgumentException when text input is not a valid ISO-8601 DateTime string.
214220
*/
215221
public DateTime(@NonNull String text) {
216-
this.offsetDateTime = OffsetDateTime.parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
222+
try {
223+
this.offsetDateTime = OffsetDateTime.parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
224+
} catch (DateTimeParseException exception) {
225+
throw new IllegalArgumentException("Failed to create Temporal.DateTime object from " + text, exception);
226+
}
217227
}
218228

219229
/**
@@ -316,6 +326,7 @@ public Time(@NonNull java.util.Date date, int offsetInSeconds) {
316326
* Constructs an {@link Temporal.Time} from a valid, extended ISO-8601 Time string.
317327
*
318328
* @param text A valid, extended ISO-8601 Time string
329+
* @throws IllegalArgumentException when text input is not a valid ISO-8601 Time string.
319330
*/
320331
public Time(@NonNull String text) {
321332
LocalTime localTime;
@@ -325,9 +336,12 @@ public Time(@NonNull String text) {
325336
localTime = LocalTime.from(offsetTime);
326337
zoneOffset = ZoneOffset.from(offsetTime);
327338
} catch (DateTimeParseException exception) {
328-
// Optional timezone offset not present
329-
localTime = LocalTime.parse(text, DateTimeFormatter.ISO_LOCAL_TIME);
330-
zoneOffset = null;
339+
try {
340+
localTime = LocalTime.parse(text, DateTimeFormatter.ISO_LOCAL_TIME);
341+
zoneOffset = null;
342+
} catch (DateTimeParseException dateTimeParseException) {
343+
throw new IllegalArgumentException("Failed to create Temporal.Time object from " + text, exception);
344+
}
331345
}
332346
this.localTime = localTime;
333347
this.zoneOffset = zoneOffset;
@@ -400,7 +414,8 @@ public int hashCode() {
400414
@Override
401415
public String toString() {
402416
return "Temporal.Time{" +
403-
"localTime=\'" + localTime + "\'" +
417+
"localTime=\'" +
418+
"" + localTime + "\'" +
404419
", zoneOffset=\'" + zoneOffset + "\'" +
405420
'}';
406421
}

aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public void parsesExpectedFormats() {
4848
}
4949
}
5050

51+
/**
52+
* Tests that {@link Temporal.Date} constructor throws when String input is invalid.
53+
*/
54+
@Test(expected = IllegalArgumentException.class)
55+
public void parseInvalidFormat() {
56+
new Temporal.Date("2001-02-03T01:30:15");
57+
}
58+
5159
/**
5260
* An {@link Temporal.Date} may be created from a Java {@link Date}, and can
5361
* be converted back to a Java {@link Date}.

aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTimeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public void parsesExpectedFormats() {
5151
assertEquals("2001-02-03T01:30:00Z", new Temporal.DateTime("2001-02-03T01:30Z").format());
5252
}
5353

54+
/**
55+
* Tests that {@link Temporal.DateTime} constructor throws when String input is invalid.
56+
*/
57+
@Test(expected = IllegalArgumentException.class)
58+
public void parseInvalidFormat() {
59+
new Temporal.DateTime("2001-02-03T01:30:15.444+05");
60+
}
61+
5462
/**
5563
* An {@link Temporal.DateTime} may be constructed from a Java {@link Date}, and
5664
* converted back to one.

aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalTimeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public void parsesExpectedFormats() {
5353
assertEquals("01:22:00", new Temporal.Time("01:22").format());
5454
}
5555

56+
/**
57+
* Tests that {@link Temporal.Time} constructor throws when String input is invalid.
58+
*/
59+
@Test(expected = IllegalArgumentException.class)
60+
public void parseInvalidFormat() {
61+
new Temporal.Time("2001-02-03T01:30:15.444+05");
62+
}
63+
5664
/**
5765
* An {@link Temporal.Time} may be converted to and from a Java {@link Date}.
5866
* When no zone offset is provided, the Date is assumed to be relative to GMT.

0 commit comments

Comments
 (0)