Skip to content

Commit

Permalink
Support alternate date formats in StateMachine.fromJson().
Browse files Browse the repository at this point in the history
This commit fixes issue #2130.

Currently, `StateMachine.fromJson(...)` can parse JSON documents with timestamps in the format `2016-03-14T01:59:00.000Z` but it fails to parse JSON documents with timestamps in the format `2016-03-14T01:59:00Z`.

This is because we currently use `org.joda.time.format.ISODateTimeFormat.dateTime().parseDateTime(...)`, which requires fractional seconds in timestamps, for JSON date deserialization.

According to the documentation and examples in the AWS StepFunction documentation, fractional seconds in timestamps are optional.

This PR changes the date parsing logic to use `com.amazonaws.util.DateUtils.parseISO8601Date(...)` to parse dates during JSON deserialization. This method supports both the date format with fractional seconds and without fractional seconds.

This PR does not change the JSON serialization format of dates.
  • Loading branch information
mcvayc committed Feb 16, 2024
1 parent 3966385 commit 308f21c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package com.amazonaws.services.stepfunctions.builder.internal;

import com.amazonaws.util.DateUtils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Date deserialize(JsonParser jsonParser,
}

public static Date fromJson(String jsonText) {
return FORMATTER.parseDateTime(jsonText).toDate();
return DateUtils.parseISO8601Date(jsonText);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,18 @@ public void stateMachineFromJson_MalformedJson_ThrowsException() {
StateMachine.fromJson("{");
}

@Test
public void stateMachineFromJson_JsonWithTimestampWithFractionalSeconds_DoesNotThrowException() {
StateMachine.fromJson(
"{\"StartAt\":\"TestTime\",\"States\":{\"TestTime\":{\"Type\":\"Wait\",\"Timestamp\":\"2016-03-14T01:59:00.000Z\",\"End\":true}}}"
);
}

@Test
public void stateMachineFromJson_JsonWithTimestampWithoutFractionalSeconds_DoesNotThrowException() {
StateMachine.fromJson(
"{\"StartAt\":\"TestTime\",\"States\":{\"TestTime\":{\"Type\":\"Wait\",\"Timestamp\":\"2016-03-14T01:59:00Z\",\"End\":true}}}"
);
}

}

0 comments on commit 308f21c

Please sign in to comment.