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(data-imports): stop to_config crashing on flat select payloads (#63325)

Tom Owers72e4899d90e8
diff --git a/posthog/temporal/data_imports/sources/common/config.py b/posthog/temporal/data_imports/sources/common/config.py
index 264574e3d8d2..f3c9d40e669d 100644
--- a/posthog/temporal/data_imports/sources/common/config.py
+++ b/posthog/temporal/data_imports/sources/common/config.py
@@ -214,7 +214,13 @@ def to_config(
# be set
assert field_type_meta
- if field_nested_key in d:
+ if field_nested_key in d and isinstance(d[field_nested_key], dict):
+ # Only recurse into a nested config when the value is actually a
+ # mapping. A scalar under this key (e.g. a select field submitted
+ # flat as `auth_method: "oauth"` instead of nested) is not a nested
+ # config — fall through to the flat-structure path below. This mirrors
+ # the guard in `validate_config`; without it `validate_config` accepts
+ # the payload while `to_config` raises an unhandled `TypeError`.
try:
value = to_config(config_type, d[field_nested_key], prefixes)
except TypeError:
diff --git a/posthog/temporal/data_imports/sources/common/test/test_config.py b/posthog/temporal/data_imports/sources/common/test/test_config.py
index 5dd8d58dbac2..1f3753e04116 100644
--- a/posthog/temporal/data_imports/sources/common/test/test_config.py
+++ b/posthog/temporal/data_imports/sources/common/test/test_config.py
@@ -1,5 +1,7 @@
import typing
+import pytest
+
from posthog.temporal.data_imports.sources.common import config
@@ -320,6 +322,58 @@ def test_to_config_union_nested_configs_with_alias():
assert a_cfg.inner.a == "test"
+ "config_dict,expected_selection,expected_integration_id,expected_account_id",
+ [
+ # Flat select payload: the option value as a scalar with the option's fields as
+ # siblings. The scalar isn't mapped to `selection`, so it keeps its default and
+ # the siblings are parsed flat.
+ ({"auth_method": "oauth", "integration_id": 123, "account_id": "acct_x"}, "api_key", 123, "acct_x"),
+ # Nested form keeps working unchanged.
+ (
+ {"auth_method": {"selection": "oauth", "integration_id": 456}, "account_id": "acct_y"},
+ "oauth",
+ 456,
+ "acct_y",
+ ),
+ ],
+)
+def test_to_config_scalar_under_nested_config_key(
+ config_dict, expected_selection, expected_integration_id, expected_account_id
+):
+ """A scalar under a nested-config key must not crash `to_config`.
+
+ A flat select payload (e.g. `auth_method: "oauth"` with the option's fields as
+ siblings, instead of the nested `auth_method: {"selection": "oauth", ...}`) puts a
+ scalar where a nested config dict is expected. `validate_config` already treats this
+ as a flat structure (it guards with `isinstance(..., dict)`), so `to_config` must do
+ the same and fall through to flat parsing instead of recursing into the scalar and
+ raising an unhandled `TypeError`.
+ """
+
+ @config.config
+ class AuthMethod:
+ selection: str = "api_key"
+ integration_id: int | None = config.value(converter=config.str_to_optional_int, default_factory=lambda: None)
+
+ @config.config
+ class SourceConfig(config.Config):
+ auth_method: AuthMethod
+ account_id: str | None = None
+
+ # Validation accepts both shapes, so construction must not crash — the two functions
+ # have to agree.
+ is_valid, errors = SourceConfig.validate_dict(config_dict)
+ assert is_valid is True
+ assert errors == []
+
+ cfg = SourceConfig.from_dict(config_dict)
+ assert isinstance(cfg.auth_method, AuthMethod)
+ assert cfg.auth_method.selection == expected_selection
+ assert cfg.auth_method.integration_id == expected_integration_id
+ assert cfg.account_id == expected_account_id
+
+
def test_validate_dict():
@config.config
class TestConfig(config.Config):
diff --git a/posthog/temporal/data_imports/sources/tests/test_generated_configs.py b/posthog/temporal/data_imports/sources/tests/test_generated_configs.py
index 4c25fc7e2988..d8389f587571 100644
--- a/posthog/temporal/data_imports/sources/tests/test_generated_configs.py
+++ b/posthog/temporal/data_imports/sources/tests/test_generated_configs.py
@@ -341,13 +341,31 @@ def test_snowflake_config_without_schema(schema_override, expected_schema):
assert config.schema == expected_schema
-def test_stripe_config():
- config = StripeSourceConfig.from_dict(
- {"auth_method": {"selection": "api_key", "stripe_secret_key": "api_key"}, "stripe_account_id": "acct_id"}
- )
+ "config_dict,expected_auth_method_attrs",
+ [
+ # Nested form: the `auth_method` select submitted as a mapping.
+ (
+ {"auth_method": {"selection": "api_key", "stripe_secret_key": "api_key"}, "stripe_account_id": "acct_id"},
+ {"selection": "api_key", "stripe_secret_key": "api_key"},
+ ),
+ # Flat form: the option value as a scalar with the option's fields as siblings. This
+ # passes `validate_dict`, so `from_dict` must not raise an unhandled TypeError on the scalar.
+ (
+ {"auth_method": "oauth", "stripe_integration_id": 176624, "stripe_account_id": "acct_id"},
+ {"stripe_integration_id": 176624},
+ ),
+ ],
+)
+def test_stripe_config(config_dict, expected_auth_method_attrs):
+ is_valid, errors = StripeSourceConfig.validate_dict(config_dict)
+ assert is_valid is True
+ assert errors == []
+
+ config = StripeSourceConfig.from_dict(config_dict)
assert config.stripe_account_id == "acct_id"
- assert config.auth_method.selection == "api_key"
- assert config.auth_method.stripe_secret_key == "api_key"
+ for attr, expected in expected_auth_method_attrs.items():
+ assert getattr(config.auth_method, attr) == expected
def test_shopify_config():