Skip to content

Commit

Permalink
[#3625] Fix handling of null values when parsing Stuf responses
Browse files Browse the repository at this point in the history
Backport-of: #3634
  • Loading branch information
Viicos authored and sergei-maertens committed Nov 28, 2023
1 parent 102d2d9 commit 443d84e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@
<ns:inp.geboorteLand>6030</ns:inp.geboorteLand>
</ns:gerelateerde>
</ns:inp.heeftAlsKinderen>
<ns:inp.heeftAlsKinderen StUF:entiteittype="NPSNPSKND">
<ns:gerelateerde StUF:entiteittype="NPS">
<!--Optional:-->
<ns:inp.bsn>123456789</ns:inp.bsn>
<!--Optional:-->
<ns:geslachtsnaam>Doe</ns:geslachtsnaam>
<!--Optional:-->
<ns:voorvoegselGeslachtsnaam>van</ns:voorvoegselGeslachtsnaam>
<!--Optional:-->
<ns:voorletters>K</ns:voorletters>
<!--Optional:-->
<ns:voornamen>Billy</ns:voornamen>
<!--Optional:-->
<ns:geboortedatum xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<!--Optional:-->
<ns:inp.geboorteplaats>Amsterdam</ns:inp.geboorteplaats>
<!--Optional:-->
<ns:inp.geboorteLand>6030</ns:inp.geboorteLand>
</ns:gerelateerde>
</ns:inp.heeftAlsKinderen>
</ns:object>
</ns:antwoord>
</ns:npsLa01>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ def test_get_children_stuf_bg(self, mock_stufbg_config_get_solo):

kids_choices = get_np_children_stuf_bg(bsn="111222333")

self.assertEqual(2, len(kids_choices))
self.assertEqual(3, len(kids_choices))
self.assertEqual(("456789123", "Bolly van Doe"), kids_choices[0])
self.assertEqual(("789123456", "Billy van Doe"), kids_choices[1])
self.assertEqual(("123456789", "Billy van Doe"), kids_choices[2])

@patch(
"openforms.formio.components.custom.FamilyMembersTypeConfig.get_solo",
Expand Down
32 changes: 21 additions & 11 deletions src/stuf/stuf_bg/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import List, Mapping, TypeVar
from collections.abc import Mapping
from typing import TypeVar

import xmltodict
from glom import glom
Expand Down Expand Up @@ -39,7 +40,7 @@ def get_values_for_attributes(self, bsn: str, attributes) -> bytes:
)
return response.content

def get_values(self, bsn: str, attributes: List[str]) -> dict:
def get_values(self, bsn: str, attributes: list[str]) -> dict:
response_data = self.get_values_for_attributes(bsn, attributes)

dict_response = _remove_nils(
Expand Down Expand Up @@ -73,23 +74,32 @@ def get_values(self, bsn: str, attributes: List[str]) -> dict:
raise ValueError("Problem processing StUF-BG response")


M = TypeVar("M", bound=Mapping)
# `Sequence` isn't used here at it would match str (and possibly others)
C = TypeVar("C", bound=Mapping | list)


def _remove_nils(d: M) -> M:
def _remove_nils(container: C) -> C:
"""Return a copy of d with nils removed"""
mapping = type(d) # use the same dict type
Container = type(container) # use the same container type

def is_nil(value):
return isinstance(value, mapping) and (
return isinstance(value, Mapping) and (
value.get("@http://www.w3.org/2001/XMLSchema-instance:nil") == "true"
or value.get("@noValue") == "geenWaarde"
)

return mapping(
**{
k: (_remove_nils(v) if isinstance(v, mapping) else v)
for k, v in d.items()
return (
Container(
**{
k: (_remove_nils(v) if isinstance(v, (Mapping, list)) else v)
for k, v in container.items()
if not is_nil(v)
}
)
if issubclass(Container, Mapping)
else [
_remove_nils(v) if isinstance(v, (Mapping, list)) else v
for v in container
if not is_nil(v)
}
]
)

0 comments on commit 443d84e

Please sign in to comment.