Skip to content

Double serialization of filtering parameter causes incorrect JSON format for filter_value #68

@KostiantynSoloviov

Description

@KostiantynSoloviov
## Issue Description
The SDK performs double serialization of the 'filtering' parameter, which makes it impossible to properly format `filter_value` according to the API documentation.

## Current Behavior
When sending a request with filtering parameter:
```python
api_instance = ReportingApi()
params = {
    "access_token": "xxx",
    "advertiser_id": "xxx",
    "report_type": "BASIC",
    "service_type": "AUCTION",
    "data_level": "AUCTION_AD",
    "dimensions": ['ad_id', 'stat_time_day'],
    "metrics": [
        "spend", "impressions", "clicks", "currency",
        "ad_name", "adgroup_id", "adgroup_name",
        "campaign_name", "campaign_id"
    ],
    "start_date": "YYYY-MM-DD",
    "end_date": "YYYY-MM-DD",
    "page": 1,
    "page_size": 1000,
    "filtering": [{
        "field_name": "ad_status",
        "filter_type": "IN",
        "filter_value": '["STATUS_ALL"]'  # JSON string as per API documentation
    }]
}
response = api_instance.report_integrated_get(**params)

The SDK performs the following transformations:

First serialization in sanitize_for_serialization
Second serialization in parameters_to_tuples because 'filtering' is marked as 'multi' format

This results in double-escaped JSON:

'[{"field_name": "ad_status", "filter_type": "IN", "filter_value": "[\\"STATUS_ALL\\"]"}]'

Expected Behavior
According to the API documentation, when filter_type is "IN", the filter_value should be a valid JSON array string. The expected format should be:

'[{"field_name": "ad_status", "filter_type": "IN", "filter_value": "[\"STATUS_ALL\"]"}]'

Root Cause
The issue appears to be caused by the automatic marking of the entire 'filtering' parameter as 'multi' format in the SDK generation, which leads to unnecessary additional JSON serialization of already properly formatted values.

Steps to Reproduce:

  1. Create a ReportingApi instance
  2. Make a request with filtering parameter where filter_type is "IN"
  3. Check the final request parameters

Code example with all required parameters:

[api_instance = ReportingApi()
response = api_instance.report_integrated_get(
    access_token="xxx",
    advertiser_id="xxx",
    report_type="BASIC",
    service_type="AUCTION",
    data_level="AUCTION_AD",
    dimensions=['ad_id', 'stat_time_day'],
    metrics=[
        "spend", "impressions", "clicks", "currency",
        "ad_name", "adgroup_id", "adgroup_name",
        "campaign_name", "campaign_id"
    ],
    start_date="YYYY-MM-DD",
    end_date="YYYY-MM-DD",
    page=1,
    page_size=1000,
    filtering=[{
        "field_name": "ad_status",
        "filter_type": "IN",
        "filter_value": '["STATUS_ALL"]'
    }]
)]

Impact
This issue makes it impossible to properly format requests according to the API documentation, forcing users to find workarounds or potentially causing API errors.
Code Location
The issue involves two main files in the SDK:

business_api_client/api/reporting_api.py - marks filtering parameter as 'multi'
business_api_client/api_client.py - contains serialization logic

Workaround
Currently, there is no clean workaround for this issue. We've tried several approaches:

Passing different formats of filter_value:

As a Python list: ["STATUS_ALL"] - results in "Not a valid string" error
As a JSON string: '["STATUS_ALL"]' - results in double escaping
With pre-escaped quotes: '["STATUS_ALL"]' - results in triple escaping

Modifying the filtering structure - not possible due to API requirements for specific format

The only way to make it work is to use direct HTTP requests instead of SDK (for example, using the requests library), but this defeats the purpose of using the SDK.
Future Solution
The proper fix would require one of the following changes in SDK:

Remove the 'multi' format marking from the 'filtering' parameter to prevent double serialization
Add special handling for the filter_value field to prevent its re-serialization
Modify the parameters_to_tuples method to check if the value is already a properly formatted JSON string

This issue likely requires updates to the Swagger/OpenAPI specification used for SDK generation to properly handle the specific requirements of the TikTok Marketing API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions