From 03c617bb7f118e9365310860b72b17d5c7474022 Mon Sep 17 00:00:00 2001 From: Thomas BOUSSELIN Date: Fri, 29 Nov 2024 10:31:03 +0100 Subject: [PATCH] feat: first PR comments --- .../model/parameter/AllowedParameters.kt | 44 +++++++++++++------ .../stellio/shared/web/ExceptionHandler.kt | 11 +++-- shared/src/main/resources/shared.properties | 2 +- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/model/parameter/AllowedParameters.kt b/shared/src/main/kotlin/com/egm/stellio/shared/model/parameter/AllowedParameters.kt index e66e6167c..da0afd689 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/model/parameter/AllowedParameters.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/model/parameter/AllowedParameters.kt @@ -3,6 +3,7 @@ package com.egm.stellio.shared.model.parameter import jakarta.validation.Constraint import jakarta.validation.ConstraintValidator import jakarta.validation.ConstraintValidatorContext +import jdk.internal.org.jline.utils.AttributedStringBuilder.append import org.springframework.http.HttpStatus import org.springframework.util.MultiValueMap import kotlin.reflect.KClass @@ -22,30 +23,47 @@ annotation class AllowedParameters( val payload: Array> = [], ) { class ParamValidator : - ConstraintValidator?> { + ConstraintValidator> { private var implemented: List = listOf() private var notImplemented: List = listOf() - override fun initialize(requiredIfChecked: AllowedParameters) { - this.implemented = requiredIfChecked.implemented.map(QueryParameter::key) - this.notImplemented = requiredIfChecked.notImplemented.map(QueryParameter::key) + override fun initialize(allowedParameters: AllowedParameters) { + this.implemented = allowedParameters.implemented.map(QueryParameter::key) + this.notImplemented = allowedParameters.notImplemented.map(QueryParameter::key) } - override fun isValid(value: MultiValueMap?, context: ConstraintValidatorContext): Boolean { - if (value == null || implemented.containsAll(value.keys)) { + override fun isValid(params: MultiValueMap, context: ConstraintValidatorContext): Boolean { + if (implemented.containsAll(params.keys)) { return true } + + val notImplementedKeys = params.keys.intersect(notImplemented) + val errorKeys = params.keys - notImplementedKeys - implemented + context.disableDefaultConstraintViolation() + + val message = StringBuilder().apply { + if (notImplementedKeys.isNotEmpty()) { + append( + "The '${notImplementedKeys.joinToString("', '")}' parameter is not implemented yet. " + ) + } + if (errorKeys.isNotEmpty()) { + append( + "The '${errorKeys.joinToString("', '")}' parameter is not allowed on this endpoint. " + ) + } + append( + "Accepted parameters are '${implemented.joinToString("', '")}'. " + ) + }.toString() + context.buildConstraintViolationWithTemplate( - "Accepted parameters are '${implemented.joinToString("', '")}'" + message + ).addPropertyNode( + if (notImplemented.isEmpty()) HttpStatus.BAD_REQUEST.name else HttpStatus.NOT_IMPLEMENTED.name ).addConstraintViolation() - if (value.keys.any { it in notImplemented }) { - context.buildConstraintViolationWithTemplate( - "The '${notImplemented.joinToString("', '")}' parameters are not implemented yet " - ).addPropertyNode(HttpStatus.NOT_IMPLEMENTED.name).addConstraintViolation() - return false - } return false } } diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/web/ExceptionHandler.kt b/shared/src/main/kotlin/com/egm/stellio/shared/web/ExceptionHandler.kt index 8111fffda..0ba6152aa 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/web/ExceptionHandler.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/web/ExceptionHandler.kt @@ -39,14 +39,13 @@ class ExceptionHandler { is MethodNotAllowedException -> ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(cause.body) is ConstraintViolationException -> { - val message = cause.constraintViolations.map { it.message }.joinToString(" . ") - if (cause.constraintViolations.any // todo simplify - { constraint -> constraint.propertyPath.any { it.name == HttpStatus.NOT_IMPLEMENTED.name } } - ) { + val message = cause.constraintViolations.joinToString(" . ") { it.message } + if (cause.constraintViolations.flatMap { it.propertyPath } + .any { it.name == HttpStatus.NOT_IMPLEMENTED.name } + ) NotImplementedException(message).toErrorResponse() - } else { + else InvalidRequestException(message).toErrorResponse() - } } else -> InternalErrorException("$cause").toErrorResponse() diff --git a/shared/src/main/resources/shared.properties b/shared/src/main/resources/shared.properties index 15d235952..bda56c3bb 100644 --- a/shared/src/main/resources/shared.properties +++ b/shared/src/main/resources/shared.properties @@ -8,7 +8,7 @@ application.authentication.enabled = false # Pagination config for query resources endpoints application.pagination.limit-default = 30 application.pagination.limit-max = 100 -application.pagination.temporal-limit = 10 +application.pagination.temporal-limit = 10000 # default core context used when not provided in the query application.contexts.core = https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.8.jsonld