question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Java 8 Datetime in Requests

See original GitHub issue

Describe the problem you’d like to have solved

At the moment, when we try to set a custom List to a user’s user_metadata that contains Objects with a Java 8 Date (an Instant in this case), we get the following error message:

com.auth0.exception.Auth0Exception: Couldn't create the request body.
	at com.auth0.net.ExtendedBaseRequest.createRequest(ExtendedBaseRequest.java:51)
	at com.auth0.net.BaseRequest.execute(BaseRequest.java:33)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.Instant` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.auth0.json.mgmt.users.User["user_metadata"]->java.util.LinkedHashMap["consents"]->java.util.LinkedList[0]->com.claas.id.backend.backendservice.model.user.Consent["created_at"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)

We use Spring Boot and have a @Bean declared in our Project with an ObjectMapper that has the JavaTimeModule registered. When using this Bean, the conversion works without problems. But it looks like this library internally instantiates a separate ObjectMapper without module registration which causes this error. If I’m doing something wrong, please correct me

Describe the ideal solution

The ideal solution would be that the Auth0 ObjectMapper searches for Modules: new ObjectMapper()..findAndRegisterModules();

Alternatives and current work-arounds

Currently, we don’t have a work-around other than not using Java 8 Time formats

Additional information, if any

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
poovamrajcommented, Jul 25, 2022

Hi @larsf96, I was checking out this issue and unfortunately the SDK doesn’t have the ability to do the customization you require.

Ideally, the CustomRequest being extensible should provide an instance of the body which should be customized in the createRequestBody. But currently, we are not exposing an instance of the body where you can do this.

A better way is to provide a lambda parameter with an instance of the body where you can customize it. This way you wouldn’t need to extend a class unnecessarily.

I am thinking of a syntax like this

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();

ManagementAPI mgmt = new ManagementAPI("", "");

User newUser = new User();
Map<String,Object> userMetadata = new LinkedHashMap<>();
userMetadata.put("anyUser", Instant.now());
newUser.setUserMetadata(userMetadata);
CustomRequest<User> customRequest = (CustomRequest<User>) mgmt.users().update("newUser", newUser);
customRequest.customizeRequestBody((body, parameter) -> {
    try {
        return objectMapper.writeValueAsString(body);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return null;
    }
}).execute();

I am going to leave this out here to gather some feedback from others and also from my team. If all good, we can integrate this as a solution.

0reactions
jimmyjamescommented, Sep 6, 2022

I think enabling richer customization of requests is something we should look into adding, most likely in v2 so we can consider simplifying the request building. Support for java 8 datetime formats is also something we should add in v2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enabling Java8 DateTime API support in SpringBoot breaks ...
I have a requirement to accept LocalDate in Spring @PathVariable and also to return LocalDate and LocalDateTime as TimeStamps in Response ...
Read more >
Java SE 8 Date and Time - Oracle
The first classes you will probably encounter when using the new API are LocalDate and LocalTime . They are local in the sense...
Read more >
New Date-Time API in Java 8 - GeeksforGeeks
New date-time API is introduced in Java 8 to overcome the following drawbacks of old date-time API : Not thread safe : Unlike...
Read more >
Rest Controller — Configure Date & Time Format for Request ...
Get Started with Spring - Working with Java 8 Date and Time format pattern for request parameters for in RestController.
Read more >
Java 8 - New Date/Time API - Tutorialspoint
LocalDate/LocalTime and LocalDateTime classes simplify the development where timezones are not required. Let's see them in action. Create the following java ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found