Skip to content

Commit 4626b9d

Browse files
committed
🎨 [#165] changed expand snake_case query params to camelCase
1 parent d05ccab commit 4626b9d

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed

src/openklant/components/klantinteracties/api/tests/test_expand.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def setUp(self):
4747

4848
def test_list_single_expansion(self):
4949
list_url = reverse("klantinteracties:klantcontact-list")
50-
response = self.client.get(list_url, {"expand": "had_betrokkenen"})
50+
response = self.client.get(list_url, {"expand": "hadBetrokkenen"})
5151

5252
self.assertEqual(response.status_code, status.HTTP_200_OK)
5353

@@ -98,7 +98,7 @@ def test_list_single_expansion(self):
9898
def test_list_multiple_level_expansion(self):
9999
list_url = reverse("klantinteracties:klantcontact-list")
100100
response = self.client.get(
101-
list_url, {"expand": "had_betrokkenen,had_betrokkenen.was_partij"}
101+
list_url, {"expand": "hadBetrokkenen,hadBetrokkenen.wasPartij"}
102102
)
103103

104104
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -188,7 +188,7 @@ def test_detail_single_expansion(self):
188188
"klantinteracties:klantcontact-detail",
189189
kwargs={"uuid": str(self.klantcontact.uuid)},
190190
)
191-
response = self.client.get(detail_url, {"expand": "had_betrokkenen"})
191+
response = self.client.get(detail_url, {"expand": "hadBetrokkenen"})
192192

193193
self.assertEqual(response.status_code, status.HTTP_200_OK)
194194

@@ -238,7 +238,7 @@ def test_detail_multiple_level_expansion(self):
238238
kwargs={"uuid": str(self.klantcontact.uuid)},
239239
)
240240
response = self.client.get(
241-
detail_url, {"expand": "had_betrokkenen,had_betrokkenen.was_partij"}
241+
detail_url, {"expand": "hadBetrokkenen,hadBetrokkenen.wasPartij"}
242242
)
243243

244244
self.assertEqual(response.status_code, status.HTTP_200_OK)

src/openklant/components/klantinteracties/openapi.yaml

+18-18
Original file line numberDiff line numberDiff line change
@@ -1364,21 +1364,21 @@ paths:
13641364
items:
13651365
type: string
13661366
enum:
1367-
- ging_over_onderwerpobjecten
1368-
- had_betrokkenen
1369-
- had_betrokkenen.digitale_adressen
1370-
- had_betrokkenen.was_partij
1371-
- leidde_tot_interne_taken
1372-
- omvatte_bijlagen
1367+
- gingOverOnderwerpobjecten
1368+
- hadBetrokkenen
1369+
- hadBetrokkenen.digitaleAdressen
1370+
- hadBetrokkenen.wasPartij
1371+
- leiddeTotInterneTaken
1372+
- omvatteBijlagen
13731373
description: |-
13741374
Sluit de gespecifieerde gerelateerde resources in in het antwoord.
13751375
1376-
* `had_betrokkenen` - had_betrokkenen
1377-
* `leidde_tot_interne_taken` - leidde_tot_interne_taken
1378-
* `ging_over_onderwerpobjecten` - ging_over_onderwerpobjecten
1379-
* `omvatte_bijlagen` - omvatte_bijlagen
1380-
* `had_betrokkenen.was_partij` - had_betrokkenen.was_partij
1381-
* `had_betrokkenen.digitale_adressen` - had_betrokkenen.digitale_adressen
1376+
* `hadBetrokkenen` - had_betrokkenen
1377+
* `leiddeTotInterneTaken` - leidde_tot_interne_taken
1378+
* `gingOverOnderwerpobjecten` - ging_over_onderwerpobjecten
1379+
* `omvatteBijlagen` - omvatte_bijlagen
1380+
* `hadBetrokkenen.wasPartij` - had_betrokkenen.was_partij
1381+
* `hadBetrokkenen.digitaleAdressen` - had_betrokkenen.digitale_adressen
13821382
explode: false
13831383
style: form
13841384
- in: query
@@ -1910,16 +1910,16 @@ paths:
19101910
type: string
19111911
enum:
19121912
- betrokkenen
1913-
- betrokkenen.had_klantcontact
1914-
- categorie_relaties
1915-
- digitale_adressen
1913+
- betrokkenen.hadKlantcontact
1914+
- categorieRelaties
1915+
- digitaleAdressen
19161916
description: |-
19171917
Sluit de gespecifieerde gerelateerde resources in in het antwoord.
19181918
1919-
* `digitale_adressen` - digitale_adressen
1919+
* `digitaleAdressen` - digitale_adressen
19201920
* `betrokkenen` - betrokkenen
1921-
* `categorie_relaties` - categorie_relaties
1922-
* `betrokkenen.had_klantcontact` - betrokkenen.had_klantcontact
1921+
* `categorieRelaties` - categorie_relaties
1922+
* `betrokkenen.hadKlantcontact` - betrokkenen.had_klantcontact
19231923
explode: false
19241924
style: form
19251925
- in: query

src/openklant/components/utils/expansion.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
from rest_framework_inclusions.core import InclusionLoader
1717
from rest_framework_inclusions.renderer import (
1818
InclusionJSONRenderer,
19-
get_allowed_paths,
2019
should_skip_inclusions,
2120
)
2221

22+
from openklant.utils.converters import (
23+
camel_to_snake_converter,
24+
snake_to_camel_converter,
25+
)
26+
2327
from .camilize import CamelCaseJSONRenderer
2428

2529
logger = logging.getLogger(__name__)
@@ -355,12 +359,32 @@ def _render_inclusions(self, data, renderer_context):
355359
return render_data
356360

357361

362+
# Added camelCase to snake_case converter
363+
def get_allowed_paths(request, view=None):
364+
if getattr(view, "get_requested_inclusions", None):
365+
include = view.get_requested_inclusions(request)
366+
else:
367+
include = request.GET.get("include") if request else None
368+
369+
if include is None:
370+
# nothing is allowed
371+
return set()
372+
if include == "*":
373+
# everything is allowed
374+
return None
375+
376+
include = camel_to_snake_converter(include)
377+
return [tuple(entry.split(".")) for entry in include.split(",")]
378+
379+
358380
def get_expand_options_for_serializer(
359381
serializer_class: Type[Serializer],
360382
) -> List[tuple]:
361383
choices = [
362384
(
363-
opt,
385+
".".join(
386+
snake_to_camel_converter(field_name) for field_name in opt.split(".")
387+
),
364388
opt,
365389
)
366390
for opt in serializer_class.inclusion_serializers

src/openklant/utils/converters.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import re
2+
3+
4+
def camel_to_snake_converter(value):
5+
pattern = re.compile(r"(?<!^)(?=[A-Z])")
6+
return pattern.sub("_", value).lower()
7+
8+
9+
def snake_to_camel_converter(value):
10+
init, *temp = value.split("_")
11+
return "".join([init.lower(), *map(str.title, temp)])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.test import TestCase
2+
3+
from ..converters import camel_to_snake_converter, snake_to_camel_converter
4+
5+
6+
class ConverterTests(TestCase):
7+
def test_camel_to_snake_converter(self):
8+
self.assertEqual(
9+
camel_to_snake_converter("snakeCaseTestString"), "snake_case_test_string"
10+
)
11+
12+
def test_snake_to_camel_converter(self):
13+
self.assertEqual(
14+
snake_to_camel_converter("camel_case_test_string"), "camelCaseTestString"
15+
)

0 commit comments

Comments
 (0)