Skip to content

Commit

Permalink
🐛 [#5089] Preventing case changes in 'service fetch config' api calls
Browse files Browse the repository at this point in the history
Make sure that `body` and `query_params` on service fetch configuration keep their original case type.

In most cases we want to change the text case types of content, because the frontend and backend expect different case notations. For the service fetch `body` and `query_params` the case type schouldn't change, because this results in logic not being performed.

Backport-Of: #5089
  • Loading branch information
robinmolen committed Feb 25, 2025
1 parent 2a34a56 commit ddc7ebf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/openforms/variables/api/renderers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from djangorestframework_camel_case.render import CamelCaseJSONRenderer


class ServiceFetchConfigurationRenderer(CamelCaseJSONRenderer):
# The field body and query_params needs to be ignored, to prevent accidental snake_case to camelCase changes.
# See github issue https://github.com/open-formulieren/open-forms/issues/5089
json_underscoreize = {
"ignore_fields": (
"body",
"query_params",
)
}
2 changes: 2 additions & 0 deletions src/openforms/variables/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from openforms.forms.api.permissions import FormAPIPermissions

from ..models import ServiceFetchConfiguration
from .renderers import ServiceFetchConfigurationRenderer
from .serializers import ServiceFetchConfigurationSerializer


Expand All @@ -26,6 +27,7 @@ class ServiceFetchConfigurationViewSet(viewsets.ReadOnlyModelViewSet):
authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (FormAPIPermissions,)
serializer_class = ServiceFetchConfigurationSerializer
renderer_classes = (ServiceFetchConfigurationRenderer,)

queryset = ServiceFetchConfiguration.objects.order_by("service", "-pk")
pagination_class = pagination.PageNumberPagination
42 changes: 42 additions & 0 deletions src/openforms/variables/tests/test_viewsets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -92,3 +94,43 @@ def test_service_fetch_configuration_have_the_right_properties(self):
}

self.assertEqual(response.data["results"][0], expected)

def test_service_fetch_configuration_list_returns_data_using_correct_text_case(
self,
):
config = ServiceFetchConfigurationFactory.create(
name="Service fetch configuration 1",
method=ServiceFetchMethods.post,
query_params={
"snake_case": ["snake_case_data"],
"camelCase": ["camelCaseData"],
},
body={"snake_case": "snake_case_data", "camelCase": "camelCaseData"},
)
endpoint = reverse("api:servicefetchconfiguration-list")
self.client.force_authenticate(user=self.admin_user)

response = self.client.get(endpoint, format="json")

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 1)

content = json.loads(response.content)
expected = {
"id": config.pk,
"name": "Service fetch configuration 1",
"service": f"http://testserver{reverse('api:service-detail', kwargs={'pk': config.service.pk})}",
"path": "",
"method": ServiceFetchMethods.post.value,
"headers": {},
"queryParams": {
"snake_case": ["snake_case_data"],
"camelCase": ["camelCaseData"],
},
"body": {"snake_case": "snake_case_data", "camelCase": "camelCaseData"},
"dataMappingType": "",
"mappingExpression": None,
"cacheTimeout": None,
}

self.assertEqual(content["results"][0], expected)

0 comments on commit ddc7ebf

Please sign in to comment.