Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Failing gracefully for inconsistent APIs #82

wants to merge 1 commit into from

2 participants


I‘m using Mantle for model objects representing the objects of the Reddit API ( 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?


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.


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
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 0 deletions.
  1. +3 −0  Mantle/NSValueTransformer+MTLPredefinedTransformerAdditions.m
3  Mantle/NSValueTransformer+MTLPredefinedTransformerAdditions.m
@@ -51,6 +51,9 @@ + (NSValueTransformer *)mtl_JSONDictionaryTransformerWithModelClass:(Class)model
return [MTLValueTransformer
reversibleTransformerWithForwardBlock:^ id (NSDictionary *JSONDictionary) {
if (JSONDictionary == nil) return nil;
+ if ([JSONDictionary isKindOfClass:[NSString class]] && ((NSString *)JSONDictionary).length == 0) {
+ return nil;
+ }
NSAssert([JSONDictionary isKindOfClass:NSDictionary.class], @"Expected a dictionary, got: %@", JSONDictionary);
Something went wrong with that request. Please try again.