Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Failing gracefully for inconsistent APIs #82

wants to merge 1 commit into


None yet
2 participants

myell0w commented Mar 20, 2013

I‘m using Mantle for model objects representing the objects of the Reddit API (https://github.com/reddit/reddit/wiki/JSON) and unfortunately the API behaves pretty inconsistent. Replies on a comment expect a dictionary (which I usually get), but when there are no replies the value sometimes is an empty string instead of nil.

I'm opening this Pull Request to start a discussion on how to handle such cases, I'm sure there are more problematic APIs out there - I don't know if you would want to include this kind of fallback/hack in Mantle though. Is the changed code reasonable for my use case or am I missing a better fix?

@ghost ghost assigned jspahrsummers Mar 24, 2013


jspahrsummers commented Mar 24, 2013

If these mismatches only occur for sub-objects (e.g., replies included in a comment resource), this sort of validation should be handled with a value transformer in the parent object. The comment class, rather than using the default JSON transformer, can create one that verifies the type of the field before trying to decode it as a JSON dictionary.

On the other hand, if a URI is returning only a string, that's invalid JSON. A fix like this might work, but their API is wrong if that's the case.


jspahrsummers commented Mar 29, 2013

Closing this due to inactivity. We can reopen it if the above comments are addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment