Twift icon indicating copy to clipboard operation
Twift copied to clipboard

Fix decoding error for searchRecentTweets if there is no data

Open Tunous opened this issue 1 year ago • 2 comments

When calling searchRecentTweets method the response from Twitter might not contain data property which results in decoding error for TwitterAPIDataIncludesAndMeta.

Example response:

{
  "meta": {
    "result_count": 0
  }
}

This pull request makes data property optional which resolves decoding issue. But perhaps a better idea would be to introduce a separate type for cases where data is an Array and instead of optional return empty? Or even change Resource type of TwitterAPIDataIncludesAndMeta to always expect array? Looking at the tests there are no cases where data is of non-array type.

Tunous avatar Oct 02 '22 10:10 Tunous

Oh this is an odd one; the reason you won't find any tests with TwitterAPIDataIncludesAndMeta where Resource isn't an array is that meta typically indicates paginated responses. Search results are no exception, but it’s interesting that Twitter returns an object with no data field—one might expect the following instead:

{
  "data": [],
  "meta": {
    "result_count": 0
  }
}

I like your suggestion of changing Resource for TwitterAPIDataIncludesAndMeta to always expect an array e.g. <Resource: [Codable]> and think that might be better than having data be optional.

daneden avatar Oct 03 '22 13:10 daneden

I've changed my code to make data a required array of Resource. I think that should be enough.

I tried to make Resource a Sequence but then I'm not really sure how to create an empty sequence in added initializer.

Tunous avatar Oct 03 '22 14:10 Tunous