From 776fe93dfe5f791f4f4b3c3963f5acfbea0bee26 Mon Sep 17 00:00:00 2001
From: Ben White <ben@posthog.com>
Date: Thu, 19 Dec 2024 16:03:18 +0100
Subject: [PATCH] Fix - don't include choice in the DB side of things

---
 posthog/api/hog_function.py                   |  3 +++
 posthog/api/test/test_hog_function.py         | 19 +++++++++++++++++++
 .../migrations/0534_alter_hogfunction_type.py | 17 +++++++++++++++++
 posthog/migrations/max_migration.txt          |  2 +-
 posthog/models/hog_functions/hog_function.py  |  2 +-
 5 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 posthog/migrations/0534_alter_hogfunction_type.py

diff --git a/posthog/api/hog_function.py b/posthog/api/hog_function.py
index 3f50d710acc96..0625775ce526d 100644
--- a/posthog/api/hog_function.py
+++ b/posthog/api/hog_function.py
@@ -32,6 +32,7 @@
     TYPES_WITH_COMPILED_FILTERS,
     TYPES_WITH_TRANSPILED_FILTERS,
     TYPES_WITH_JAVASCRIPT_SOURCE,
+    HogFunctionType,
 )
 from posthog.models.plugin import TranspilerError
 from posthog.plugins.plugin_server_api import create_hog_invocation_test
@@ -88,6 +89,8 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer):
     template = HogFunctionTemplateSerializer(read_only=True)
     masking = HogFunctionMaskingSerializer(required=False, allow_null=True)
 
+    type = serializers.ChoiceField(choices=HogFunctionType.choices, required=False, allow_null=True)
+
     class Meta:
         model = HogFunction
         fields = [
diff --git a/posthog/api/test/test_hog_function.py b/posthog/api/test/test_hog_function.py
index 414f5f19aa51f..6e1d49b08098a 100644
--- a/posthog/api/test/test_hog_function.py
+++ b/posthog/api/test/test_hog_function.py
@@ -409,6 +409,25 @@ def test_inputs_required(self, *args):
             "attr": "inputs__url",
         }
 
+    def test_validation_error_on_invalid_type(self, *args):
+        payload = {
+            "name": "Fetch URL",
+            "hog": "fetch(inputs.url);",
+            "inputs_schema": [
+                {"key": "url", "type": "string", "label": "Webhook URL", "required": True},
+            ],
+            "type": "invalid_type",
+        }
+        # Check required
+        res = self.client.post(f"/api/projects/{self.team.id}/hog_functions/", data={**payload})
+        assert res.status_code == status.HTTP_400_BAD_REQUEST, res.json()
+        assert res.json() == {
+            "type": "validation_error",
+            "code": "invalid_choice",
+            "detail": '"invalid_type" is not a valid choice.',
+            "attr": "type",
+        }
+
     def test_inputs_mismatch_type(self, *args):
         payload = {
             "name": "Fetch URL",
diff --git a/posthog/migrations/0534_alter_hogfunction_type.py b/posthog/migrations/0534_alter_hogfunction_type.py
new file mode 100644
index 0000000000000..8eedf94b2f799
--- /dev/null
+++ b/posthog/migrations/0534_alter_hogfunction_type.py
@@ -0,0 +1,17 @@
+# Generated by Django 4.2.15 on 2024-12-19 14:58
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("posthog", "0533_externaldatajob_pipeline_version"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="hogfunction",
+            name="type",
+            field=models.CharField(blank=True, max_length=24, null=True),
+        ),
+    ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 44547aebb012e..ad31dc830a082 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-0533_externaldatajob_pipeline_version
+0534_alter_hogfunction_type
diff --git a/posthog/models/hog_functions/hog_function.py b/posthog/models/hog_functions/hog_function.py
index 3ddd0212f21d5..8328973bc0a2e 100644
--- a/posthog/models/hog_functions/hog_function.py
+++ b/posthog/models/hog_functions/hog_function.py
@@ -66,7 +66,7 @@ class Meta:
     deleted = models.BooleanField(default=False)
     updated_at = models.DateTimeField(auto_now=True)
     enabled = models.BooleanField(default=False)
-    type = models.CharField(max_length=24, choices=HogFunctionType.choices, null=True, blank=True)
+    type = models.CharField(max_length=24, null=True, blank=True)
 
     icon_url = models.TextField(null=True, blank=True)