From 1ebd7383956a07180ef948fa690efbe29ea55e8a Mon Sep 17 00:00:00 2001
From: Sagar Khurana <50016799+hellosagar@users.noreply.github.com>
Date: Wed, 16 Sep 2020 01:25:39 +0530
Subject: [PATCH] fix(api) Throw exception when constructing Date from invalid
input String (#825)
---
.../api/aws/TemporalDeserializers.java | 35 ++++++++++++++-----
.../core/model/temporal/Temporal.java | 31 +++++++++++-----
.../core/model/temporal/TemporalDateTest.java | 8 +++++
.../model/temporal/TemporalDateTimeTest.java | 8 +++++
.../core/model/temporal/TemporalTimeTest.java | 8 +++++
5 files changed, 73 insertions(+), 17 deletions(-)
diff --git a/aws-api-appsync/src/main/java/com/amplifyframework/api/aws/TemporalDeserializers.java b/aws-api-appsync/src/main/java/com/amplifyframework/api/aws/TemporalDeserializers.java
index 84121a2740..78837e7e94 100644
--- a/aws-api-appsync/src/main/java/com/amplifyframework/api/aws/TemporalDeserializers.java
+++ b/aws-api-appsync/src/main/java/com/amplifyframework/api/aws/TemporalDeserializers.java
@@ -32,21 +32,26 @@
public final class TemporalDeserializers {
/**
* Deserializer of Temporal.Date, an extended ISO-8601 Date string, with an optional timezone offset.
- *
+ *
* Based on the AWS AppSync AWSDate
* scalar.
*/
public static final class DateDeserializer implements JsonDeserializer {
@Override
- public Temporal.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
- throws JsonParseException {
- return new Temporal.Date(json.getAsString());
+ public Temporal.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
+ try {
+ return new Temporal.Date(json.getAsString());
+ } catch (IllegalArgumentException exception) {
+ throw new JsonParseException("Failed to deserialize " +
+ json.getAsString() + " as a Temporal.Date due to " + exception);
+ }
+
}
}
/**
* Deserializer of Temporal.DateTime, an extended ISO-8601 DateTime string. Time zone offset is required.
- *
+ *
* Based on the AWS AppSync AWSDateTime
* scalar.
*/
@@ -54,13 +59,19 @@ public static final class DateTimeDeserializer implements JsonDeserializer
* Based on the AWS AppSync AWSTime
* scalar.
*/
@@ -68,7 +79,13 @@ public static final class TimeDeserializer implements JsonDeserializer
* Based on the AWS AppSync AWSTemporal
* scalar.
*/
diff --git a/aws-api-appsync/src/main/java/com/amplifyframework/core/model/temporal/Temporal.java b/aws-api-appsync/src/main/java/com/amplifyframework/core/model/temporal/Temporal.java
index 0dbe16648f..096a9f4814 100644
--- a/aws-api-appsync/src/main/java/com/amplifyframework/core/model/temporal/Temporal.java
+++ b/aws-api-appsync/src/main/java/com/amplifyframework/core/model/temporal/Temporal.java
@@ -81,6 +81,7 @@ public Date(@NonNull java.util.Date date, int offsetInSeconds) {
* with an optional timezone offset.
*
* @param text A valid extended ISO-8601 Date string, with an optional timezone offset
+ * @throws IllegalArgumentException when text input is not a valid ISO-8601 Date string.
*/
public Date(@NonNull String text) {
LocalDate localDate;
@@ -90,9 +91,13 @@ public Date(@NonNull String text) {
localDate = LocalDate.from(odt);
zoneOffset = ZoneOffset.from(odt);
} catch (DateTimeParseException exception) {
- // Optional timezone offset not present
- localDate = LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
- zoneOffset = null;
+ try {
+ // Optional timezone offset not present
+ localDate = LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
+ zoneOffset = null;
+ } catch (DateTimeParseException dateTimeParseException) {
+ throw new IllegalArgumentException("Failed to create Temporal.Date object from " + text, exception);
+ }
}
this.localDate = localDate;
this.zoneOffset = zoneOffset;
@@ -211,9 +216,14 @@ public DateTime(@NonNull java.util.Date date, int offsetInSeconds) {
* Constructs an {@link Temporal.DateTime} from a valid extended ISO-8601 DateTime string.
*
* @param text a valid extended ISO-8601 DateTime string
+ * @throws IllegalArgumentException when text input is not a valid ISO-8601 DateTime string.
*/
public DateTime(@NonNull String text) {
- this.offsetDateTime = OffsetDateTime.parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
+ try {
+ this.offsetDateTime = OffsetDateTime.parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
+ } catch (DateTimeParseException exception) {
+ throw new IllegalArgumentException("Failed to create Temporal.DateTime object from " + text, exception);
+ }
}
/**
@@ -316,6 +326,7 @@ public Time(@NonNull java.util.Date date, int offsetInSeconds) {
* Constructs an {@link Temporal.Time} from a valid, extended ISO-8601 Time string.
*
* @param text A valid, extended ISO-8601 Time string
+ * @throws IllegalArgumentException when text input is not a valid ISO-8601 Time string.
*/
public Time(@NonNull String text) {
LocalTime localTime;
@@ -325,9 +336,12 @@ public Time(@NonNull String text) {
localTime = LocalTime.from(offsetTime);
zoneOffset = ZoneOffset.from(offsetTime);
} catch (DateTimeParseException exception) {
- // Optional timezone offset not present
- localTime = LocalTime.parse(text, DateTimeFormatter.ISO_LOCAL_TIME);
- zoneOffset = null;
+ try {
+ localTime = LocalTime.parse(text, DateTimeFormatter.ISO_LOCAL_TIME);
+ zoneOffset = null;
+ } catch (DateTimeParseException dateTimeParseException) {
+ throw new IllegalArgumentException("Failed to create Temporal.Time object from " + text, exception);
+ }
}
this.localTime = localTime;
this.zoneOffset = zoneOffset;
@@ -400,7 +414,8 @@ public int hashCode() {
@Override
public String toString() {
return "Temporal.Time{" +
- "localTime=\'" + localTime + "\'" +
+ "localTime=\'" +
+ "" + localTime + "\'" +
", zoneOffset=\'" + zoneOffset + "\'" +
'}';
}
diff --git a/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTest.java b/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTest.java
index 15c59e66c8..cf844cd62f 100644
--- a/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTest.java
+++ b/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTest.java
@@ -48,6 +48,14 @@ public void parsesExpectedFormats() {
}
}
+ /**
+ * Tests that {@link Temporal.Date} constructor throws when String input is invalid.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void parseInvalidFormat() {
+ new Temporal.Date("2001-02-03T01:30:15");
+ }
+
/**
* An {@link Temporal.Date} may be created from a Java {@link Date}, and can
* be converted back to a Java {@link Date}.
diff --git a/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTimeTest.java b/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTimeTest.java
index b8f0bf6fbb..32984055ec 100644
--- a/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTimeTest.java
+++ b/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalDateTimeTest.java
@@ -51,6 +51,14 @@ public void parsesExpectedFormats() {
assertEquals("2001-02-03T01:30:00Z", new Temporal.DateTime("2001-02-03T01:30Z").format());
}
+ /**
+ * Tests that {@link Temporal.DateTime} constructor throws when String input is invalid.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void parseInvalidFormat() {
+ new Temporal.DateTime("2001-02-03T01:30:15.444+05");
+ }
+
/**
* An {@link Temporal.DateTime} may be constructed from a Java {@link Date}, and
* converted back to one.
diff --git a/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalTimeTest.java b/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalTimeTest.java
index 01287473d6..6b8e803a34 100644
--- a/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalTimeTest.java
+++ b/aws-api-appsync/src/test/java/com/amplifyframework/core/model/temporal/TemporalTimeTest.java
@@ -53,6 +53,14 @@ public void parsesExpectedFormats() {
assertEquals("01:22:00", new Temporal.Time("01:22").format());
}
+ /**
+ * Tests that {@link Temporal.Time} constructor throws when String input is invalid.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void parseInvalidFormat() {
+ new Temporal.Time("2001-02-03T01:30:15.444+05");
+ }
+
/**
* An {@link Temporal.Time} may be converted to and from a Java {@link Date}.
* When no zone offset is provided, the Date is assumed to be relative to GMT.