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

Avoid Instant and LocalDate GSON serialization errors (Java 17+) #48

Merged

Conversation

nachots-codefuente
Copy link
Contributor

Mailersend uses the default GSON serializer for serializing some classes when using then com.mailersend.sdk.emails.Email.serializeForSending method. The default GSON serializer uses reflection to serialize such classes; this is not compatible with a change made in the JDK for Java 17 (see JEP 403 or https://github.com/google/gson/blob/main/Troubleshooting.md#-inaccessibleobjectexception-module--does-not-opens--to-unnamed-module) regarding reflection, causing a com.google.gson.JsonIOException to be thrown when calling the com.mailersend.sdk.emails.Email.serializeForSending method.

For the Mailersend client, this problem usually happens when you pass an Object that contains fields of type java.time.LocalDate or java.time.Instant to the com.mailersend.sdk.emails.Email.addPersonalization method and then send the email using the com.mailersend.sdk.emails.Emails.send method.

GSON's suggested solution says:

Write custom Gson TypeAdapter implementations for the affected classes or change the type of your data. If this occurs for a field in one of your classes which you did not actually want to serialize or deserialize in the first place, you can exclude that field, see the user guide.

As the second option would imply changing the com.mailersend.sdk.emails.Email.addPersonalization method or restricting the classes that can be serialized, I have implemented the first solution by adding TypeAdapters for the java.time.LocalDate and java.time.Instant classes.

When serializing an email, serialize the Java type LocalDate using a
custom GSON TypeAdapter: LocalDateTypeAdapter.

LocalDateTypeAdapter just serializes LocalDate values calling
LocalDate's toString method.
When serializing an email, serialize the Java type Instant using a
custom GSON TypeAdapter: InstantTypeAdapter.

InstantTypeAdapter just serializes Instant values calling
Instant's toString method.
@johnkelesidis
Copy link
Contributor

thanks for this pr, merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants