Skip to content

Conversation

@irfano
Copy link
Contributor

@irfano irfano commented Oct 23, 2025

Closes WOOMOB-1560

Description

Sometimes API returns Json array while we're expecting Json object. In this case, we used JsonObjectOrEmptyArray for long time. This class was changing those unexpected values to null.

image

In the example above, MediaDetails is a JsonObjectOrEmptyArray, and its Sizes field is also a JsonObjectOrEmptyArray. When Gson encounters MediaDetails, it uses JsonObjectOrEmptyArrayDeserializer to parse it. However, because JsonObjectOrEmptyArrayDeserializer uses a new Gson instance it, it can't parse Sizes. I tried using the same Gson instance with JsonObjectOrEmptyArrayDeserializer, but that caused a StackOverflowError, so I switched to a TypeAdapterFactory (d6fb0bc).

When the API returns MediaDetails as an array, JsonObjectOrEmptyArrayDeserializer resolves to null, which is why I made MediaDetails nullable (180671c).

Test Steps

I suggest you follow these steps on trunk first to reproduce the error, and then on the PR to verify the solution:

Case 1

  1. Go to Products.
  2. Select a product.
  3. Tap the Add photo button.
  4. Tap Take a photo.
  5. Capture a photo.
  6. Open Network Inspector and find the response from the https://public-api.wordpress.com/wp/v2/sites/.../media/... endpoint.
  7. Create a fake response by modifying the "media_details": {...}" part in the response to "media_details": [].
  8. Use this fake response with your preferred tool.
  9. Repeat steps 1-5.

Case 2

Repeat the steps in Case 1, but this time, in step 7, use the actual "media_details": {...}" object but change only the Sizes field to [].
For example:

  "media_details": {
    "width": 1440,
    "height": 1920,
    "file": "2024/06/JPEG_20251023_164055_3514015245320243876.jpg",
    "filesize": 28882,
    "sizes": [],
    "image_meta": {
      "aperture": "0",
      "credit": "",
      "camera": "",
      "caption": "",
      "created_timestamp": "0",
      "copyright": "",
      "focal_length": "0",
      "iso": "0",
      "shutter_speed": "0",
      "title": "",
      "orientation": "0",
      "keywords": []
    }
  },

Images/gif

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Add a new Gson `TypeAdapterFactory` for handling fields that should be a JSON object but might come as an empty array (`[]`), null, or a primitive from the API.

This factory ensures that if the incoming JSON token for a field is not `BEGIN_OBJECT`, it's parsed as `null` instead of causing a crash. This makes the JSON parsing more robust against inconsistent API responses.
Replaces the `JsonObjectOrEmptyArrayDeserializer` with a new `JsonObjectOrNullAdapterFactory`.
The `mediaDetails` field in the `MediaWPRESTResponse` is now nullable to prevent crashes when the API response for media items is missing this information.
@irfano irfano added this to the 23.6 milestone Oct 23, 2025
@irfano irfano added the Bug label Oct 23, 2025
@irfano irfano requested a review from hichamboushaba October 23, 2025 19:30
@irfano irfano changed the title Issue/woomob 1560 update json object or empty array Fix unexpected API response error for media upload Oct 23, 2025
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Oct 23, 2025

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App NameWooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit35123ad
Direct Downloadwoocommerce-wear-prototype-build-pr14813-35123ad.apk

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Oct 23, 2025

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App NameWooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commit35123ad
Direct Downloadwoocommerce-prototype-build-pr14813-35123ad.apk

@irfano irfano force-pushed the issue/WOOMOB-1560-update-json-object-or-empty-array branch from 60270ff to 35123ad Compare October 24, 2025 08:45
@hichamboushaba hichamboushaba self-assigned this Oct 24, 2025
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 52.63158% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 38.22%. Comparing base (66fd1d6) to head (35123ad).
⚠️ Report is 24 commits behind head on trunk.

Files with missing lines Patch % Lines
...xc/network/rest/wpapi/media/MediaWPRESTResponse.kt 0.00% 6 Missing ⚠️
...uxc/network/rest/JsonObjectOrNullAdapterFactory.kt 83.33% 2 Missing ⚠️
...d/fluxc/network/discovery/RootWPAPIRestResponse.kt 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##              trunk   #14813   +/-   ##
=========================================
  Coverage     38.22%   38.22%           
- Complexity    10081    10084    +3     
=========================================
  Files          2131     2132    +1     
  Lines        120391   120403   +12     
  Branches      16480    16484    +4     
=========================================
+ Hits          46015    46025   +10     
- Misses        69720    69722    +2     
  Partials       4656     4656           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@hichamboushaba hichamboushaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work @irfano 👏 , TIL about the AdapterFactory pattern.

I believe we might need to apply the same pattern for the JsonObjectOrFalse item, or maybe even remove it and replace its usage with the new JsonObjectOrNull as it works for all issues, but I think this can be done in a separate PR.

@irfano
Copy link
Contributor Author

irfano commented Oct 24, 2025

Good suggestion! I've opened an issue for it (WOOMOB-1580) and might look into it during my rotation if I can.

@irfano irfano merged commit 0d1b97a into trunk Oct 24, 2025
16 checks passed
@irfano irfano deleted the issue/WOOMOB-1560-update-json-object-or-empty-array branch October 24, 2025 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants