Skip to content

Commit ddc7ebf

Browse files
committed
🐛 [#5089] Preventing case changes in 'service fetch config' api calls
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
1 parent 2a34a56 commit ddc7ebf

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from djangorestframework_camel_case.render import CamelCaseJSONRenderer
2+
3+
4+
class ServiceFetchConfigurationRenderer(CamelCaseJSONRenderer):
5+
# The field body and query_params needs to be ignored, to prevent accidental snake_case to camelCase changes.
6+
# See github issue https://github.com/open-formulieren/open-forms/issues/5089
7+
json_underscoreize = {
8+
"ignore_fields": (
9+
"body",
10+
"query_params",
11+
)
12+
}

src/openforms/variables/api/viewsets.py

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from openforms.forms.api.permissions import FormAPIPermissions
99

1010
from ..models import ServiceFetchConfiguration
11+
from .renderers import ServiceFetchConfigurationRenderer
1112
from .serializers import ServiceFetchConfigurationSerializer
1213

1314

@@ -26,6 +27,7 @@ class ServiceFetchConfigurationViewSet(viewsets.ReadOnlyModelViewSet):
2627
authentication_classes = (authentication.SessionAuthentication,)
2728
permission_classes = (FormAPIPermissions,)
2829
serializer_class = ServiceFetchConfigurationSerializer
30+
renderer_classes = (ServiceFetchConfigurationRenderer,)
2931

3032
queryset = ServiceFetchConfiguration.objects.order_by("service", "-pk")
3133
pagination_class = pagination.PageNumberPagination

src/openforms/variables/tests/test_viewsets.py

+42
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from rest_framework import status
24
from rest_framework.reverse import reverse
35
from rest_framework.test import APITestCase
@@ -92,3 +94,43 @@ def test_service_fetch_configuration_have_the_right_properties(self):
9294
}
9395

9496
self.assertEqual(response.data["results"][0], expected)
97+
98+
def test_service_fetch_configuration_list_returns_data_using_correct_text_case(
99+
self,
100+
):
101+
config = ServiceFetchConfigurationFactory.create(
102+
name="Service fetch configuration 1",
103+
method=ServiceFetchMethods.post,
104+
query_params={
105+
"snake_case": ["snake_case_data"],
106+
"camelCase": ["camelCaseData"],
107+
},
108+
body={"snake_case": "snake_case_data", "camelCase": "camelCaseData"},
109+
)
110+
endpoint = reverse("api:servicefetchconfiguration-list")
111+
self.client.force_authenticate(user=self.admin_user)
112+
113+
response = self.client.get(endpoint, format="json")
114+
115+
self.assertEqual(response.status_code, status.HTTP_200_OK)
116+
self.assertEqual(response.data["count"], 1)
117+
118+
content = json.loads(response.content)
119+
expected = {
120+
"id": config.pk,
121+
"name": "Service fetch configuration 1",
122+
"service": f"http://testserver{reverse('api:service-detail', kwargs={'pk': config.service.pk})}",
123+
"path": "",
124+
"method": ServiceFetchMethods.post.value,
125+
"headers": {},
126+
"queryParams": {
127+
"snake_case": ["snake_case_data"],
128+
"camelCase": ["camelCaseData"],
129+
},
130+
"body": {"snake_case": "snake_case_data", "camelCase": "camelCaseData"},
131+
"dataMappingType": "",
132+
"mappingExpression": None,
133+
"cacheTimeout": None,
134+
}
135+
136+
self.assertEqual(content["results"][0], expected)

0 commit comments

Comments
 (0)