Skip to content

Commit

Permalink
fix(api) Throw exception when constructing Date from invalid input St…
Browse files Browse the repository at this point in the history
…ring (#825)
  • Loading branch information
hellosagar authored Sep 15, 2020
1 parent 6bd5b4c commit 1ebd738
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,68 @@
public final class TemporalDeserializers {
/**
* Deserializer of Temporal.Date, an extended ISO-8601 Date string, with an optional timezone offset.
*
* <p>
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html> AWS AppSync AWSDate
* scalar.</a>
*/
public static final class DateDeserializer implements JsonDeserializer<Temporal.Date> {
@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.
*
* <p>
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html>AWS AppSync AWSDateTime
* scalar.</a>
*/
public static final class DateTimeDeserializer implements JsonDeserializer<Temporal.DateTime> {
@Override
public Temporal.DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new Temporal.DateTime(json.getAsString());
try {
return new Temporal.DateTime(json.getAsString());
} catch (IllegalArgumentException exception) {
throw new JsonParseException("Failed to deserialize " +
json.getAsString() + " as a Temporal.DateTime due to " + exception);
}

}
}

/**
* Deserializer of Temporal.Time, an extended ISO-8601 Time string, with an optional timezone offset.
*
* <p>
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html>AWS AppSync AWSTime
* scalar.</a>
*/
public static final class TimeDeserializer implements JsonDeserializer<Temporal.Time> {
@Override
public Temporal.Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new Temporal.Time(json.getAsString());
try {
return new Temporal.Time(json.getAsString());
} catch (IllegalArgumentException exception) {
throw new JsonParseException("Failed to deserialize " +
json.getAsString() + " as a Temporal.Time due to " + exception);
}

}
}

/**
* Deserializer of Temporal.Timestamp, a scalar type that represents the number of seconds elapsed
* since 1970-01-01T00:00Z. Timestamps are serialized and deserialized as numbers. Negative values are also accepted
* and these represent the number of seconds till 1970-01-01T00:00Z.
*
* <p>
* Based on the <a href=https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html>AWS AppSync AWSTemporal
* scalar.</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -400,7 +414,8 @@ public int hashCode() {
@Override
public String toString() {
return "Temporal.Time{" +
"localTime=\'" + localTime + "\'" +
"localTime=\'" +
"" + localTime + "\'" +
", zoneOffset=\'" + zoneOffset + "\'" +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1ebd738

Please sign in to comment.