diff --git a/invenio_rdm_records/resources/serializers/ui/schema.py b/invenio_rdm_records/resources/serializers/ui/schema.py index 05395550a..b536db105 100644 --- a/invenio_rdm_records/resources/serializers/ui/schema.py +++ b/invenio_rdm_records/resources/serializers/ui/schema.py @@ -115,6 +115,18 @@ def mask_removed_by(obj): return return_value +def get_coordinates(obj): + """Coordinates determined by geometry type.""" + geometry_type = obj.get("type", None) + + if geometry_type == "Point": + return obj.get("coordinates", []) + elif geometry_type == "Polygon": + return obj.get("coordinates", [[[]]]) + else: + return None + + class RelatedIdentifiersSchema(Schema): """Localization of language titles.""" @@ -168,7 +180,7 @@ class GeometrySchema(Schema): """Schema for geometry in the UI.""" type = fields.Str() - coordinates = fields.List(fields.Float()) + coordinates = fields.Function(get_coordinates) class IdentifierSchema(Schema): diff --git a/tests/resources/serializers/test_ui_serializer.py b/tests/resources/serializers/test_ui_serializer.py index 89bf2d091..a9ff80d3e 100644 --- a/tests/resources/serializers/test_ui_serializer.py +++ b/tests/resources/serializers/test_ui_serializer.py @@ -98,6 +98,15 @@ def full_to_dict_record(full_record_to_dict): "place": "CERN", "description": "Invenio birth place.", }, + { + "geometry": { + "type": "Polygon", + "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]], + }, + "identifiers": [{"scheme": "geonames", "identifier": "123456"}], + "place": "Rectangle", + "description": "Example Polygon", + }, ] } @@ -256,6 +265,15 @@ def test_ui_serializer(app, full_to_dict_record): "place": "CERN", "description": "Invenio birth place.", }, + { + "geometry": { + "type": "Polygon", + "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]], + }, + "identifiers": [{"scheme": "geonames", "identifier": "123456"}], + "place": "Rectangle", + "description": "Example Polygon", + }, ] }, }