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

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

Open
pinaf opened this issue Nov 7, 2016 · 6 comments
Open

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

pinaf opened this issue Nov 7, 2016 · 6 comments
Assignees

Comments

@pinaf
Copy link

pinaf commented Nov 7, 2016

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
Copy link
Collaborator

yepher commented Nov 7, 2016

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

@yepher
Copy link
Collaborator

yepher commented Nov 8, 2016

@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
Copy link
Author

pinaf commented Nov 8, 2016

@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
Copy link
Author

pinaf commented Nov 8, 2016

@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
Copy link
Author

pinaf commented Nov 8, 2016

@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
Copy link
Collaborator

yepher commented Nov 8, 2016

@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 yepher self-assigned this Mar 10, 2017
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

No branches or pull requests

2 participants