jackson-module-kotlin
jackson-module-kotlin copied to clipboard
MissingKotlinParameterException should have a reference to KCallable
Use case
I want to convert a parameter error to a similar response as it would have been thrown when validation would have run and using the error messages of javax.validation.constraints
. To be specific:
I want to write a data class like:
data class NotNullExample(
@field:NotNull(message = "{my.translated.message}")
val notNullProperty: String
)
When making the variable nullable, I would get the message translated. With the definition above, it never gets to validation and throws a MissingKotlinParameterException
instead. I can catch an process this exception, thus accessing the parameter within the exception. Unfortunately, the parameter type's annotation list is empty (probably because the annotation is associated to the field) - which is required for all other validations as well. And KParameter
does not provide access to the callable (although KParameterImpl
has it).
So what I want to achieve is:
- get the callable that fails
- to get the return type
- to get the field the parameter is for
- to get the field annotation
- to get the annotation message
- to resolve the translated message
Describe the solution you'd like
If I can get
class MissingKotlinParameterException(
val callable: KCallable, // <-- new
val parameter: KParameter,
processor: JsonParser? = null,
msg: String
) : MismatchedInputException(processor, msg)
I can already proceed.
Describe alternatives you've considered
As a workaround, I will try to fetch the callable of Parameter via reflection. Getting the actual type might not work with other construction types and callable should be available already.