SparkPost / java-sparkpost

SparkPost client library for Java
https://www.sparkpost.com/
Other
39 stars 35 forks source link

Json syntax error parsing "from" field from retrieved template #52

Open pinaf opened 7 years ago

pinaf commented 7 years ago

Using sparkpost-lib 0.16.1.

When using ResourceTemplates.retrieve to retrieve a template, the following JsonSyntaxException occurs when parsing the from field.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 242 path $.results.content.from
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    com.google.gson.Gson.fromJson(Gson.java:810)
    com.google.gson.Gson.fromJson(Gson.java:775)
    com.google.gson.Gson.fromJson(Gson.java:724)
    com.sparkpost.model.responses.Response.decode(Response.java:47)
    com.sparkpost.resources.ResourceTemplates.retrieve(ResourceTemplates.java:38)

It seems like the java code expects a JSON object but the API returns a simple string. Here is what the from field looks like when manually hitting the API at https://api.sparkpost.com/api/v1/templates/mytemplate with postman

"from": "My Company <info@mycompany.com>",

yepher commented 7 years ago

Thank you for reporting this. We will fix this in the next release.

yepher commented 7 years ago

@pinaf

I ran RetrieveAllTemplatesSample and it worked. But looking at the From spec it says Address “from” : "deals@company.com" or JSON object composed of the “name” and “email” fields “from” : { “name” : “My Company”, “email” : "deals@company.com" } used to compose the email’s “From” header

So yes the code needs to be smarter when parsing that template response. Currently it assumes it is the object form of the from address.

Can you tell me how you created that template so I can make sure I run and test your specific use-case?

Can you provide the JSON that is returned for the template that is failing? You can sanitize it by replacing your domain names with "example.com" and substitute strings for any other personal/confidential information.

Thanks.

pinaf commented 7 years ago

@yepher so it seems that this might be an issue with the template being "old". Some of them return

"from": {
        "name": "--redacted--",
        "email": "--redacted--"
      },

But others return

"from": "name <email>",
pinaf commented 7 years ago

@yepher interesting. If I take one of the templates that return from as a string and click save & publish without changing anything, upon a subsequent API call I get a json for from

pinaf commented 7 years ago

@yepher I just confirmed that solves my problem (just re-publishing the templates). It's up to you whether you want the smarter parser or not :)

yepher commented 7 years ago

@pinaf, Awesome glad this is not holding you up. I think the code should handle the full spec so yes I think I will add it but I will make it a little lower priority now.