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 2, 2024
1 parent c952db1 commit 175c0cd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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 @@ -94,7 +94,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 @@ -185,7 +185,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 @@ -235,7 +235,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
49 changes: 27 additions & 22 deletions src/openklant/components/klantinteracties/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1199,23 +1199,23 @@ paths:
items:
type: string
enum:
- ging_over_onderwerpobjecten
- had_betrokken_actoren
- had_betrokkenen
- had_betrokkenen.digitale_adressen
- had_betrokkenen.was_partij
- leidde_tot_interne_taken
- omvatte_bijlagen
- gingOverOnderwerpobjecten
- hadBetrokkenActoren
- hadBetrokkenen
- hadBetrokkenen.DigitaleAdressen
- hadBetrokkenen.WasPartij
- leiddeTotInterneTaken
- omvatteBijlagen
description: |-
Sluit de gespecifieerde gerelateerde resources in in het antwoord.
* `had_betrokkenen` - had_betrokkenen
* `had_betrokken_actoren` - had_betrokken_actoren
* `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
* `hadBetrokkenActoren` - had_betrokken_actoren
* `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 @@ -1714,6 +1714,11 @@ paths:
name: bezoekadres_nummeraanduiding_id
schema:
type: string
- in: query
name: categorierelatie__categorie__naam
schema:
type: string
description: Zoek partij object op basis van categorie namen.
- in: query
name: correspondentieadres_adresregel1
schema:
Expand Down Expand Up @@ -1742,18 +1747,18 @@ paths:
type: string
enum:
- betrokkenen
- betrokkenen.had_klantcontact
- betrokkenen.had_klantcontact.had_betrokken_actoren
- categorie_relaties
- digitale_adressen
- betrokkenen.hadKlantcontact
- betrokkenen.hadKlantcontact.HadBetrokkenActoren
- 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
* `betrokkenen.had_klantcontact.had_betrokken_actoren` - betrokkenen.had_klantcontact.had_betrokken_actoren
* `categorieRelaties` - categorie_relaties
* `betrokkenen.hadKlantcontact` - betrokkenen.had_klantcontact
* `betrokkenen.hadKlantcontact.HadBetrokkenActoren` - betrokkenen.had_klantcontact.had_betrokken_actoren
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 175c0cd

Please sign in to comment.