diff --git a/src/openforms/contrib/objects_api/helpers.py b/src/openforms/contrib/objects_api/helpers.py index e7e2bd7f47..7f26972276 100644 --- a/src/openforms/contrib/objects_api/helpers.py +++ b/src/openforms/contrib/objects_api/helpers.py @@ -7,10 +7,11 @@ def prepare_data_for_registration( record_data: dict[str, Any], objecttype: str, objecttype_version: int, + geometry_data: dict[str, Any] | None = None, ) -> dict[str, Any]: """Prepare the submission data for sending it to the Objects API.""" - return { + data = { "type": objecttype, "record": { "typeVersion": objecttype_version, @@ -18,3 +19,7 @@ def prepare_data_for_registration( "startAt": get_today(), }, } + if geometry_data is not None: + data["record"]["geometry"] = geometry_data + + return data diff --git a/src/openforms/registrations/contrib/objects_api/api/views.py b/src/openforms/registrations/contrib/objects_api/api/views.py index 08992a5573..4473b67b0a 100644 --- a/src/openforms/registrations/contrib/objects_api/api/views.py +++ b/src/openforms/registrations/contrib/objects_api/api/views.py @@ -87,10 +87,22 @@ def post(self, request: Request, *args: Any, **kwargs: Any): with get_objecttypes_client() as client: - json_schema = client.get_objecttype_version( + allow_geometry = client.get_objecttype(objecttype_uuid).get( + "allowGeometry", True + ) + + _json_schema = client.get_objecttype_version( objecttype_uuid, input_serializer.validated_data["objecttype_version"] )["jsonSchema"] + json_schema = { + "type": "object", + "properties": {"data": {"type": "object", "properties": _json_schema}}, + } + + if allow_geometry: + json_schema["properties"]["geometry"] = {"type": "object"} + return_data = [ { "target_path": json_path.segments, diff --git a/src/openforms/registrations/contrib/objects_api/config.py b/src/openforms/registrations/contrib/objects_api/config.py index 7a92270dba..d64d33e583 100644 --- a/src/openforms/registrations/contrib/objects_api/config.py +++ b/src/openforms/registrations/contrib/objects_api/config.py @@ -150,23 +150,13 @@ class ObjectsAPIOptionsSerializer(JsonSchemaSerializerMixin, serializers.Seriali required=False, ) - # As `record.geometry` is outside `record.data`, we special case this attribute: - geometry_variable_key = FormioVariableKeyField( - label=_("geometry variable"), - help_text=_( - "The 'dotted' path to a form variable key that should be mapped to the `record.geometry` attribute." - ), - required=False, - allow_blank=True, - ) - def validate(self, attrs: dict[str, Any]) -> dict[str, Any]: v1_only_fields = { "productaanvraag_type", "content_json", "payment_status_update_json", } - v2_only_fields = {"variables_mapping", "geometry_variable_key"} + v2_only_fields = {"variables_mapping"} version = get_from_serializer_data_or_instance("version", attrs, self) diff --git a/src/openforms/registrations/contrib/objects_api/submission_registration.py b/src/openforms/registrations/contrib/objects_api/submission_registration.py index 9e86657c5f..c5aa2e2847 100644 --- a/src/openforms/registrations/contrib/objects_api/submission_registration.py +++ b/src/openforms/registrations/contrib/objects_api/submission_registration.py @@ -27,7 +27,6 @@ SubmissionFileAttachment, SubmissionReport, ) -from openforms.typing import JSONObject from openforms.variables.constants import FormVariableSources from openforms.variables.service import get_static_variables from openforms.variables.utils import get_variables_for_context @@ -363,10 +362,10 @@ def get_update_payment_status_data( class ObjectsAPIV2Handler(ObjectsAPIRegistrationHandler[RegistrationOptionsV2]): @staticmethod - def _get_record_data( + def _get_payload_data( variables_values: FormioData, variables_mapping: list[ObjecttypeVariableMapping] - ) -> JSONObject: - record_data: JSONObject = {} + ) -> dict[str, Any]: + payload_data: dict[str, Any] = {} for mapping in variables_mapping: variable_key = mapping["variable_key"] @@ -383,9 +382,9 @@ def _get_record_data( if isinstance(value, (datetime, date)): value = value.isoformat() - glom.assign(record_data, glom.Path(*target_path), value, missing=dict) + glom.assign(payload_data, glom.Path(*target_path), value, missing=dict) - return record_data + return payload_data @staticmethod def _process_value(value: Any, component: Component) -> Any: @@ -455,17 +454,15 @@ def get_object_data( variables_values = FormioData({**dynamic_values, **static_values}) variables_mapping = options["variables_mapping"] - record_data = self._get_record_data(variables_values, variables_mapping) + payload_data = self._get_payload_data(variables_values, variables_mapping) object_data = prepare_data_for_registration( - record_data=record_data, + record_data=payload_data.get("data", {}), + geometry_data=payload_data.get("geometry"), objecttype=options["objecttype"], objecttype_version=options["objecttype_version"], ) - if geometry_variable_key := options.get("geometry_variable_key"): - object_data["record"]["geometry"] = variables_values[geometry_variable_key] - return object_data @override @@ -485,10 +482,11 @@ def get_update_payment_status_data( variables_values = FormioData(values) variables_mapping = options["variables_mapping"] - record_data = self._get_record_data(variables_values, variables_mapping) + payload_data = self._get_payload_data(variables_values, variables_mapping) object_data = prepare_data_for_registration( - record_data=record_data, + record_data=payload_data.get("data", {}), + geometry_data=payload_data.get("geometry"), objecttype=options["objecttype"], objecttype_version=options["objecttype_version"], ) diff --git a/src/openforms/registrations/contrib/objects_api/typing.py b/src/openforms/registrations/contrib/objects_api/typing.py index fe3ae7c861..536c59679e 100644 --- a/src/openforms/registrations/contrib/objects_api/typing.py +++ b/src/openforms/registrations/contrib/objects_api/typing.py @@ -30,7 +30,6 @@ class ObjecttypeVariableMapping(TypedDict): class RegistrationOptionsV2(_BaseRegistrationOptions, total=False): version: Required[Literal[2]] variables_mapping: Required[list[ObjecttypeVariableMapping]] - geometry_variable_key: str RegistrationOptions: TypeAlias = RegistrationOptionsV1 | RegistrationOptionsV2