Skip to content

Fix GSON Double deserialization for Object fields (Issue #489)#500

Open
arnabnandy7 wants to merge 1 commit into
facebook:mainfrom
arnabnandy7:fix/automation-setting-parse
Open

Fix GSON Double deserialization for Object fields (Issue #489)#500
arnabnandy7 wants to merge 1 commit into
facebook:mainfrom
arnabnandy7:fix/automation-setting-parse

Conversation

@arnabnandy7

Copy link
Copy Markdown

Description

This PR resolves the issue where integer values inside dynamic or untyped Object fields (such as TargetingAutomation.individual_setting containing "geo": 1) were parsed by Gson as Double (e.g. 1.0). When these models were serialized back to JSON for request parameters, the decimal suffix was preserved (e.g., sending {"geo": 1.0} instead of {"geo": 1}), causing the Meta Graph/Ads API to reject the requests.

We resolve this by setting Gson's ToNumberPolicy.LONG_OR_DOUBLE strategy on all Gson instances. This ensures integers in untyped Object maps and lists are parsed as Long (retaining their integer representation upon serialization) while actual floating point numbers are parsed as Double.


Changes

1. Updated APIRequest Parameter Serialization

  • Modified convertToString(Object input) in APIRequest.java to set .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) on the inline GsonBuilder instances. This prevents integers inside raw Map/List parameter lists from receiving a decimal suffix.

2. SDK-wide Model GSON Updates (982 files)

  • Since the SDK's model classes define their own decentralized static getGson() helper methods, we updated all 982 model classes' builders to use the same LONG_OR_DOUBLE strategy. This comprehensively prevents the bug from occurring on any other dynamic Object field.

3. Added Regression Unit Test

  • Added TargetingAutomationTest.java to verify that TargetingAutomation correctly deserializes and serializes untyped settings containing integers without decimal conversion.

Verification & Test Results

Test Execution

We compiled the modified classes and ran the newly introduced TargetingAutomationTest verification suite:

$cp = Get-Content cp.txt
javac -cp "target/classes;$cp" src/test/java/com/facebook/ads/sdk/TargetingAutomationTest.java
java -cp "target/classes;src/test/java;$cp" com.facebook.ads.sdk.TargetingAutomationTest

Output

individualSetting: {geo=1}
individualSetting class: com.google.gson.internal.LinkedTreeMap
serialized: {"advantage_audience":1,"individual_setting":{"geo":1}}
TEST PASSED!

The output confirms that the integer value 1 is successfully parsed as Long and serialized correctly as 1 instead of 1.0.

- Configured ToNumberPolicy.LONG_OR_DOUBLE strategy on GSON builders in all auto-generated models (via getGson()) and in APIRequest's convertToString.

- This ensures numeric values in unstructured/dynamic Object fields (e.g. TargetingAutomation's individual_setting) are deserialized as Long (integers) rather than Double (floats like 1.0).

- Added TargetingAutomationTest to verify that integers are serialized correctly without decimal points.
@meta-cla meta-cla Bot added the CLA Signed label Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant