Skip to content

Commit

Permalink
🎨 [#165] changed expand snake_case query params to camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Feb 8, 2024
1 parent d05ccab commit 4626b9d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setUp(self):

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

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

Expand Down Expand Up @@ -98,7 +98,7 @@ def test_list_single_expansion(self):
def test_list_multiple_level_expansion(self):
list_url = reverse("klantinteracties:klantcontact-list")
response = self.client.get(
list_url, {"expand": "had_betrokkenen,had_betrokkenen.was_partij"}
list_url, {"expand": "hadBetrokkenen,hadBetrokkenen.wasPartij"}
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_detail_single_expansion(self):
"klantinteracties:klantcontact-detail",
kwargs={"uuid": str(self.klantcontact.uuid)},
)
response = self.client.get(detail_url, {"expand": "had_betrokkenen"})
response = self.client.get(detail_url, {"expand": "hadBetrokkenen"})

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

Expand Down Expand Up @@ -238,7 +238,7 @@ def test_detail_multiple_level_expansion(self):
kwargs={"uuid": str(self.klantcontact.uuid)},
)
response = self.client.get(
detail_url, {"expand": "had_betrokkenen,had_betrokkenen.was_partij"}
detail_url, {"expand": "hadBetrokkenen,hadBetrokkenen.wasPartij"}
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down
36 changes: 18 additions & 18 deletions src/openklant/components/klantinteracties/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1364,21 +1364,21 @@ paths:
items:
type: string
enum:
- ging_over_onderwerpobjecten
- had_betrokkenen
- had_betrokkenen.digitale_adressen
- had_betrokkenen.was_partij
- leidde_tot_interne_taken
- omvatte_bijlagen
- gingOverOnderwerpobjecten
- hadBetrokkenen
- hadBetrokkenen.digitaleAdressen
- hadBetrokkenen.wasPartij
- leiddeTotInterneTaken
- omvatteBijlagen
description: |-
Sluit de gespecifieerde gerelateerde resources in in het antwoord.
* `had_betrokkenen` - had_betrokkenen
* `leidde_tot_interne_taken` - leidde_tot_interne_taken
* `ging_over_onderwerpobjecten` - ging_over_onderwerpobjecten
* `omvatte_bijlagen` - omvatte_bijlagen
* `had_betrokkenen.was_partij` - had_betrokkenen.was_partij
* `had_betrokkenen.digitale_adressen` - had_betrokkenen.digitale_adressen
* `hadBetrokkenen` - had_betrokkenen
* `leiddeTotInterneTaken` - leidde_tot_interne_taken
* `gingOverOnderwerpobjecten` - ging_over_onderwerpobjecten
* `omvatteBijlagen` - omvatte_bijlagen
* `hadBetrokkenen.wasPartij` - had_betrokkenen.was_partij
* `hadBetrokkenen.digitaleAdressen` - had_betrokkenen.digitale_adressen
explode: false
style: form
- in: query
Expand Down Expand Up @@ -1910,16 +1910,16 @@ paths:
type: string
enum:
- betrokkenen
- betrokkenen.had_klantcontact
- categorie_relaties
- digitale_adressen
- betrokkenen.hadKlantcontact
- categorieRelaties
- digitaleAdressen
description: |-
Sluit de gespecifieerde gerelateerde resources in in het antwoord.
* `digitale_adressen` - digitale_adressen
* `digitaleAdressen` - digitale_adressen
* `betrokkenen` - betrokkenen
* `categorie_relaties` - categorie_relaties
* `betrokkenen.had_klantcontact` - betrokkenen.had_klantcontact
* `categorieRelaties` - categorie_relaties
* `betrokkenen.hadKlantcontact` - betrokkenen.had_klantcontact
explode: false
style: form
- in: query
Expand Down
28 changes: 26 additions & 2 deletions src/openklant/components/utils/expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
from rest_framework_inclusions.core import InclusionLoader
from rest_framework_inclusions.renderer import (
InclusionJSONRenderer,
get_allowed_paths,
should_skip_inclusions,
)

from openklant.utils.converters import (
camel_to_snake_converter,
snake_to_camel_converter,
)

from .camilize import CamelCaseJSONRenderer

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


# Added camelCase to snake_case converter
def get_allowed_paths(request, view=None):
if getattr(view, "get_requested_inclusions", None):
include = view.get_requested_inclusions(request)
else:
include = request.GET.get("include") if request else None

if include is None:
# nothing is allowed
return set()
if include == "*":
# everything is allowed
return None

include = camel_to_snake_converter(include)
return [tuple(entry.split(".")) for entry in include.split(",")]


def get_expand_options_for_serializer(
serializer_class: Type[Serializer],
) -> List[tuple]:
choices = [
(
opt,
".".join(
snake_to_camel_converter(field_name) for field_name in opt.split(".")
),
opt,
)
for opt in serializer_class.inclusion_serializers
Expand Down
11 changes: 11 additions & 0 deletions src/openklant/utils/converters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import re


def camel_to_snake_converter(value):
pattern = re.compile(r"(?<!^)(?=[A-Z])")
return pattern.sub("_", value).lower()


def snake_to_camel_converter(value):
init, *temp = value.split("_")
return "".join([init.lower(), *map(str.title, temp)])
15 changes: 15 additions & 0 deletions src/openklant/utils/tests/test_converters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.test import TestCase

from ..converters import camel_to_snake_converter, snake_to_camel_converter


class ConverterTests(TestCase):
def test_camel_to_snake_converter(self):
self.assertEqual(
camel_to_snake_converter("snakeCaseTestString"), "snake_case_test_string"
)

def test_snake_to_camel_converter(self):
self.assertEqual(
snake_to_camel_converter("camel_case_test_string"), "camelCaseTestString"
)

0 comments on commit 4626b9d

Please sign in to comment.