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(core): non existing attrs in append or update operations should be ignored #1059

Merged
merged 4 commits into from
Dec 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.egm.stellio.search.config
import org.springframework.context.annotation.Configuration
import org.springframework.http.codec.ServerCodecConfigurer
import org.springframework.web.reactive.config.EnableWebFlux
import org.springframework.web.reactive.config.PathMatchConfigurer
import org.springframework.web.reactive.config.WebFluxConfigurer

@Configuration
Expand All @@ -14,8 +13,4 @@ class WebConfig(private val searchProperties: SearchProperties) : WebFluxConfigu
configurer.defaultCodecs().enableLoggingRequestDetails(true)
configurer.defaultCodecs().maxInMemorySize(searchProperties.payloadMaxBodySize)
}

override fun configurePathMatching(configurer: PathMatchConfigurer) {
configurer.setUseTrailingSlashMatch(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ data class UpdateResult(
updated = this.updated.plus(other.updated),
notUpdated = this.notUpdated.plus(other.notUpdated)
)

@JsonIgnore
fun hasSuccessfulUpdate(): Boolean =
this.updated.isNotEmpty()
}

val EMPTY_UPDATE_RESULT: UpdateResult = UpdateResult(emptyList(), emptyList())
Expand Down Expand Up @@ -48,7 +52,8 @@ data class UpdateAttributeResult(
this.updateOperationResult in listOf(
UpdateOperationResult.APPENDED,
UpdateOperationResult.REPLACED,
UpdateOperationResult.UPDATED
UpdateOperationResult.UPDATED,
UpdateOperationResult.IGNORED
)
}

Expand All @@ -62,9 +67,6 @@ enum class UpdateOperationResult {
fun isSuccessResult(): Boolean = listOf(APPENDED, REPLACED, UPDATED).contains(this)
}

fun UpdateResult.hasSuccessfulUpdate(): Boolean =
this.updated.isNotEmpty()

fun updateResultFromDetailedResult(updateStatuses: List<UpdateAttributeResult>): UpdateResult {
val updated = updateStatuses.filter { it.isSuccessfullyUpdated() }
.map { UpdatedDetails(it.attributeName, it.datasetId, it.updateOperationResult) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,33 +492,32 @@ class EntityPayloadService(
expandedAttributes: ExpandedAttributes,
disallowOverwrite: Boolean,
sub: Sub?
): Either<APIException, UpdateResult> =
either {
val (coreAttrs, otherAttrs) =
expandedAttributes.toList().partition { JSONLD_EXPANDED_ENTITY_SPECIFIC_MEMBERS.contains(it.first) }
val createdAt = ZonedDateTime.now(ZoneOffset.UTC)
): Either<APIException, UpdateResult> = either {
val (coreAttrs, otherAttrs) =
expandedAttributes.toList().partition { JSONLD_EXPANDED_ENTITY_SPECIFIC_MEMBERS.contains(it.first) }
val createdAt = ZonedDateTime.now(ZoneOffset.UTC)

val operationType =
if (disallowOverwrite) APPEND_ATTRIBUTES
else APPEND_ATTRIBUTES_OVERWRITE_ALLOWED
val coreUpdateResult = updateCoreAttributes(entityUri, coreAttrs, createdAt, operationType).bind()
val attrsUpdateResult = temporalEntityAttributeService.appendEntityAttributes(
entityUri,
otherAttrs.toMap().toNgsiLdAttributes().bind(),
expandedAttributes,
disallowOverwrite,
createdAt,
sub
).bind()
val operationType =
if (disallowOverwrite) APPEND_ATTRIBUTES
else APPEND_ATTRIBUTES_OVERWRITE_ALLOWED
val coreUpdateResult = updateCoreAttributes(entityUri, coreAttrs, createdAt, operationType).bind()
val attrsUpdateResult = temporalEntityAttributeService.appendEntityAttributes(
entityUri,
otherAttrs.toMap().toNgsiLdAttributes().bind(),
expandedAttributes,
disallowOverwrite,
createdAt,
sub
).bind()

val updateResult = coreUpdateResult.mergeWith(attrsUpdateResult)
// update modifiedAt in entity if at least one attribute has been added
if (updateResult.hasSuccessfulUpdate()) {
val teas = temporalEntityAttributeService.getForEntity(entityUri, emptySet())
updateState(entityUri, createdAt, teas).bind()
}
updateResult
val updateResult = coreUpdateResult.mergeWith(attrsUpdateResult)
// update modifiedAt in entity if at least one attribute has been added
if (updateResult.hasSuccessfulUpdate()) {
val teas = temporalEntityAttributeService.getForEntity(entityUri, emptySet())
updateState(entityUri, createdAt, teas).bind()
}
updateResult
}

@Transactional
suspend fun updateAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class TemporalEntityAttributeService(
UpdateAttributeResult(
attributeName,
datasetId,
UpdateOperationResult.IGNORED,
UpdateOperationResult.FAILED,
"Unknown attribute $attributeName with datasetId $datasetId in entity $entityId"
)
}
Expand Down Expand Up @@ -819,7 +819,7 @@ class TemporalEntityAttributeService(
UpdateAttributeResult(
attributeName,
datasetId,
UpdateOperationResult.IGNORED,
UpdateOperationResult.FAILED,
"Unknown attribute $attributeName with datasetId $datasetId in entity $entityId"
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.egm.stellio.search.authorization.AuthorizationService
import com.egm.stellio.search.model.hasSuccessfulUpdate
import com.egm.stellio.search.service.EntityEventService
import com.egm.stellio.search.service.EntityPayloadService
import com.egm.stellio.search.service.QueryService
Expand Down Expand Up @@ -288,10 +287,6 @@ class EntityHandler(
{ it }
)

@DeleteMapping("/", "")
fun handleMissingEntityIdOnDelete(): ResponseEntity<*> =
missingPathErrorResponse("Missing entity id when trying to delete an entity")

/**
* Implements 6.6.3.1 - Append Entity Attributes
*
Expand Down Expand Up @@ -489,7 +484,7 @@ class EntityHandler(
{ it }
)

@DeleteMapping("/attrs/{attrId}", "/{entityId}/attrs")
@DeleteMapping("/attrs/{attrId}")
fun handleMissingEntityIdOrAttributeOnDeleteAttribute(): ResponseEntity<*> =
missingPathErrorResponse("Missing entity id or attribute id when trying to delete an attribute")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,11 @@ class TemporalEntityHandler(

@PatchMapping(
"/attrs/{attrId}/{instanceId}",
"/{entityId}/attrs/{instanceId}",
"/attrs/{instanceId}",
"/{entityId}/attrs",
"/attrs"
)
fun handleMissingParametersOnModifyInstanceTemporal(): ResponseEntity<*> =
missingPathErrorResponse(
"Missing some parameter(entity id, attribute id, instance id) when trying to modify temporal entity"
"Missing some parameter (entity id, attribute id, instance id) when trying to modify temporal entity"
)

/**
Expand All @@ -277,10 +274,6 @@ class TemporalEntityHandler(
{ it }
)

@DeleteMapping("/", "")
fun handleMissingEntityIdOnDeleteTemporalEntity(): ResponseEntity<*> =
missingPathErrorResponse("Missing entity id when trying to delete temporal entity")

/**
* Implements 6.21.3.1 - Delete Attribute from Temporal Representation of an Entity
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1256,20 +1256,11 @@ class TemporalEntityHandlerTests {
}

@Test
fun `delete temporal entity should return a 400 if entity id is missing`() {
fun `delete temporal entity should return a 405 if entity id is missing`() {
webClient.delete()
.uri("/ngsi-ld/v1/temporal/entities/")
.exchange()
.expectStatus().isBadRequest
.expectBody().json(
"""
{
"type":"https://uri.etsi.org/ngsi-ld/errors/BadRequestData",
"title":"The request includes input data which does not meet the requirements of the operation",
"detail":"Missing entity id when trying to delete temporal entity"
}
""".trimIndent()
)
.expectStatus().isEqualTo(HttpStatus.METHOD_NOT_ALLOWED)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.egm.stellio.shared.config

import org.springframework.http.server.reactive.ServerHttpRequest
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
import org.springframework.web.server.WebFilterChain
import reactor.core.publisher.Mono

@Component
class TrailingSlashRedirectFilter : WebFilter {

override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
val request: ServerHttpRequest = exchange.request
val path: String = request.path.value()
if (path.endsWith("/")) {
val newPath = path.removeSuffix("/")
val newRequest: ServerHttpRequest = request.mutate().path(newPath).build()
return chain.filter(exchange.mutate().request(newRequest).build())
}
return chain.filter(exchange)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.http.ProblemDetail
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.server.MethodNotAllowedException
import org.springframework.web.server.NotAcceptableStatusException
import org.springframework.web.server.UnsupportedMediaTypeStatusException

Expand Down Expand Up @@ -65,6 +66,8 @@ class ExceptionHandler {
HttpStatus.NOT_ACCEPTABLE,
NotAcceptableResponse(cause.message)
)
is MethodNotAllowedException ->
ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(cause.body)
is NonexistentTenantException -> generateErrorResponse(
HttpStatus.NOT_FOUND,
NonexistentTenantResponse(cause.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.egm.stellio.subscription.config
import org.springframework.context.annotation.Configuration
import org.springframework.http.codec.ServerCodecConfigurer
import org.springframework.web.reactive.config.EnableWebFlux
import org.springframework.web.reactive.config.PathMatchConfigurer
import org.springframework.web.reactive.config.WebFluxConfigurer

@Configuration
Expand All @@ -13,8 +12,4 @@ class WebConfig : WebFluxConfigurer {
override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
configurer.defaultCodecs().enableLoggingRequestDetails(true)
}

override fun configurePathMatching(configurer: PathMatchConfigurer) {
configurer.setUseTrailingSlashMatch(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ class SubscriptionHandler(
{ it }
)

@PatchMapping("/", "")
fun handleMissingIdOnUpdate(): ResponseEntity<*> =
missingPathErrorResponse("Missing id when trying to update a subscription")

/**
* Implements 6.11.3.3 - Delete Subscription
*/
Expand All @@ -185,10 +181,6 @@ class SubscriptionHandler(
{ it }
)

@DeleteMapping("/", "")
fun handleMissingIdOnDelete(): ResponseEntity<*> =
missingPathErrorResponse("Missing id when trying to delete a subscription")

private suspend fun checkSubscriptionExists(subscriptionId: URI): Either<APIException, Unit> =
subscriptionService.exists(subscriptionId)
.flatMap {
Expand Down