jackson-module-kotlin: Remove MissingKotlinParameterException and replace with MismatchedInputException

This change was originally scheduled to take place in 2.16, but later discussions postponed the decision to the 2.17 transition.


If the work on removing kotlin-reflect is reflected in jackson-module-kotlin, the MissingKotlinParameterException change is unavoidable since it references KParameter. https://github.com/FasterXML/jackson-module-kotlin/issues/450

The same applies to resolving the following issue https://github.com/FasterXML/jackson-module-kotlin/issues/572

I have also found a way to significant improve performance when the strictNullChecks option is enabled, but it is difficult to avoid changing error messages when enabling this option as well. https://github.com/ProjectMapK/jackson-module-kogera/pull/44

So, remove MissingKotlinParameterException and replace it with MismatchedInputException(MismatchedInputException is a superclass of MissingKotlinParameterException). This removal process will be done in phases, with deprecation in 2.15 and removal in 2.16.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 31 (23 by maintainers)

Commits related to this issue

Most upvoted comments

I’m a bit late, but just wanted to chime in and let you know my preference would be a replacement exception which contains the missing parameter name (as a couple of others have suggested).

Replacing MissingKotlinParameterException with the more generic MismatchedInputException causes a problem for us, and I think it is the same as @remen indicated, namely it becomes harder to figure out what the failure type is. Except for parsing the message of the exception, there is no way to find out what the failure type is. We also use the type of the exception to simplify the error so that we do not leak internals implementation details, so being able to figure out the type of failure is important to us.

If MissingKotlinParameterException has to be removed, maybe replace it with com.fasterxml.jackson.databind.exc.InvalidNullException, which to me seems to be an equivalent exception type. That way we can convert both to the same simplified exception indicating a missing or null field.

Indeed, the biggest problem is the KParameter contained in the MissingKotlinParameterException, and if that can be removed, the goal is achieved. From the points raised, I also understand that there is a specific use case. For this reason, I agree that there should be a unique Exception.

What I am struggling with is whether to keep it simple and just revive it or make a few more changes.

First, the name MissingKotlinParameterException is inappropriate for what is thrown from the strictNullChecks process. It occurs because an illegal null was entered, not because a parameter was missing. Therefore, I think it should be renamed or separate exceptions should be defined for parameters and strictNullChecks.

The next is whether the superclass should be changed to InvalidNullException when reviving MissingKotlinParameterException. From the perspective of expected usage, InvalidNullException seems to be a more appropriate parent class.

Personally, I am thinking of adding a new Exception named KotlinInvalidNullException that extends InvalidNullException. However, I will not work on it for a while as I need to think about it some more (I will work on it before 2.16).

WDYT @cowtowncoder ?

Would it be possible to keep MissingKotlinParameterException but change the type of the parameter to just a string (i.e. parameter.name)? Rationale: our global error handler in our project uses this exception to generate a simpler error message than “Instantiation of [simple type, class …] etc.” which leaks very internal implementation details.

I apologize for not addressing this issue as I have been extremely busy. Also, it seems difficult to address this issue in 2.19 as I am focusing on other improvements.

Here is what I plan to do.

With respect to deprecating MissingKotlinParameterException, I would like to start by improving StrictNullChecks. This is a method using JsonSetter.ContentNulls, which has already been implemented in an experimental project, and has the following advantages

This destructively changes the error thrown when StrictNullCheck is violated to InvalidNullException, but I believe the benefits outweigh the disadvantages. It also reduces the number of places where MissingKotlinParameterException is used, which allows for more precise consideration of the change policy.

From the Input Validation Requirement perspective, When some field is missing or null, I want respond The xxx is empty to the client. xxx is the field name. When some field is unexpected format such as pass alpha numeric to some number type field, I want respond The xxx has an invalid value/format to the client. Originally, MissingKotlinParameterException is helpful for judge if the mapping are failed due to missing or empty value. Ideally , I hope Jackson can easily to judge

  • if some field is missing
  • if some field value is empty
  • if some field value is invalid format ( such as the json field value can’t be convert to the application data structure defined type )
  • if some field are unknown ( present in json but not related application data structure field defined. should be configurable because sometime we just want ignore it. )

@cowtowncoder I have submitted a PR. Can you please confirm that it is as you have imagined? #720

Yes I would be ok to get PR that allows DeserializationContext to be null for constructor. Ideally this wouldn’t be the case (it really should only be called from places where DeserializationContext is available) but if not then it is not strictly required from implementation perspective.

@k163377 I do not have enough knowledge to give strong opinion, but it does sound good to me. So please proceed if it makes sense to you.