Skip to content

Commit

Permalink
[#4396] Added the prefill available attributes functionality
Browse files Browse the repository at this point in the history
This is based on retrieving the specific objecttype version and based on
the JsonSchema we define the available attributes.
  • Loading branch information
vaszig committed Aug 26, 2024
1 parent 9a5479a commit 66ed5a5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/openforms/prefill/contrib/objects_api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ...base import BasePlugin
from ...constants import IdentifierRoles
from ...registry import register
from .utils import retrieve_properties

logger = logging.getLogger(__name__)

Expand All @@ -25,7 +26,8 @@ class ObjectsAPIPrefill(BasePlugin):
def get_available_attributes(
reference: dict[str, Any] | None = None,
) -> Iterable[tuple[str, str]]:
pass
assert reference is not None
return retrieve_properties(reference)

Check warning on line 30 in src/openforms/prefill/contrib/objects_api/plugin.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/plugin.py#L29-L30

Added lines #L29 - L30 were not covered by tests

@classmethod
def get_prefill_values(
Expand All @@ -41,3 +43,6 @@ def get_co_sign_values(
cls, submission: Submission, identifier: str
) -> tuple[dict[str, Any], str]:
pass

def check_config(self):
pass
55 changes: 55 additions & 0 deletions src/openforms/prefill/contrib/objects_api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from typing import Any, Iterable

from referencing.jsonschema import ObjectSchema

from openforms.registrations.contrib.objects_api.client import get_objecttypes_client


def parse_schema_properties(
schema: ObjectSchema, parent_key: str = ""
) -> list[tuple[str, str]]:
properties = []

Check warning on line 11 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L11

Added line #L11 was not covered by tests

if schema["type"] == "object":
for prop, prop_schema in schema.get("properties", {}).items():
full_key = f"{parent_key}.{prop}" if parent_key else prop
prop_type = prop_schema.get("type", "unknown")
properties.append((full_key, prop_type))

Check warning on line 17 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L15-L17

Added lines #L15 - L17 were not covered by tests
if prop_type == "object" or (
prop_type == "array" and "items" in prop_schema
):
properties.extend(parse_schema_properties(prop_schema, full_key))

Check warning on line 21 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L21

Added line #L21 was not covered by tests
elif schema["type"] == "array":
items_schema = schema.get("items", {})

Check warning on line 23 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L23

Added line #L23 was not covered by tests
if isinstance(items_schema, dict):
properties.extend(parse_schema_properties(items_schema, f"{parent_key}[]"))

Check warning on line 25 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L25

Added line #L25 was not covered by tests
elif isinstance(items_schema, list):
for i, item_schema in enumerate(items_schema):
properties.extend(

Check warning on line 28 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L28

Added line #L28 was not covered by tests
parse_schema_properties(item_schema, f"{parent_key}[{i}]")
)
else:
properties.append((parent_key, schema["type"]))

Check warning on line 32 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L32

Added line #L32 was not covered by tests

# also remove the props of type object since it's not needed e.g.-> (name, object)
# it's the parent prop of the object, we need the real properties and their types
return [

Check warning on line 36 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L36

Added line #L36 was not covered by tests
(prop[0].replace(".", " > "), prop[1])
for prop in properties
if prop[1] != "object"
]


def retrieve_properties(
reference: dict[str, Any] | None = None,
) -> Iterable[tuple[str, str]]:
assert reference is not None

Check warning on line 46 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L46

Added line #L46 was not covered by tests

with get_objecttypes_client(reference["objects_api_group"]) as client:
json_schema = client.get_objecttype_version(

Check warning on line 49 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L49

Added line #L49 was not covered by tests
reference["objects_api_objecttype_uuid"],
reference["objects_api_objecttype_version"],
)["jsonSchema"]

properties = parse_schema_properties(json_schema)
return [(prop[0], f"{prop[0]} ({prop[1]})") for prop in properties]

Check warning on line 55 in src/openforms/prefill/contrib/objects_api/utils.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/prefill/contrib/objects_api/utils.py#L54-L55

Added lines #L54 - L55 were not covered by tests

0 comments on commit 66ed5a5

Please sign in to comment.