Skip to content

Serializing a Mail object to/from JSON does not result in equivalent objects. #189

Closed

Description

@efenderbosch

Issue Summary

Serializing a Mail object to/from JSON does not result in equivalent objects.

Steps to Reproduce

Email to = new Email("foo@bar.com");
Content content = new Content(MIMETYPE_HTML, "test");
Email from = new Email("no-reply@bar.com");
Mail mail = new Mail(from, "subject", to, content);

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(mail);
Mail deserialized = mapper.readValue(json, Mail.class);

System.out.println(ReflectionToStringBuilder.toString(deserialized));
assertThat(deserialized, sameBeanAs(mail));

sameBeanAs is in the shazamcrest library and performs a recursive deep equality check.

The first issue is that the from attribute is not deserialized correctly:
com.sendgrid.Mail@401e7803[from=<null>,subject=subject,personalization=[com.sendgrid.Personalization@43a0cee9],content=[com.sendgrid.Content@eb21112],attachments=<null>,templateId=<null>,sections=<null>,headers=<null>,categories=<null>,customArgs=<null>,sendAt=0,batchId=<null>,asm=<null>,ipPoolId=<null>,mailSettings=<null>,trackingSettings=<null>,replyTo=<null>]

This seems to be because the Mail object has public attributes and non-standard getters:

public class Mail {
    @JsonProperty("from")
    public Email from;

    //...

    @JsonProperty("from")
    public Email getFrom(Email from) {
        return from;
    }

    public void setFrom(Email from) {
        this.from = from;
    }

    // ...
}

The goal is to write a Mail object to a queue so that we can have a process that manages a circuit breaker, rate limiting, retries and DLQ behavior. Right now, I can write the JSON to the queue, but it doesn't deserialize properly when being read.

Technical details:

  • sendgrid-java Version: 3.2.0 from Maven Central
  • Java Version: 1.8
  • Jackson Version: 2.8.7

Activity

  1. added
    status: help wanted
    type: community enhancement
    on Mar 27, 2017
  2. thinkingserious commented on Mar 27, 2017

    @thinkingserious
    Contributor

    Thanks for alerting us to this issue @efenderbosch!

    I have added this to our backlog for implementation.

    This issue can rise in our queue via additional +1's or comments on this issue or it can move up our queue the fastest with a PR.

    Thanks!

    Elmer

  3. sean-shou commented on May 8, 2017

    @sean-shou

    3.2.1 still have this issue. My template solution is create my own 'mail' object.

    public class MyMail extends Mail{
    public MyDesktopMail() {
    }

    public MyMail(Email from, String subject, Email to, Content content) {
        super(from, subject, to, content);
    }
    
    @Override
    @JsonProperty("from")
    public Email getFrom(Email from) {
        super.from = from;
        return from;
    }
    
  4. thinkingserious commented on May 9, 2017

    @thinkingserious
    Contributor

    Thats awesome @sean-shou :)

    Would you be open to making a PR to add this to our library as a helper?

  5. removed
    difficulty: very hard
    difficulty: medium
    on Oct 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty: hardstatus: help wantedtype: community enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions