Skip to content

Fixed old StrictNullChecks to throw exceptions similar to those thrown by new StrictNullChecks #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2025

Conversation

k163377
Copy link
Contributor

@k163377 k163377 commented Jul 12, 2025

To deprecate the old StrictNullChecks option, changes were made to make the exceptions thrown by the old process the similar to the new StrictNullChecks.
This means that the old StrictNullChecks will no longer throw MissingKotlinParameterException.

In the situation where this error occurs, it was inappropriate to use MissingKotlinParameterException because the input was not missing.
This is also relevant for #617.


The following is a comparison of the respective messages displayed when printStackTrace is performed on the result of assertThrows for com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTest(NewStrictNullChecks) and StrictNullChecksTestOld.
As for messages, it has been improved so that even the key of a value that was null can be checked.

ClassWithArrayOfInt

NewStrictNullChecks
com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "samples"
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 16] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTest$ClassWithArrayOfInt["samples"]->java.lang.Object[][1])
Fixed old StrictNullChecks
com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "samples"
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 21] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTestOld$ClassWithArrayOfInt["samples"]->java.lang.Integer[][1])
Conventional old StrictNullChecks
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of kotlin.Int kotlin.Array<kotlin.Int> failed for JSON property samples due to null value in a kotlin.Array<kotlin.Int> that does not allow null values
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 21] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTestOld$ClassWithArrayOfInt["samples"])

ClassWithListOfInt

NewStrictNullChecks
com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "samples"
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 16] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTest$ClassWithListOfInt["samples"]->java.util.ArrayList[1])
Fixed old StrictNullChecks
com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "samples"
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 21] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTestOld$ClassWithListOfInt["samples"]->java.util.ArrayList[1])
Conventional old StrictNullChecks
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of kotlin.Int kotlin.collections.List<kotlin.Int> failed for JSON property samples due to null value in a kotlin.collections.List<kotlin.Int> that does not allow null values
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 21] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTestOld$ClassWithListOfInt["samples"])

ClassWithMapOfStringToInt

NewStrictNullChecks
com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "samples"
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 23] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTest$ClassWithMapOfStringToInt["samples"]->java.util.LinkedHashMap["key"])
Fixed old StrictNullChecks
com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "samples"
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 30] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTestOld$ClassWithMapOfStringToInt["samples"]->java.util.LinkedHashMap["key"])
Conventional old StrictNullChecks
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of kotlin.Int kotlin.collections.Map<kotlin.String, kotlin.Int> failed for JSON property samples due to null value in a kotlin.collections.Map<kotlin.String, kotlin.Int> that does not allow null values
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 30] (through reference chain: com.fasterxml.jackson.module.kotlin.test.StrictNullChecksTestOld$ClassWithMapOfStringToInt["samples"])

@k163377 k163377 marked this pull request as draft July 12, 2025 12:33
@k163377 k163377 requested a review from Copilot July 12, 2025 12:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR aligns the old strict-null-check behavior with the new implementation by replacing MissingKotlinParameterException with InvalidNullException and updating the value instantiator to throw InvalidNullException for null elements in collections, maps, and arrays.

  • Tests now expect InvalidNullException instead of MissingKotlinParameterException.
  • KotlinValueInstantiator imports and uses InvalidNullException.from(...) to construct and wrap exceptions when encountering null items.
  • Removed the old branching logic and replaced it with a when block that creates and throws the new exception type.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTestOld.kt Updated tests to assert InvalidNullException and removed the old exception import.
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt Added import for InvalidNullException and replaced the old null-check branches with logic that constructs and throws InvalidNullException.
Comments suppressed due to low confidence (2)

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt:109

  • [nitpick] Variable name ex is quite generic. Rename it to something more descriptive like nullException to clarify its purpose.
                val ex = when {

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTestOld.kt:35

  • Consider extending this test to verify that the thrown InvalidNullException includes the correct JSON path or property name/index, ensuring the wrapping behavior is fully covered.
        assertThrows<InvalidNullException> {

itemType = arguments[0].type
// To make the behavior the same as deserialization of each element using NullsFailProvider,
// first wrapWithPath with paramVal and key.
val ex = when {
Copy link
Preview

Copilot AI Jul 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The when block that builds InvalidNullException contains repeated patterns for collection and array cases. Consider extracting a helper function to handle index-based null checks and exception wrapping to reduce duplication and improve readability.

Copilot uses AI. Check for mistakes.

@k163377 k163377 marked this pull request as ready for review July 12, 2025 13:02
@k163377 k163377 changed the title 【WIP】Fixed old StrictNullChecks to throw exceptions similar to those thrown by new StrictNullChecks Fixed old StrictNullChecks to throw exceptions similar to those thrown by new StrictNullChecks Jul 12, 2025
@k163377 k163377 merged commit d8b681b into FasterXML:2.x Jul 12, 2025
15 checks passed
@k163377 k163377 deleted the mkpe branch July 12, 2025 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant