Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do a partial update patch in update attributes (and not a replace) #1310

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading