Skip to content

Commit

Permalink
feat: first PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Nov 29, 2024
1 parent 0e4f532 commit 03c617b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,30 +23,47 @@ annotation class AllowedParameters(
val payload: Array<KClass<*>> = [],
) {
class ParamValidator :
ConstraintValidator<AllowedParameters, MultiValueMap<String, String>?> {
ConstraintValidator<AllowedParameters, MultiValueMap<String, String>> {
private var implemented: List<String> = listOf()
private var notImplemented: List<String> = 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<String, String>?, context: ConstraintValidatorContext): Boolean {
if (value == null || implemented.containsAll(value.keys)) {
override fun isValid(params: MultiValueMap<String, String>, 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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/resources/shared.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 03c617b

Please sign in to comment.