Skip to content

Commit

Permalink
fix: do a partial update patch in update attributes (and not a replac…
Browse files Browse the repository at this point in the history
…e) (#1310)
  • Loading branch information
bobeal authored Jan 9, 2025
1 parent e88ba43 commit 655717f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
1 change: 1 addition & 0 deletions search-service/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( attribute: Attribute, attributeName: ExpandedTerm, attributeMetadata: AttributeMetadata, mergedAt: ZonedDateTime, observedAt: ZonedDateTime?, attributePayload: ExpandedAttributeInstance, sub: Sub? )</ID>
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( attribute: Attribute, ngsiLdAttribute: NgsiLdAttribute, attributeMetadata: AttributeMetadata, createdAt: ZonedDateTime, attributePayload: ExpandedAttributeInstance, sub: Sub? )</ID>
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( entityId: URI, attributeName: ExpandedTerm, attributeMetadata: AttributeMetadata, createdAt: ZonedDateTime, attributePayload: ExpandedAttributeInstance, sub: Sub? )</ID>
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( entityId: URI, attributeName: ExpandedTerm, datasetId: URI?, attributeValues: ExpandedAttributeInstance, modifiedAt: ZonedDateTime, sub: Sub? )</ID>
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( entityUri: URI, ngsiLdAttributes: List&lt;NgsiLdAttribute&gt;, expandedAttributes: ExpandedAttributes, createdAt: ZonedDateTime, observedAt: ZonedDateTime?, sub: Sub? )</ID>
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( entityUri: URI, ngsiLdAttributes: List&lt;NgsiLdAttribute&gt;, expandedAttributes: ExpandedAttributes, disallowOverwrite: Boolean, createdAt: ZonedDateTime, sub: Sub? )</ID>
<ID>LongParameterList:TemporalEntityHandler.kt$TemporalEntityHandler$( @RequestHeader httpHeaders: HttpHeaders, @PathVariable entityId: URI, @PathVariable attrId: String, @PathVariable instanceId: URI, @RequestBody requestBody: Mono&lt;String&gt;, @AllowedParameters(notImplemented = [QP.LOCAL, QP.VIA]) @RequestParam queryParams: MultiValueMap&lt;String, String&gt; )</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,21 +705,14 @@ class EntityAttributeService(
createdAt
).bind().first()
} else {
replaceAttribute(
currentAttribute,
ngsiLdAttribute,
attributeMetadata,
createdAt,
applyPartialUpdatePatchOperation(
entityUri,
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId,
attributePayload,
createdAt,
sub
).map {
SucceededAttributeOperationResult(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId,
OperationStatus.REPLACED,
attributePayload
)
}.bind()
).bind()
}
}
}.fold({ it.left() }, { it.right() })
Expand Down Expand Up @@ -754,40 +747,59 @@ class EntityAttributeService(
modifiedAt
).bind().first()
} else {
// first update payload in temporal entity attribute
val attribute = getForEntityAndAttribute(entityId, attributeName, datasetId).bind()
attributeValues[JSONLD_TYPE]?.let {
ensure(isAttributeOfType(attributeValues, AttributeType(NGSILD_PREFIX + attribute.attributeType))) {
BadRequestDataException("The type of the attribute has to be the same as the existing one")
}
}
val (jsonTargetObject, updatedAttributeInstance) =
partialUpdatePatch(attribute.payload.toExpandedAttributeInstance(), attributeValues)
val value = getValueFromPartialAttributePayload(attribute, updatedAttributeInstance)
val attributeValueType = guessAttributeValueType(attribute.attributeType, attributeValues)
updateOnUpdate(attribute.id, attributeValueType, modifiedAt, jsonTargetObject).bind()

// then update attribute instance
val attributeInstance = createContextualAttributeInstance(
attribute,
updatedAttributeInstance,
value,
modifiedAt,
sub
)
attributeInstanceService.create(attributeInstance).bind()

SucceededAttributeOperationResult(
applyPartialUpdatePatchOperation(
entityId,
attributeName,
datasetId,
OperationStatus.UPDATED,
updatedAttributeInstance
)
attributeValues,
modifiedAt,
sub
).bind()
}

attributeOperationResult
}

@Transactional
suspend fun applyPartialUpdatePatchOperation(
entityId: URI,
attributeName: ExpandedTerm,
datasetId: URI?,
attributeValues: ExpandedAttributeInstance,
modifiedAt: ZonedDateTime,
sub: Sub?
): Either<APIException, SucceededAttributeOperationResult> = either {
// first update payload in temporal entity attribute
val attribute = getForEntityAndAttribute(entityId, attributeName, datasetId).bind()
attributeValues[JSONLD_TYPE]?.let {
ensure(isAttributeOfType(attributeValues, AttributeType(NGSILD_PREFIX + attribute.attributeType))) {
BadRequestDataException("The type of the attribute has to be the same as the existing one")
}
}
val (jsonTargetObject, updatedAttributeInstance) =
partialUpdatePatch(attribute.payload.toExpandedAttributeInstance(), attributeValues)
val value = getValueFromPartialAttributePayload(attribute, updatedAttributeInstance)
val attributeValueType = guessAttributeValueType(attribute.attributeType, attributeValues)
updateOnUpdate(attribute.id, attributeValueType, modifiedAt, jsonTargetObject).bind()

// then update attribute instance
val attributeInstance = createContextualAttributeInstance(
attribute,
updatedAttributeInstance,
value,
modifiedAt,
sub
)
attributeInstanceService.create(attributeInstance).bind()

SucceededAttributeOperationResult(
attributeName,
datasetId,
OperationStatus.UPDATED,
updatedAttributeInstance
)
}

@Transactional
suspend fun upsertAttributes(
entityUri: URI,
Expand Down

0 comments on commit 655717f

Please sign in to comment.