-
-
Notifications
You must be signed in to change notification settings - Fork 182
Deprecate MissingKotlinParameterException and replace with new exception #1025
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b201287
Changed to throw a new exception instead of using the old exception
k163377 279e639
Fix tests
k163377 2630290
Update docs
k163377 4d39dd4
Add tests
k163377 25b940e
Fix comment
k163377 e2e6b48
Suppress DEPRECATION_ERROR
k163377 4ab4308
Fix to test that KotlinInvalidNullException is thrown
k163377 50f5527
Update release notes wrt #1025
k163377 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/fasterxml/jackson/module/kotlin/KotlinInvalidNullException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.fasterxml.jackson.module.kotlin; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.databind.JsonMappingException; | ||
| import com.fasterxml.jackson.databind.PropertyName; | ||
| import com.fasterxml.jackson.databind.exc.InvalidNullException; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| // Due to a limitation in KT-6653, there is no user-friendly way to override Java getters in Kotlin. | ||
| // The reason for not having detailed information(e.g. KParameter) is to keep the class Serializable. | ||
| /** | ||
| * Specialized {@link JsonMappingException} sub-class used to indicate that a mandatory Kotlin creator parameter was | ||
| * missing or null. | ||
| */ | ||
| public final class KotlinInvalidNullException extends InvalidNullException { | ||
| @NotNull | ||
| private final String kotlinPropertyName; | ||
|
|
||
| KotlinInvalidNullException( | ||
| @Nullable | ||
| String kotlinParameterName, | ||
| @NotNull | ||
| Class<?> valueClass, | ||
| @NotNull | ||
| JsonParser p, | ||
| @NotNull | ||
| String msg, | ||
| @NotNull | ||
| PropertyName pname | ||
| ) { | ||
| super(p, msg, pname); | ||
| // Basically, this will never be null, but it is handled here to avoid errors in unusual cases. | ||
| this.kotlinPropertyName = kotlinParameterName == null ? "UNKNOWN" : kotlinParameterName; | ||
| this._targetType = valueClass; | ||
| } | ||
|
|
||
| /** | ||
| * @return Parameter name in Kotlin. | ||
| */ | ||
| @NotNull | ||
| public String getKotlinPropertyName() { | ||
| return kotlinPropertyName; | ||
| } | ||
|
|
||
| // region: Override getters to make nullability explicit and to explain its role in this class. | ||
| /** | ||
| * @return Parameter name in Jackson. | ||
| */ | ||
| @NotNull | ||
| @Override | ||
| public PropertyName getPropertyName() { | ||
| return super.getPropertyName(); | ||
| } | ||
|
|
||
| /** | ||
| * @return The {@link Class} object representing the class that declares the creator. | ||
| */ | ||
| @NotNull | ||
| @Override | ||
| public Class<?> getTargetType() { | ||
| return super.getTargetType(); | ||
| } | ||
| // endregion | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInvalidNullExceptionTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.fasterxml.jackson.module.kotlin | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.assertThrows | ||
| import kotlin.test.assertEquals | ||
|
|
||
| private data class Dto( | ||
| val foo: String, | ||
| @JsonProperty("bar") | ||
| val _bar: String | ||
| ) | ||
|
|
||
| class KotlinInvalidNullExceptionTest { | ||
| @Test | ||
| fun fooTest() { | ||
| val json = """{"bar":"bar"}""" | ||
| val ex = assertThrows<KotlinInvalidNullException> { defaultMapper.readValue<Dto>(json) } | ||
|
|
||
| assertEquals("foo", ex.kotlinPropertyName) | ||
| assertEquals("foo", ex.propertyName.simpleName) | ||
| assertEquals(Dto::class, ex.targetType.kotlin) | ||
| } | ||
|
|
||
| @Test | ||
| fun barTest() { | ||
| val json = """{"foo":"foo","bar":null}""" | ||
| val ex = assertThrows<KotlinInvalidNullException> { defaultMapper.readValue<Dto>(json) } | ||
|
|
||
| assertEquals("_bar", ex.kotlinPropertyName) | ||
| assertEquals("bar", ex.propertyName.simpleName) | ||
| assertEquals(Dto::class, ex.targetType.kotlin) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.