jackson-module-kotlin icon indicating copy to clipboard operation
jackson-module-kotlin copied to clipboard

MissingKotlinParameterException should have a reference to KCallable

Open dirkbolte opened this issue 3 years ago • 0 comments

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:

  1. get the callable that fails
  2. to get the return type
  3. to get the field the parameter is for
  4. to get the field annotation
  5. to get the annotation message
  6. 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.

dirkbolte avatar Feb 09 '22 08:02 dirkbolte