Skip to content

Dates default output format causes deserialization error #14

Description

@apenlor

When using ZoneDateTime or Date params, the serialization generates a string that cannot be deserialized by Jackson libraries.

Model example for ZoneDateTime

@PactDslBodyBuilder
public class Student {

    @Example("idValue")
    private String id;
    @Example("nameValue")
    private String name;
    @Example("email@email.com")
    private String email;
    @Example("2023-12-03T10:15:30+01:00[Europe/Madrid]")
    private ZonedDateTime date;

}

Model example for Date

@PactDslBodyBuilder
public class Student {

    @Example("idValue")
    private String id;
    @Example("nameValue")
    private String name;
    @Example("email@email.com")
    private String email;
    @Example("Sep 27, 2023, 11:02:00 AM")
    private Date init;

}
    @Pact(consumer = "consumer-poc", provider = "provider-poc")
    public RequestResponsePact getStudent(PactDslWithProvider builder) throws JsonProcessingException {
        return builder.given("Student 1 exists")
                .uponReceiving("get student with ID 1")
                .path("/students/1")
                .method("GET")
                .willRespondWith()
                .status(200)
                .headers(Map.of("Content-Type", "application/json"))
                .body(body.build();)
                .toPact();
    }
    @Test
    @PactTestFor(pactMethod = "getStudent")
    void getStudent_whenStudentExist(MockServer mockServer) {
        RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build();
        Student student = new StudentService(restTemplate).getStudent("1");

        //assertEquals(expected, student);
    }

Error shown in the console for ZoneDateTime:

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.ZonedDateTime` from String "2023-12-03T04:15:30.000000.000-05:00[America/New_York]": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '2023-12-03T04:15:30.000000.000-05:00[America/New_York]' could not be parsed at index 26; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.ZonedDateTime` from String "2023-12-03T04:15:30.000000.000-05:00[America/New_York]": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '2023-12-03T04:15:30.000000.000-05:00[America/New_York]' could not be parsed at index 26
 at [Source: (PushbackInputStream); line: 1, column: 32] (through reference chain: com.example.pactjvm.bidirectional.consumer.model.Student["end"])

Error shown in the console for Date:

org.springframework.web.client.RestClientException: Error while extracting response for type [class com.example.pactjvm.bidirectional.consumer.model.Student] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2023-09-27[[-04:00]]": not a valid representation (error: Failed to parse Date value '2023-09-27[[-04:00]]': Cannot parse date "2023-09-27[[-04:00]]": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2023-09-27[[-04:00]]": not a valid representation (error: Failed to parse Date value '2023-09-27[[-04:00]]': Cannot parse date "2023-09-27[[-04:00]]": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
 at [Source: (PushbackInputStream); line: 1, column: 120] (through reference chain: com.example.pactjvm.bidirectional.consumer.model.Student["init"])

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status
Done

Relationships

None yet

Development

No branches or pull requests

Issue actions