microsoft / botbuilder-java

The Microsoft Bot Framework provides what you need to build and connect intelligent bots that interact naturally wherever your users are talking, from text/sms to Skype, Slack, Office 365 mail and other popular services.
http://botframework.com
MIT License
178 stars 115 forks source link

Activity event with timetstamp not having milli seconds results in com.fasterxml.jackson.databind.exc.InvalidFormatException #1502

Closed harinandan-reddy closed 1 year ago

harinandan-reddy commented 1 year ago

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Version

4.14.2

Describe the bug

We some time receive activity event with timetstamp not having milli seconds like 2022-09-23T09:19:29Z which results in the following error

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2022-09-23T09:19:29Z": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2022-09-23T09:19:29Z' could not be parsed at index 19; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2022-09-23T09:19:29Z": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2022-09-23T09:19:29Z' could not be parsed at index 19 at [Source: (PushbackInputStream); line: 1, column: 57] (through reference chain: com.microsoft.bot.schema.Activity["timestamp"])]

To Reproduce

Steps to reproduce the behaviour:

Run the following unit test to reproduce the error

import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import com.microsoft.bot.schema.Activity;
import java.io.IOException;
import org.junit.jupiter.api.Test;

class ActivityTest {

  @Test
  void testActivityDeserialize() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JSR310Module());
    String json =
        "{\"fromStreamingConnection\":false,\"teamsActivity\":false,\"type\":\"message\",\"timestamp\":\"2022-09-23T09:19:29Z\",\"historyDisclosed\":false}";
    assertNotNull(objectMapper.readValue(json, Activity.class));
  }
}

Expected behaviour

The Activity object should be deserialised without any error

Screenshots

NA

Additional context

NA

harinandan-reddy commented 1 year ago

Changing the annotation on Activity timestamp field should help resolve the issue

@JsonProperty("timestamp")
@JsonInclude(Include.NON_EMPTY)
@JsonFormat(
  shape = Shape.STRING,
  pattern = "yyyy-MM-dd'T'HH:mm:ss[.n]XXX",
  timezone = "UTC"
)
private OffsetDateTime timestamp;
johnataylor commented 1 year ago

Please go ahead and submit a PR for your proposed fix, linking it to this issue. Thanks.