Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(batch-exports): coerce config types in destination responses (#61815)

Ross31013eb5a468
diff --git a/products/batch_exports/backend/api/batch_export.py b/products/batch_exports/backend/api/batch_export.py
index 0116d4151879..0c421f495368 100644
--- a/products/batch_exports/backend/api/batch_export.py
+++ b/products/batch_exports/backend/api/batch_export.py
@@ -69,6 +69,7 @@ from products.batch_exports.backend.service import (
BatchExportWithNoEndNotAllowedError,
backfill_export,
cancel_running_batch_export_run,
+ coerce_config_to_declared_types,
delete_batch_export,
pause_batch_export,
sync_batch_export,
@@ -574,7 +575,8 @@ class BatchExportDestinationSerializer(serializers.ModelSerializer):
return target
- data["config"] = remove_secret_fields_recursive(data["config"])
+ config = remove_secret_fields_recursive(data["config"])
+ data["config"] = coerce_config_to_declared_types(instance.type, config)
return data
diff --git a/products/batch_exports/backend/service.py b/products/batch_exports/backend/service.py
index e35146b4e565..0eddb178e8cc 100644
--- a/products/batch_exports/backend/service.py
+++ b/products/batch_exports/backend/service.py
@@ -543,6 +543,34 @@ DESTINATION_WORKFLOWS = {
}
+def coerce_config_to_declared_types(destination_type: str, config: dict[str, typing.Any]) -> dict[str, typing.Any]:
+ """Restore stringified config values to the types declared on the destination's workflow inputs.
+
+ EncryptedJSONField stringifies values on write, so bool/int config fields read back as strings.
+ This function mirrors the coercion the worker applies in BaseBatchExportInputs.__post_init__, so
+ API responses return correct JSON types.
+ """
+ if destination_type not in DESTINATION_WORKFLOWS:
+ return config
+
+ _, workflow_inputs = DESTINATION_WORKFLOWS[destination_type]
+ field_by_name = {f.name: f for f in fields(workflow_inputs)}
+
+ coerced: dict[str, typing.Any] = {}
+ for key, value in config.items():
+ field = field_by_name.get(key)
+ if field is not None and isinstance(value, str):
+ if _field_is_type(field, bool):
+ value = value.lower() == "true"
+ elif _field_is_type(field, int):
+ try:
+ value = int(value)
+ except ValueError:
+ pass
+ coerced[key] = value
+ return coerced
+
+
class BatchExportServiceError(Exception):
"""Base class for BatchExport service exceptions."""
diff --git a/products/batch_exports/backend/tests/api/test_get.py b/products/batch_exports/backend/tests/api/test_get.py
index dfffc260319d..8813f823dbcb 100644
--- a/products/batch_exports/backend/tests/api/test_get.py
+++ b/products/batch_exports/backend/tests/api/test_get.py
@@ -160,3 +160,56 @@ def test_batch_exports_are_partitioned_by_team(client: HttpClient, temporal, org
response = get_batch_export(client, team.pk, batch_export["id"])
assert response.status_code == status.HTTP_404_NOT_FOUND, response.json()
+
+
+def test_serialization_of_destination_config(client: HttpClient, temporal, organization, team, user):
+ """Test that the destination config is serialized correctly.
+
+ Our destination config is encrypted using EncryptedJSONField, which decrypts most data types
+ (including booleans) into strings. Therefore, we want to ensure these are serialized to their
+ correct JSON types in the response.
+ """
+ destination_data = {
+ "type": "Postgres",
+ "config": {
+ "host": "127.0.0.1",
+ "port": 5432,
+ "user": "test",
+ "password": "test",
+ "schema": "public",
+ "database": "test",
+ "table_name": "batch_export_events",
+ "has_self_signed_cert": False,
+ },
+ }
+
+ batch_export_data = {
+ "name": "my-export",
+ "destination": destination_data,
+ "interval": "day",
+ "timezone": "UTC",
+ }
+
+ client.force_login(user)
+
+ response = create_batch_export_ok(
+ client,
+ team.pk,
+ batch_export_data,
+ )
+
+ response = get_batch_export(client, team.pk, response["id"])
+ assert response.status_code == status.HTTP_200_OK, response.json()
+
+ batch_export = response.json()
+
+ # Check that the destination config is returned with correct JSON types, except for user and
+ # password, which are sensitive
+ assert batch_export["destination"]["config"] == {
+ "host": "127.0.0.1",
+ "port": 5432,
+ "schema": "public",
+ "database": "test",
+ "table_name": "batch_export_events",
+ "has_self_signed_cert": False,
+ }