: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(surveys): drop a stale linked flag variant on update (#117182)
posthog[bot]f18e6bfbce2d
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.pyindex 397d48ae4899..ab96e3542d52 100644--- a/products/surveys/backend/api/survey.py+++ b/products/surveys/backend/api/survey.py@@ -1695,7 +1695,13 @@ class SurveySerializerCreateUpdateOnly(serializers.ModelSerializer):f"Feature flag variant '{linked_flag_variant}' specified but the linked feature flag has no variants")elif linked_flag_variant and not linked_flag_id:- raise serializers.ValidationError("linkedFlagVariant can only be used when a linked_flag_id is specified")+ if self.instance is None:+ raise serializers.ValidationError(+ "linkedFlagVariant can only be used when a linked_flag_id is specified"+ )+ if "linked_flag_id" in data or not self.instance.linked_flag_id:+ # Deleting the flag sets linked_flag to NULL but keeps the variant, so drop it instead of blocking every save.+ data["conditions"] = {key: value for key, value in conditions.items() if key != "linkedFlagVariant"}if (self.context["request"].method == "POST"diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.pyindex 015169c63a7f..4cf06a68658d 100644--- a/products/surveys/backend/api/test/test_survey.py+++ b/products/surveys/backend/api/test/test_survey.py@@ -6441,6 +6441,48 @@ class TestSurveyStats(ClickhouseTestMixin, APIBaseTest):self.assertIn("Feature flag variant 'invalid_option' does not exist", error_data["detail"])self.assertIn("Available variants: option_1, option_2", error_data["detail"])+ @parameterized.expand(+ [+ ("stale_variant_without_flag", False, {}, None),+ ("explicit_unlink", True, {"linked_flag_id": None}, None),+ ("partial_update_keeps_linked_flag", True, {}, "alpha"),+ ]+ )+ def test_update_survey_with_linked_flag_variant_and_no_flag_in_payload(+ self, _name: str, instance_has_flag: bool, extra_payload: dict, expected_variant: str | None+ ) -> None:+ flag = FeatureFlag.objects.create(+ team=self.team,+ key="stale-variant-flag",+ created_by=self.user,+ filters={+ "groups": [{"properties": [], "rollout_percentage": 100}],+ "multivariate": {+ "variants": [{"key": "alpha", "rollout_percentage": 50}, {"key": "beta", "rollout_percentage": 50}]+ },+ },+ )+ survey = Survey.objects.create(+ team=self.team,+ created_by=self.user,+ name="Stale Variant Survey",+ type="popover",+ questions=[{"type": "open", "question": "Initial question"}],+ linked_flag=flag if instance_has_flag else None,+ conditions={"linkedFlagVariant": "alpha", "url": "https://example.com"},+ )++ response = self.client.patch(+ f"/api/projects/{self.team.id}/surveys/{survey.id}/",+ data={"conditions": {"linkedFlagVariant": "alpha", "url": "https://example.com"}, **extra_payload},+ format="json",+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())+ conditions = response.json()["conditions"]+ self.assertEqual(conditions.get("linkedFlagVariant"), expected_variant)+ self.assertEqual(conditions["url"], "https://example.com")+class TestSurveyQuestionLabels(APIBaseTest):"""`surveys/question_labels/` is the slim taxonomy endpoint the frontend uses to