Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dates default output format causes deserialization error #14

Closed
apenlor opened this issue Nov 1, 2023 · 2 comments · Fixed by #24
Closed

Dates default output format causes deserialization error #14

apenlor opened this issue Nov 1, 2023 · 2 comments · Fixed by #24
Assignees
Labels
bug Something isn't working
Milestone

Comments

@apenlor
Copy link
Contributor

apenlor commented Nov 1, 2023

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"])
@apenlor apenlor added the bug Something isn't working label Nov 1, 2023
@apenlor apenlor moved this from Todo to Planning in PactDslBuilder - annotation processor Nov 2, 2023
@jemacineiras jemacineiras self-assigned this Nov 2, 2023
@apenlor apenlor added this to the 1.1.0 RELEASE milestone Nov 2, 2023
@jemacineiras
Copy link
Contributor

Those errors are expected since you are not using dates or datetimes following the default format. What I will suggest is allow the example annotation to support a format, so you can add value and its format.

@apenlor apenlor modified the milestone: 1.1.0 RELEASE Nov 9, 2023
@apenlor apenlor moved this from Planning to In Progress in PactDslBuilder - annotation processor Nov 21, 2023
@apenlor
Copy link
Contributor Author

apenlor commented Nov 21, 2023

Work done in this ticket covers this issue as well: #10

jemacineiras added a commit that referenced this issue Nov 22, 2023
jemacineiras added a commit that referenced this issue Nov 22, 2023
jemacineiras added a commit that referenced this issue Nov 22, 2023
jemacineiras added a commit that referenced this issue Nov 22, 2023
jemacineiras added a commit that referenced this issue Nov 29, 2023
jemacineiras added a commit that referenced this issue Dec 1, 2023
jemacineiras added a commit that referenced this issue Dec 1, 2023
jemacineiras added a commit that referenced this issue Dec 11, 2023
jemacineiras added a commit that referenced this issue Dec 20, 2023
jemacineiras added a commit that referenced this issue Dec 20, 2023
jemacineiras added a commit that referenced this issue Jan 8, 2024
jemacineiras added a commit that referenced this issue Jan 8, 2024
jemacineiras added a commit that referenced this issue Jan 8, 2024
jemacineiras added a commit that referenced this issue Jan 8, 2024
jemacineiras added a commit that referenced this issue Jan 17, 2024
* Building test

* Mocking Random Generator.

* Add custom Dates Format

* Add Test

* Add Test

* Add Test

* Add Test

* Standardize test section

* README.md codacy corrections

* README.md codacy corrections

* README.md codacy corrections

* Fix import & format

* #14 Merge Master fix things

* Align style files with main branch

* #14 Merge Master fix things

* #14 Fix MarkdownLint complains

* #14 fix Codacy issues

* #14 Merge Master fix things

* Short random values limitation fix

* BigInteger imports missed

* #14 Fix numbers

* #14 Fix Date Parser

* #14 Fix Date Parser

* #14 Merge with Master

* #14 Merge with Master

* 49-increment-coverage-for-testing

* #49 Version bump

---------

Co-authored-by: Jose Enrique García Maciñeiras <joseenrique.garcia@sngular.com>
Co-authored-by: Alejandro Pena Lorenzo <alejandro.pena@sngular.com>
Co-authored-by: Jose E. Garcia Maciñeiras <68995937+jemacineiras@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging a pull request may close this issue.

2 participants