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(surveys): allow empty link in question translations (#62077)

Ben Leaf94bac7192aa
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.py
index ea8fc5c3f358..fcd54827e577 100644
--- a/products/surveys/backend/api/survey.py
+++ b/products/surveys/backend/api/survey.py
@@ -1171,13 +1171,14 @@ class SurveySerializerCreateUpdateOnly(serializers.ModelSerializer):
else:
cleaned_translation[field] = translation_data[field]
- # Validate and sanitize link field
- if "link" in translation_data:
- if not isinstance(translation_data["link"], str):
+ # Empty link is treated as absent rather than rejected, mirroring the base question.
+ link = translation_data.get("link")
+ if link:
+ if not isinstance(link, str):
raise serializers.ValidationError(
f"Question {question_num}: Translation '{raw_lang_code}' field 'link' must be a string"
)
- cleaned_translation["link"] = self._validate_and_sanitize_link(translation_data["link"])
+ cleaned_translation["link"] = self._validate_and_sanitize_link(link)
# Validate and sanitize choices array
if "choices" in translation_data:
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index c57602adc895..58f80f2e4cc2 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -3,7 +3,7 @@ import json
import time
import uuid
from datetime import UTC, datetime, timedelta
-from typing import Any, cast
+from typing import Any, Optional, cast
import pytest
from freezegun.api import freeze_time
@@ -141,6 +141,49 @@ class TestSurvey(APIBaseTest):
assert questions[0]["translations"]["fr"]["question"] == "Êtes-vous satisfait?"
assert questions[1]["translations"]["es"]["choices"] == ["Analítica", "Feature Flags"]
+ @parameterized.expand(
+ [
+ ("empty_string", "", False),
+ ("absent", None, False),
+ ("valid_url", "https://posthog.com/docs", True),
+ ]
+ )
+ def test_link_question_translation_allows_empty_link(
+ self, _name: str, translation_link: Optional[str], expect_link_present: bool
+ ) -> None:
+ translation: dict[str, Any] = {"question": "Mira nuestra documentación"}
+ if translation_link is not None:
+ translation["link"] = translation_link
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/surveys/",
+ data={
+ "name": "Link survey with translations",
+ "type": "popover",
+ "questions": [
+ {
+ "type": "link",
+ "question": "Check out our docs",
+ "link": "https://posthog.com",
+ "translations": {
+ "es": translation,
+ },
+ },
+ ],
+ },
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_201_CREATED, response.json()
+ survey = Survey.objects.get(id=response.json()["id"])
+ questions = cast(list[dict[str, Any]], survey.questions)
+
+ es_translation = questions[0]["translations"]["es"]
+ assert es_translation["question"] == "Mira nuestra documentación"
+ if expect_link_present:
+ assert es_translation.get("link") == translation_link
+ else:
+ assert "link" not in es_translation
+
def test_translation_language_codes_are_normalized(self) -> None:
"""BCP-47-ish codes are normalized to lowercase + hyphenated on save."""
response = self.client.post(