java-sparkpost icon indicating copy to clipboard operation
java-sparkpost copied to clipboard

Json syntax error parsing "from" field from retrieved template

Open pinaf opened this issue 8 years ago • 6 comments

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 <[email protected]>",

pinaf avatar Nov 07 '16 22:11 pinaf

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

yepher avatar Nov 07 '16 22:11 yepher

@pinaf

I ran RetrieveAllTemplatesSample and it worked. But looking at the From spec it says Address “from” : "[email protected]" or JSON object composed of the “name” and “email” fields “from” : { “name” : “My Company”, “email” : "[email protected]" } 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.

yepher avatar Nov 08 '16 15:11 yepher

@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 avatar Nov 08 '16 16:11 pinaf

@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 avatar Nov 08 '16 16:11 pinaf

@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 :)

pinaf avatar Nov 08 '16 16:11 pinaf

@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.

yepher avatar Nov 08 '16 17:11 yepher