From 793a5ff6a522bd305c06ca1fa48f39cb6a5d8dda Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Mon, 27 Nov 2023 08:31:35 +0100 Subject: [PATCH] fix(core): ensure no type change in partial attribute update operation (#1051) --- .../search/service/TemporalEntityAttributeService.kt | 9 +++++++++ .../kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt | 1 + 2 files changed, 10 insertions(+) diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/service/TemporalEntityAttributeService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/service/TemporalEntityAttributeService.kt index eedf10f2c..b450a38d1 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/service/TemporalEntityAttributeService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/service/TemporalEntityAttributeService.kt @@ -4,13 +4,17 @@ import arrow.core.Either import arrow.core.flatMap import arrow.core.left import arrow.core.raise.either +import arrow.core.raise.ensure import arrow.core.right import arrow.fx.coroutines.parMap import com.egm.stellio.search.model.* import com.egm.stellio.search.util.* import com.egm.stellio.shared.model.* import com.egm.stellio.shared.util.* +import com.egm.stellio.shared.util.AttributeType +import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_TYPE import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_OBSERVED_AT_PROPERTY +import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PREFIX import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PROPERTY_VALUE import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_HAS_OBJECT import com.egm.stellio.shared.util.JsonLdUtils.buildNonReifiedDateTime @@ -655,6 +659,11 @@ class TemporalEntityAttributeService( if (exists) { // first update payload in temporal entity attribute val tea = getForEntityAndAttribute(entityId, attributeName, datasetId).bind() + attributeValues[JSONLD_TYPE]?.let { + ensure(isAttributeOfType(attributeValues, AttributeType(NGSILD_PREFIX + tea.attributeType))) { + BadRequestDataException("The type of the attribute has to be the same as the existing one") + } + } val (jsonTargetObject, updatedAttributeInstance) = mergeAttributePayload(tea, attributeValues) val value = getValueFromPartialAttributePayload(tea, updatedAttributeInstance) val attributeValueType = guessAttributeValueType(tea.attributeType, attributeValues) diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt b/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt index abeb14569..cad467492 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt @@ -38,6 +38,7 @@ object JsonLdUtils { "https://raw.githubusercontent.com/easy-global-market/ngsild-api-data-models/master" const val NGSILD_EGM_CONTEXT = "$EGM_BASE_CONTEXT_URL/shared-jsonld-contexts/egm.jsonld" + const val NGSILD_PREFIX = "https://uri.etsi.org/ngsi-ld/" const val NGSILD_DEFAULT_VOCAB = "https://uri.etsi.org/ngsi-ld/default-context/" val NGSILD_PROPERTY_TYPE = AttributeType("https://uri.etsi.org/ngsi-ld/Property")