Skip to content

Commit

Permalink
[#4141] Fix Objects API registration crash with fieldset
Browse files Browse the repository at this point in the history
Backport-of: #4180
  • Loading branch information
Viicos authored and sergei-maertens committed Apr 16, 2024
1 parent 285f96e commit 055a609
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
20 changes: 20 additions & 0 deletions docker/objects-apis/fixtures/objects_api_fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
"_name": "Geo in data"
}
},
{
"model": "core.objecttype",
"pk": 4,
"fields": {
"service": 1,
"uuid": "644ab597-e88c-43c0-8321-f12113510b0e",
"_name": "Fieldset component"
}
},
{
"model": "token.tokenauth",
"pk": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
Expand Down Expand Up @@ -72,6 +81,17 @@
"fields": null
}
},
{
"model": "token.permission",
"pk": 4,
"fields": {
"token_auth": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
"object_type": 4,
"mode": "read_and_write",
"use_fields": false,
"fields": null
}
},
{
"model": "zgw_consumers.service",
"pk": 1,
Expand Down
45 changes: 45 additions & 0 deletions docker/objects-apis/fixtures/objecttypes_api_fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@
"allow_geometry": true
}
},
{
"model": "core.objecttype",
"pk": 5,
"fields": {
"uuid": "644ab597-e88c-43c0-8321-f12113510b0e",
"name": "Fieldset component",
"name_plural": "Fieldset component",
"description": "",
"data_classification": "confidential",
"maintainer_organization": "",
"maintainer_department": "",
"contact_person": "",
"contact_email": "",
"source": "",
"update_frequency": "unknown",
"provider_organization": "",
"documentation_url": "",
"labels": {},
"created_at": "2024-02-08",
"modified_at": "2024-02-08",
"allow_geometry": true
}
},
{
"model": "core.objectversion",
"pk": 1,
Expand Down Expand Up @@ -306,6 +329,28 @@
"status": "published"
}
},
{
"model": "core.objectversion",
"pk": 7,
"fields": {
"object_type": 5,
"version": 1,
"created_at": "2024-02-08",
"modified_at": "2024-02-08",
"published_at": "2024-02-08",
"json_schema": {
"type": "object",
"title": "Fieldset component",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"textfield": {
"type": "string"
}
}
},
"status": "published"
}
},
{
"model": "token.tokenauth",
"pk": "171be5abaf41e7856b423ad513df1ef8f867ff48",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def get_object_data(
) -> dict[str, Any]:

state = submission.load_submission_value_variables_state()
dynamic_values = state.get_data()
dynamic_values = FormioData(state.get_data())

# For every file upload component, we alter the value of the variable to be
# the Document API URL(s).
Expand All @@ -420,8 +420,7 @@ def get_object_data(
for o in objects_api_attachments:
urls_map[o.variable_key].append(o.document_url)

for key in dynamic_values.keys():
variable = state.get_variable(key)
for key, variable in state.variables.items():
submission_value = dynamic_values[key]

# special casing documents - we transform the formio file upload data into
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
interactions:
- request:
body: '{"type": "http://objecttypes-web:8000/api/v2/objecttypes/644ab597-e88c-43c0-8321-f12113510b0e",
"record": {"typeVersion": 1, "data": {"textfield": "some_string"}, "startAt":
"2024-03-19"}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Token 7657474c3d75f56ae0abd0d1bf7994b09964dca9
Connection:
- keep-alive
Content-Crs:
- EPSG:4326
Content-Length:
- '188'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: POST
uri: http://localhost:8002/api/v2/objects
response:
body:
string: '{"url":"http://localhost:8002/api/v2/objects/e28b18d9-9c5e-4be6-9822-935b9784a92a","uuid":"e28b18d9-9c5e-4be6-9822-935b9784a92a","type":"http://objecttypes-web:8000/api/v2/objecttypes/644ab597-e88c-43c0-8321-f12113510b0e","record":{"index":1,"typeVersion":1,"data":{"textfield":"some_string"},"geometry":null,"startAt":"2024-03-19","endAt":null,"registrationAt":"2024-04-16","correctionFor":null,"correctedBy":null}}'
headers:
Allow:
- GET, POST, HEAD, OPTIONS
Content-Crs:
- EPSG:4326
Content-Length:
- '416'
Content-Type:
- application/json
Cross-Origin-Opener-Policy:
- same-origin
Location:
- http://localhost:8002/api/v2/objects/e28b18d9-9c5e-4be6-9822-935b9784a92a
Referrer-Policy:
- same-origin
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
status:
code: 201
message: Created
version: 1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from unittest.mock import patch

from django.test import TestCase
from django.test import TestCase, tag
from django.utils import timezone

from freezegun import freeze_time
Expand Down Expand Up @@ -294,3 +294,47 @@ def test_submission_with_map_component_inside_data(self):
"coordinates": [52.36673378967122, 4.893164274470299],
},
)

@tag("gh-4141")
def test_layout_components(self):
submission = SubmissionFactory.from_components(
[
{
"key": "fieldset",
"type": "fieldset",
"components": [{"key": "fieldset.textfield", "type": "textfield"}],
},
],
completed=True,
submitted_data={
"fieldset": {
"textfield": "some_string",
},
},
)
v2_options: RegistrationOptionsV2 = {
"version": 2,
# See the docker compose fixtures for more info on these values:
"objecttype": "http://objecttypes-web:8000/api/v2/objecttypes/644ab597-e88c-43c0-8321-f12113510b0e",
"objecttype_version": 1,
"upload_submission_csv": False,
"informatieobjecttype_submission_report": "",
"informatieobjecttype_attachment": "",
"organisatie_rsin": "000000000",
"variables_mapping": [
# fmt: off
{
"variable_key": "fieldset.textfield",
"target_path": ["textfield"],
},
# fmt: on
],
}

plugin = ObjectsAPIRegistration(PLUGIN_IDENTIFIER)

# Run the registration
result = plugin.register_submission(submission, v2_options)

record_data = result["record"]["data"]
self.assertEqual(record_data["textfield"], "some_string")

0 comments on commit 055a609

Please sign in to comment.