diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..1036ca72e --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: [k163377, cowtowncoder] +tidelift: maven/com.fasterxml.jackson.module:jackson-module-kotlin diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml index c5f764037..c93e89e1d 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -10,14 +10,17 @@ body: label: Search before asking description: |- Please search [issues](https://github.com/FasterXML/jackson-module-kotlin/issues) to check if your issue has already been reported. + Check [all value class labeled issues](https://github.com/FasterXML/jackson-module-kotlin/issues?q=is%3Aopen+is%3Aissue+label%3A%22value+class%22), especially if the problem is related to a value class. Also, KotlinModule is only an extension of databind, so it is not an appropriate place to report problems with databind or other modules. Please try to ensure that the problem occurs only in Kotlin, and not regular Java objects. options: - label: "I searched in the [issues](https://github.com/FasterXML/jackson-module-kotlin/issues) and found nothing similar." required: true + - label: "I have confirmed that the same problem is not reproduced if I exclude the KotlinModule." + required: true - label: "I searched in the [issues of databind](https://github.com/FasterXML/jackson-databind/issues) and other modules used and found nothing similar." required: false - - label: "I have confirmed that the problem only occurs when using Kotlin." + - label: "I have confirmed that the problem does not reproduce in Java and only occurs when using Kotlin and KotlinModule." required: false - type: textarea id: bug-description diff --git a/.github/workflows/dep_build_v2.yml b/.github/workflows/dep_build_v2.yml index 48ef8416a..0598f8435 100644 --- a/.github/workflows/dep_build_v2.yml +++ b/.github/workflows/dep_build_v2.yml @@ -10,21 +10,21 @@ permissions: jobs: build: - runs-on: 'ubuntu-22.04' + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - java_version: ['8', '17', '21', '23'] + java_version: ['8', '17', '21', '24'] # Versions need to align with ones in 'main.yml' workflow - # kotlin-reflect 1.8.2x has a bug and some tests fail, so we are downgrading to 1.8.10. - # https://youtrack.jetbrains.com/issue/KT-65156 - kotlin_version: ['1.8.10', '1.9.24', '2.0.21', '2.1.0'] + kotlin_version: ['1.9.24', '2.0.21', '2.1.20'] env: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: - - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: 2.x - name: Set up JDK - uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1 + uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 with: distribution: 'temurin' java-version: ${{ matrix.java_version }} diff --git a/.github/workflows/dep_build_v3.yml b/.github/workflows/dep_build_v3.yml index 2379d3230..da61ee1f9 100644 --- a/.github/workflows/dep_build_v3.yml +++ b/.github/workflows/dep_build_v3.yml @@ -2,36 +2,30 @@ name: Re-build on jackson-databind v3 push on: repository_dispatch: types: [jackson-databind-pushed-v3] - # just for testing - workflow_dispatch: permissions: contents: read jobs: build: - runs-on: 'ubuntu-22.04' + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - java_version: ['8', '17', '21', '23'] + java_version: ['17', '21', '24'] # Versions need to align with ones in 'main.yml' workflow - # kotlin-reflect 1.8.2x has a bug and some tests fail, so we are downgrading to 1.8.10. - # https://youtrack.jetbrains.com/issue/KT-65156 - kotlin_version: ['1.8.10', '1.9.24', '2.0.21', '2.1.0'] + kotlin_version: ['1.9.24', '2.0.21', '2.1.20'] env: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: - - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: - ref: master + ref: 3.x - name: Set up JDK - uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1 + uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 with: distribution: 'temurin' java-version: ${{ matrix.java_version }} cache: 'maven' - name: Build and test run: ./mvnw -B -ff -ntp -Dversion.kotlin=${{ matrix.kotlin_version }} clean verify - -# No recursive rebuild (yet?) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1f6a54084..439688735 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,25 +15,23 @@ permissions: jobs: build: - runs-on: 'ubuntu-latest' + runs-on: ubuntu-latest strategy: fail-fast: false max-parallel: 5 matrix: - java_version: ['8', '11', '17', '21', '23'] - # kotlin-reflect 1.8.2x has a bug and some tests fail, so we are downgrading to 1.8.10. - # https://youtrack.jetbrains.com/issue/KT-65156 - kotlin_version: ['1.8.10', '1.9.24', '2.0.21', '2.1.0'] + java_version: ['8', '11', '17', '21', '24'] + kotlin_version: ['1.9.25', '2.0.21', '2.1.20'] include: - java_version: '8' - kotlin_version: '1.8.10' + kotlin_version: '1.9.25' release_build: 'R' env: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - name: Set up JDK - uses: actions/setup-java@v4 + uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 with: distribution: 'temurin' java-version: ${{ matrix.java_version }} diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties index ca5ab4bab..b9b1153ae 100644 --- a/.mvn/wrapper/maven-wrapper.properties +++ b/.mvn/wrapper/maven-wrapper.properties @@ -14,5 +14,5 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar diff --git a/README.md b/README.md index b0f58245d..ce791cbf7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -[![Kotlin](https://img.shields.io/badge/kotlin-1.5.x-blue.svg)](https://kotlinlang.org) [![Kotlin Slack](https://img.shields.io/badge/chat-kotlin%20slack-orange.svg)](https://slack.kotlinlang.org/) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.module/jackson-module-kotlin/badge.svg)](https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-kotlin) +[![Change log](https://img.shields.io/badge/change%20log-%E2%96%A4-yellow.svg)](./release-notes/VERSION-2.x) +[![Tidelift](https://tidelift.com/badges/package/maven/com.fasterxml.jackson.module:jackson-module-kotlin)](https://tidelift.com/subscription/pkg/maven-com-fasterxml-jackson-module-jackson-module-kotlin?utm_source=maven-com-fasterxml-jackson-module-jackson-module-kotlin&utm_medium=referral&utm_campaign=readme) +[![Kotlin Slack](https://img.shields.io/badge/chat-kotlin%20slack-orange.svg)](https://slack.kotlinlang.org/) # Overview @@ -10,17 +13,15 @@ and those with secondary constructors or static factories are also supported. # Status -* release `2.18.2` (for Jackson `2.18.x`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.18)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.18) +* release `2.19.0` (for Jackson `2.19.0`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.19)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.19) +* release `2.18.3` (for Jackson `2.18.x`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.18)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.18) * release `2.17.3` (for Jackson `2.17.x`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.17)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.17) -* release `2.16.2` (for Jackson `2.16.x`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.16)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.16) -* release `2.15.4` (for Jackson `2.15.x`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.15)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.15) -* release `2.14.3` (for Jackson `2.14.x`) [![GitHub Actions build](https://github.com/FasterXML/jackson-module-kotlin/actions/workflows/main.yml/badge.svg?branch=2.14)](https://github.com/FasterXML/jackson-module-kotlin/actions?query=branch%3A2.14) Releases require that you have included Kotlin stdlib and reflect libraries already. Gradle: ``` -implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.18.+" +implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.19.+" ``` Maven: @@ -28,7 +29,7 @@ Maven: com.fasterxml.jackson.module jackson-module-kotlin - 2.18.2 + 2.19.0 ``` @@ -111,12 +112,9 @@ println(arrayNode.toString()) // ["foo",true,1,1.0,"YmFy"] Different `kotlin-core` versions are supported by different Jackson Kotlin module minor versions. Here is an incomplete list of supported versions: +* Jackson 2.19.x: Kotlin-core 1.9 - 2.1 * Jackson 2.18.x: Kotlin-core 1.8 - 2.1 * Jackson 2.17.x: Kotlin-core 1.7 - 2.0 -* Jackson 2.16.x: Kotlin-core 1.6 - 1.9 -* Jackson 2.15.x: Kotlin-core 1.5 - 1.8 -* Jackson 2.14.x: Kotlin-core 1.4 - 1.8 -* Jackson 2.13.x: Kotlin-core 1.4 - 1.7 Please note that the versions supported by 2.17 are tentative and may change depending on the release date. @@ -193,7 +191,7 @@ when(root){ # Configuration The Kotlin module may be given a few configuration parameters at construction time; -see the [inline documentation](https://github.com/FasterXML/jackson-module-kotlin/blob/master/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt) +see the [inline documentation](https://github.com/FasterXML/jackson-module-kotlin/blob/2.19/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt) for details on what options are available and what they do. ```kotlin @@ -256,9 +254,9 @@ See the [main Jackson contribution guidelines](https://github.com/FasterXML/jack If you are going to write code, choose the appropriate base branch: -- `2.17` for bugfixes against the current stable version -- `2.18` for additive functionality & features or [minor](https://semver.org), backwards compatible changes to existing behavior to be included in the next minor version release -- `master` for significant changes to existing behavior, which will be part of Jackson 3.0 +- `2.18` for bugfixes against the current stable version +- `2.19` for additive functionality & features or [minor](https://semver.org), backwards compatible changes to existing behavior to be included in the next minor version release +- `3.x` for significant changes to existing behavior, which will be part of Jackson 3.0 ### Failing tests diff --git a/pom.xml b/pom.xml index 97026cb13..a26e15816 100644 --- a/pom.xml +++ b/pom.xml @@ -8,12 +8,12 @@ com.fasterxml.jackson jackson-base - 2.18.5-SNAPSHOT + 2.19.1-SNAPSHOT com.fasterxml.jackson.module jackson-module-kotlin jackson-module-kotlin - 2.18.5-SNAPSHOT + 2.19.1-SNAPSHOT bundle Add-on module for Jackson (https://github.com/FasterXML/jackson/) to support Kotlin language, specifically introspection of method/constructor parameter names, @@ -62,9 +62,7 @@ 1.8 1.8 - - - 1.8.10 + 1.9.25 com/fasterxml/jackson/module/kotlin @@ -111,15 +109,11 @@ compile - - - junit - junit - test - + + org.jetbrains.kotlin - kotlin-test-junit + kotlin-test-junit5 ${version.kotlin} test @@ -128,6 +122,11 @@ jackson-dataformat-xml test + + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + test + com.fasterxml.jackson.datatype @@ -215,7 +214,7 @@ - de.jjohannes + org.gradlex gradle-module-metadata-maven-plugin @@ -233,7 +232,7 @@ --> - 2.17.0 + 2.18.0 jar @@ -249,43 +248,16 @@ - com.fasterxml.jackson.module.kotlin.KotlinModule#KotlinModule(int,boolean,boolean,boolean,com.fasterxml.jackson.module.kotlin.SingletonSupport,boolean,boolean,boolean) - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#getNullIsSameAsDefault() - - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#nullIsSameAsDefault(boolean) - - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#getNullToEmptyCollection() - - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#nullToEmptyCollection(boolean) - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#getNullToEmptyMap() - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#nullToEmptyMap(boolean) - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#getSingletonSupport() - - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#singletonSupport(com.fasterxml.jackson.module.kotlin.SingletonSupport) - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#getStrictNullChecks() - - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#strictNullChecks(boolean) - - com.fasterxml.jackson.module.kotlin.KotlinModule$Builder#reflectionCacheSize(int) + com.fasterxml.jackson.module.kotlin.KotlinModule#getUseKotlinPropertyNameForGetter() + com.fasterxml.jackson.module.kotlin.KotlinModule#getSingletonSupport() + com.fasterxml.jackson.module.kotlin.SingletonSupport - com.fasterxml.jackson.module.kotlin.KotlinNamesAnnotationIntrospector#KotlinNamesAnnotationIntrospector(com.fasterxml.jackson.module.kotlin.ReflectionCache,java.util.Set,boolean) + com.fasterxml.jackson.module.kotlin.KotlinNamesAnnotationIntrospector#KotlinNamesAnnotationIntrospector(com.fasterxml.jackson.module.kotlin.ReflectionCache,boolean) - - com.fasterxml.jackson.module.kotlin.ReflectionCache#checkConstructorIsCreatorAnnotated(com.fasterxml.jackson.databind.introspect.AnnotatedConstructor,kotlin.jvm.functions.Function1) - - + com.fasterxml.jackson.module.kotlin.KotlinKeyDeserializers#INSTANCE + com.fasterxml.jackson.module.kotlin.ReflectionCache$BooleanTriState diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 920e4df73..d56cae886 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -15,6 +15,34 @@ Authors: Contributors: +# 2.19.1 (not yet released) + +# 2.19.0 (24-Apr-2025) + +WrongWrong (@k163377) +* #959: Add extensions for configOverride +* #954: Replace BooleanTriState with OptBoolean +* #944: Fixed to use common util for Member accessibility override +* #937: Added type match check to read functions + +Tatu Saloranta (@cowtowncoder) +* #889: Upgrade kotlin dep to 1.9.25 (from 1.9.24) + +WrongWrong (@k163377) +* #930: Add tests for #917 +* #929: Bug fixes to hasRequiredMarker and added isRequired considerations +* #914: Add test case to serialize Nothing? (for #314) +* #910: Add default KeyDeserializer for value class +* #885: Performance improvement of strictNullChecks +* #884: Changed the base class of MissingKotlinParameterException to InvalidNullException +* #878: Fix for #876 +* #868: Added test case for FAIL_ON_NULL_FOR_PRIMITIVES +* #866: Upgrade to JUnit5 +* #861: Update Kotlin to 1.9.24 +* #858: Refactor findDefaultCreator +* #839: Remove useKotlinPropertyNameForGetter and unify with kotlinPropertyNameAsImplicitName +* #835: Remove old SingletonSupport class and unified with KotlinFeature.SingletonSupport + # 2.18.4 (not yet released) WrongWrong (@k163377) @@ -31,6 +59,8 @@ WrongWrong (@k163377) # 2.18.0 (26-Sep-2024) WrongWrong (@k163377) +* #883: Raise the deprecation level to error for the MissingKotlinParameterException secondary constructor +* #869: Replaced Enum.values with Enum.entries * #818: Optimize the search process for creators * #817: Fixed nullability of convertValue function argument * #782: Organize deprecated contents diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 4b25e696f..a20868c41 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -16,6 +16,46 @@ Co-maintainers: === Releases === ------------------------------------------------------------------------ +2.19.1 (not yet released) + +2.19.0 (24-Apr-2025) + +#959: Extension functions has been added to simplify `configOverride` calls to `ObjectMapper` and `Module.SetupContext`. +#954: Replaced `OptBoolean` of internal caching with a common implementation. +#944: Common util is now used for member accessibility overrides. +#937: For `readValue` and other shorthands for `ObjectMapper` deserialization methods, + type consistency checks have been added. + A `RuntimeJsonMappingException` will be thrown in case of inconsistency. + This fixes a problem that broke `Kotlin` null safety by reading null as a value even if the type parameter was specified as non-null. + It also checks for custom errors in ObjectMapper that cause a different value to be read than the specified type parameter. +#929: Added consideration of `JsonProperty.isRequired` added in `2.19` in `hasRequiredMarker` processing. + Previously `JsonProperty.required` was defined as `Boolean` with default `false`, + so `KotlinModule` was forced to override it if the value was `false`. + This made it impossible for users to override the parsed result by `KotlinModule`. + The new `JsonProperty.isRequired` is defined with three values, including the default, + so `KotlinModule` can now respect user specifications. +#929: Fixed a problem with the `NullToEmptyCollection` and `NullToEmptyMap` options being applied to non-parameters + in the `hasRequiredMarker` process. + They currently do not work for setters or fields and are not related to serialization, + but were being incorrectly applied to their `required` decisions. +#910: A default `KeyDeserializer` for `value class` has been added. + This eliminates the need to have a custom `KeyDeserializer` for each `value class` when using it as a key in a `Map`, if only simple boxing is needed. +#889: Kotlin has been upgraded to 1.9.25. +#885: A new `StrictNullChecks` option(KotlinFeature.NewStrictNullChecks) has been added which greatly improves throughput. + Benchmarks show a consistent throughput drop of less than 2% when enabled (prior to the improvement, the worst throughput drop was more than 30%). + Note that the new backend changes the exception thrown to `InvalidNullException` and with it the error message. + Also note that the base class for `MissingKotlinParameterException` was changed to `InvalidNullException` in #884. +#884: The base class for `MissingKotlinParameterException` has been changed to `InvalidNullException`. + If you do not catch this exception or catch `MismatchedInputException`, the behavior is unchanged. + If you are catching both `MismatchedKotlinParameterException` and `InvalidNullException`, you must catch `MismatchedKotlinParameterException` first. +#883: The deprecation level has been raised to error for the `MissingKotlinParameterException` secondary constructor. + This is a problematic process that has been marked as deprecated for a very long time and will be removed in 2.20 or later. +#878: Fixed a problem where settings like `@JsonSetter(nulls = AS_EMPTY)` were not being applied when the input was `undefined`. +#869: By using Enum.entries in the acquisition of KotlinFeature.defaults, the initialization load was reduced, albeit slightly. +#858: Minor performance improvement of findDefaultCreator in edge cases. +#839: Remove useKotlinPropertyNameForGetter and unify with kotlinPropertyNameAsImplicitName. +#835: Remove old SingletonSupport class and unified with KotlinFeature.SingletonSupport. + 2.18.4 (06-May-2025) #923: Fixed a problem where the result of processing `hasRequiredMarker ` by a `KotlinModule` would also apply to diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Converters.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Converters.kt index e1416426d..c284fb8c3 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Converters.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Converters.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JavaType import com.fasterxml.jackson.databind.deser.std.StdDelegatingDeserializer import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer import com.fasterxml.jackson.databind.type.TypeFactory +import com.fasterxml.jackson.databind.util.ClassUtil import com.fasterxml.jackson.databind.util.StdConverter import kotlin.reflect.KClass import kotlin.time.toJavaDuration @@ -51,7 +52,7 @@ internal class ValueClassBoxConverter( val boxedClass: KClass ) : StdConverter() { private val boxMethod = boxedClass.java.getDeclaredMethod("box-impl", unboxedClass).apply { - if (!this.isAccessible) this.isAccessible = true + ClassUtil.checkAndFixAccess(this, false) } @Suppress("UNCHECKED_CAST") diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Exceptions.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Exceptions.kt index add73669b..120ad0f08 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Exceptions.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Exceptions.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.JsonMappingException -import com.fasterxml.jackson.databind.exc.MismatchedInputException +import com.fasterxml.jackson.databind.exc.InvalidNullException import java.io.Closeable import kotlin.reflect.KParameter @@ -31,8 +31,12 @@ class MissingKotlinParameterException( val parameter: KParameter, processor: JsonParser? = null, msg: String -) : MismatchedInputException(processor, msg) { - @Deprecated("Use main constructor", ReplaceWith("MissingKotlinParameterException(KParameter, JsonParser?, String)")) +) : InvalidNullException(processor, msg, null) { + @Deprecated( + "Use main constructor, ", + ReplaceWith("MissingKotlinParameterException(KParameter, JsonParser?, String)"), + DeprecationLevel.ERROR, + ) constructor( parameter: KParameter, processor: Closeable? = null, diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt index 2a8c3b1a2..dc75e7b26 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt @@ -7,8 +7,11 @@ import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.JsonSerializer import com.fasterxml.jackson.databind.MappingIterator +import com.fasterxml.jackson.databind.Module import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectReader +import com.fasterxml.jackson.databind.RuntimeJsonMappingException +import com.fasterxml.jackson.databind.cfg.MutableConfigOverride import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.databind.node.ArrayNode @@ -50,21 +53,130 @@ fun ObjectMapper.registerKotlinModule(initializer: KotlinModule.Builder.() -> Un inline fun jacksonTypeRef(): TypeReference = object: TypeReference() {} +@PublishedApi +internal inline fun Any?.checkTypeMismatch(): T { + // Basically, this check assumes that T is non-null and the value is null. + // Since this can be caused by both input or ObjectMapper implementation errors, + // a more abstract RuntimeJsonMappingException is thrown. + if (this !is T) { + val nullability = if (null is T) "?" else "(non-null)" + + // Since the databind implementation of MappingIterator throws RuntimeJsonMappingException, + // JsonMappingException was not used to unify the behavior. + throw RuntimeJsonMappingException( + "Deserialized value did not match the specified type; " + + "specified ${T::class.qualifiedName}${nullability} but was ${this?.let { it::class.qualifiedName }}" + ) + } + return this +} + +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ inline fun ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef()) -inline fun ObjectMapper.readValues(jp: JsonParser): MappingIterator = readValues(jp, jacksonTypeRef()) + .checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.readValues]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ +inline fun ObjectMapper.readValues(jp: JsonParser): MappingIterator { + val values = readValues(jp, jacksonTypeRef()) + + return object : MappingIterator(values) { + override fun nextValue(): T = super.nextValue().checkTypeMismatch() + } +} -inline fun ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef()) -inline fun ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef()) +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ +inline fun ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef()).checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ +inline fun ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef()).checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ inline fun ObjectMapper.readValue(content: String): T = readValue(content, jacksonTypeRef()) -inline fun ObjectMapper.readValue(src: Reader): T = readValue(src, jacksonTypeRef()) + .checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ +inline fun ObjectMapper.readValue(src: Reader): T = readValue(src, jacksonTypeRef()).checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ inline fun ObjectMapper.readValue(src: InputStream): T = readValue(src, jacksonTypeRef()) + .checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ inline fun ObjectMapper.readValue(src: ByteArray): T = readValue(src, jacksonTypeRef()) - + .checkTypeMismatch() + +/** + * Shorthand for [ObjectMapper.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ inline fun ObjectMapper.treeToValue(n: TreeNode): T = readValue(this.treeAsTokens(n), jacksonTypeRef()) + .checkTypeMismatch() +/** + * Shorthand for [ObjectMapper.convertValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectMapper]. + */ inline fun ObjectMapper.convertValue(from: Any?): T = convertValue(from, jacksonTypeRef()) - + .checkTypeMismatch() + +/** + * Shorthand for [ObjectReader.readValue]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectReader]. + */ inline fun ObjectReader.readValueTyped(jp: JsonParser): T = readValue(jp, jacksonTypeRef()) -inline fun ObjectReader.readValuesTyped(jp: JsonParser): Iterator = readValues(jp, jacksonTypeRef()) + .checkTypeMismatch() +/** + * Shorthand for [ObjectReader.readValues]. + * @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null. + * Other cases where the read value is of a different type than [T] + * due to an incorrect customization to [ObjectReader]. + */ +inline fun ObjectReader.readValuesTyped(jp: JsonParser): Iterator { + val values = readValues(jp, jacksonTypeRef()) + + return object : Iterator by values { + override fun next(): T = values.next().checkTypeMismatch() + } +} inline fun ObjectReader.treeToValue(n: TreeNode): T? = readValue(this.treeAsTokens(n), jacksonTypeRef()) inline fun ObjectMapper.addMixIn(): ObjectMapper = this.addMixIn(T::class.java, U::class.java) @@ -114,3 +226,7 @@ fun SimpleModule.addDeserializer(kClass: KClass, deserializer: Json kClass.javaPrimitiveType?.let { addDeserializer(it, deserializer) } addDeserializer(kClass.javaObjectType, deserializer) } + +inline fun ObjectMapper.configOverride(): MutableConfigOverride = configOverride(T::class.java) +inline fun Module.SetupContext.configOverride(): MutableConfigOverride = + configOverride(T::class.java) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt index 7efb65377..0054b3659 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt @@ -1,6 +1,7 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.OptBoolean import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.JsonSerializer import com.fasterxml.jackson.databind.Module @@ -36,20 +37,27 @@ internal class KotlinAnnotationIntrospector( // TODO: implement nullIsSameAsDefault flag, which represents when TRUE that if something has a default value, it can be passed a null to default it // this likely impacts this class to be accurate about what COULD be considered required + // If a new isRequired is explicitly specified or the old required is true, those values take precedence. + // In other cases, override is done by KotlinModule. + private fun JsonProperty.forceRequiredByAnnotation(): Boolean? = when { + isRequired != OptBoolean.DEFAULT -> isRequired.asBoolean() + required -> true + else -> null + } + + private fun AccessibleObject.forceRequiredByAnnotation(): Boolean? = + getAnnotation(JsonProperty::class.java)?.forceRequiredByAnnotation() + override fun hasRequiredMarker( m: AnnotatedMember ): Boolean? = m.takeIf { it.member.declaringClass.isKotlinClass() }?.let { _ -> cache.javaMemberIsRequired(m) { try { - when { - nullToEmptyCollection && m.type.isCollectionLikeType -> false - nullToEmptyMap && m.type.isMapLikeType -> false - else -> when (m) { - is AnnotatedField -> m.hasRequiredMarker() - is AnnotatedMethod -> m.hasRequiredMarker() - is AnnotatedParameter -> m.hasRequiredMarker() - else -> null - } + when (m) { + is AnnotatedField -> m.hasRequiredMarker() + is AnnotatedMethod -> m.hasRequiredMarker() + is AnnotatedParameter -> m.hasRequiredMarker() + else -> null } } catch (_: UnsupportedOperationException) { null @@ -100,28 +108,9 @@ internal class KotlinAnnotationIntrospector( } private fun AnnotatedField.hasRequiredMarker(): Boolean? { - val byAnnotation = (member as Field).isRequiredByAnnotation() - val byNullability = (member as Field).kotlinProperty?.returnType?.isRequired() - - return requiredAnnotationOrNullability(byAnnotation, byNullability) - } - - private fun AccessibleObject.isRequiredByAnnotation(): Boolean? = annotations - .firstOrNull { it.annotationClass == JsonProperty::class } - ?.let { it as JsonProperty } - ?.required - - private fun requiredAnnotationOrNullability(byAnnotation: Boolean?, byNullability: Boolean?): Boolean? { - if (byAnnotation != null && byNullability != null) { - return byAnnotation || byNullability - } else if (byNullability != null) { - return byNullability - } - return byAnnotation - } - - private fun Method.isRequiredByAnnotation(): Boolean? { - return (this.annotations.firstOrNull { it.annotationClass.java == JsonProperty::class.java } as? JsonProperty)?.required + val field = member as Field + return field.forceRequiredByAnnotation() + ?: field.kotlinProperty?.returnType?.isRequired() } // Since Kotlin's property has the same Type for each field, getter, and setter, @@ -136,9 +125,7 @@ internal class KotlinAnnotationIntrospector( private fun AnnotatedMethod.getRequiredMarkerFromCorrespondingAccessor(): Boolean? { member.declaringClass.kotlin.declaredMemberProperties.forEach { kProperty -> if (kProperty.javaGetter == this.member || (kProperty as? KMutableProperty1)?.javaSetter == this.member) { - val byAnnotation = this.member.isRequiredByAnnotation() - val byNullability = kProperty.isRequiredByNullability() - return requiredAnnotationOrNullability(byAnnotation, byNullability) + return member.forceRequiredByAnnotation() ?: kProperty.isRequiredByNullability() } } return null @@ -146,10 +133,11 @@ internal class KotlinAnnotationIntrospector( // Is the member method a regular method of the data class or private fun Method.getRequiredMarkerFromAccessorLikeMethod(): Boolean? = cache.kotlinFromJava(this)?.let { func -> - val byAnnotation = this.isRequiredByAnnotation() - return when { - func.isGetterLike() -> requiredAnnotationOrNullability(byAnnotation, func.returnType.isRequired()) - func.isSetterLike() -> requiredAnnotationOrNullability(byAnnotation, func.valueParameters[0].isRequired()) + forceRequiredByAnnotation() ?: when { + func.isGetterLike() -> func.returnType.isRequired() + // If nullToEmpty could be supported for setters, + // a branch similar to AnnotatedParameter.hasRequiredMarker should be added. + func.isSetterLike() -> func.valueParameters[0].isRequired() else -> null } } @@ -157,12 +145,15 @@ internal class KotlinAnnotationIntrospector( private fun KFunction<*>.isGetterLike(): Boolean = parameters.size == 1 private fun KFunction<*>.isSetterLike(): Boolean = parameters.size == 2 && returnType == UNIT_TYPE - private fun AnnotatedParameter.hasRequiredMarker(): Boolean? { - val byAnnotation = this.getAnnotation(JsonProperty::class.java)?.required - val byNullability = cache.findKotlinParameter(this)?.isRequired() - - return requiredAnnotationOrNullability(byAnnotation, byNullability) - } + private fun AnnotatedParameter.hasRequiredMarker(): Boolean? = getAnnotation(JsonProperty::class.java) + ?.forceRequiredByAnnotation() + ?: run { + when { + nullToEmptyCollection && type.isCollectionLikeType -> false + nullToEmptyMap && type.isMapLikeType -> false + else -> cache.findKotlinParameter(this)?.isRequired() + } + } private fun AnnotatedMethod.findValueClassReturnType() = cache.findValueClassReturnType(this) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt index 4d8057ee6..09dd7e022 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinDeserializers.kt @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.deser.Deserializers import com.fasterxml.jackson.databind.deser.std.StdDeserializer import com.fasterxml.jackson.databind.exc.InvalidDefinitionException +import com.fasterxml.jackson.databind.util.ClassUtil import java.lang.reflect.Method import java.lang.reflect.Modifier import kotlin.reflect.full.primaryConstructor @@ -106,7 +107,7 @@ internal class WrapsNullableValueClassBoxDeserializer( private val inputType: Class<*> = creator.parameterTypes[0] init { - creator.apply { if (!this.isAccessible) this.isAccessible = true } + ClassUtil.checkAndFixAccess(creator, false) } // Cache the result of wrapping null, since the result is always expected to be the same. diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinFeature.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinFeature.kt index f8da63d4a..e7e12d5ba 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinFeature.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinFeature.kt @@ -1,5 +1,7 @@ package com.fasterxml.jackson.module.kotlin +import com.fasterxml.jackson.annotation.JsonSetter +import com.fasterxml.jackson.databind.exc.InvalidNullException import java.util.BitSet /** @@ -30,7 +32,6 @@ enum class KotlinFeature(internal val enabledByDefault: Boolean) { * Deserializing a singleton overwrites the value of the single instance. * * See [jackson-module-kotlin#225]: keep Kotlin singletons as singletons. - * @see com.fasterxml.jackson.module.kotlin.SingletonSupport */ SingletonSupport(enabledByDefault = false), @@ -41,6 +42,11 @@ enum class KotlinFeature(internal val enabledByDefault: Boolean) { * may contain null values after deserialization. * Enabling it protects against this but has significant performance impact. */ + @Deprecated( + level = DeprecationLevel.WARNING, + message = "This option will be migrated to the new backend in 2.21.", + replaceWith = ReplaceWith("NewStrictNullChecks") + ) StrictNullChecks(enabledByDefault = false), /** @@ -67,13 +73,29 @@ enum class KotlinFeature(internal val enabledByDefault: Boolean) { * `@JsonFormat` annotations need to be declared either on getter using `@get:JsonFormat` or field using `@field:JsonFormat`. * See [jackson-module-kotlin#651] for details. */ - UseJavaDurationConversion(enabledByDefault = false); + UseJavaDurationConversion(enabledByDefault = false), + + /** + * New [StrictNullChecks] feature with improved throughput. + * Internally, it will be the same as if [JsonSetter] (contentNulls = FAIL) had been granted. + * Benchmarks show that it can check for illegal nulls with throughput nearly identical to the default (see [jackson-module-kotlin#719]). + * + * Note that in the new backend, the exception thrown has changed from [MissingKotlinParameterException] to [InvalidNullException]. + * The message will be changed accordingly. + * Since 2.19, the base class of [MissingKotlinParameterException] has also been changed to [InvalidNullException], + * so be careful when catching it. + * + * This is a temporary option for a phased backend migration, + * which will eventually be merged into [StrictNullChecks]. + * Also, specifying both this and [StrictNullChecks] is not permitted. + */ + NewStrictNullChecks(enabledByDefault = false); internal val bitSet: BitSet = (1 shl ordinal).toBitSet() companion object { internal val defaults - get() = values().fold(BitSet(Int.SIZE_BITS)) { acc, cur -> + get() = entries.fold(BitSet(Int.SIZE_BITS)) { acc, cur -> acc.apply { if (cur.enabledByDefault) this.or(cur.bitSet) } } } diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt index 70e2e35a6..8658747a8 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinKeyDeserializers.kt @@ -5,6 +5,12 @@ import com.fasterxml.jackson.core.exc.InputCoercionException import com.fasterxml.jackson.databind.* import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializer import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializers +import com.fasterxml.jackson.databind.exc.InvalidDefinitionException +import com.fasterxml.jackson.databind.util.ClassUtil +import java.lang.reflect.Method +import kotlin.reflect.KClass +import kotlin.reflect.full.primaryConstructor +import kotlin.reflect.jvm.javaMethod // The reason why key is treated as nullable is to match the tentative behavior of StdKeyDeserializer. // If StdKeyDeserializer is modified, need to modify this too. @@ -65,18 +71,68 @@ internal object ULongKeyDeserializer : StdKeyDeserializer(TYPE_LONG, ULong::clas } } -internal object KotlinKeyDeserializers : StdKeyDeserializers() { - private fun readResolve(): Any = KotlinKeyDeserializers +// The implementation is designed to be compatible with various creators, just in case. +internal class ValueClassKeyDeserializer( + private val creator: Method, + private val converter: ValueClassBoxConverter +) : KeyDeserializer() { + private val unboxedClass: Class<*> = creator.parameterTypes[0] + init { + ClassUtil.checkAndFixAccess(creator, false) + } + + // Based on databind error + // https://github.com/FasterXML/jackson-databind/blob/341f8d360a5f10b5e609d6ee0ea023bf597ce98a/src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java#L624 + private fun errorMessage(boxedType: JavaType): String = + "Could not find (Map) Key deserializer for types wrapped in $boxedType" + + override fun deserializeKey(key: String?, ctxt: DeserializationContext): Any { + val unboxedJavaType = ctxt.constructType(unboxedClass) + + return try { + // findKeyDeserializer does not return null, and an exception will be thrown if not found. + val value = ctxt.findKeyDeserializer(unboxedJavaType, null).deserializeKey(key, ctxt) + @Suppress("UNCHECKED_CAST") + converter.convert(creator.invoke(null, value) as S) + } catch (e: InvalidDefinitionException) { + throw JsonMappingException.from(ctxt, errorMessage(ctxt.constructType(converter.boxedClass.java)), e) + } + } + + companion object { + fun createOrNull( + boxedClass: KClass<*>, + cache: ReflectionCache + ): ValueClassKeyDeserializer<*, *>? { + // primaryConstructor.javaMethod for the value class returns constructor-impl + // Only primary constructor is allowed as creator, regardless of visibility. + // This is because it is based on the WrapsNullableValueClassBoxDeserializer. + // Also, as far as I could research, there was no such functionality as JsonKeyCreator, + // so it was not taken into account. + val creator = boxedClass.primaryConstructor?.javaMethod ?: return null + val converter = cache.getValueClassBoxConverter(creator.returnType, boxedClass) + + return ValueClassKeyDeserializer(creator, converter) + } + } +} + +internal class KotlinKeyDeserializers(private val cache: ReflectionCache) : StdKeyDeserializers() { override fun findKeyDeserializer( type: JavaType, config: DeserializationConfig?, beanDesc: BeanDescription? - ): KeyDeserializer? = when (type.rawClass) { - UByte::class.java -> UByteKeyDeserializer - UShort::class.java -> UShortKeyDeserializer - UInt::class.java -> UIntKeyDeserializer - ULong::class.java -> ULongKeyDeserializer - else -> null + ): KeyDeserializer? { + val rawClass = type.rawClass + + return when { + rawClass == UByte::class.java -> UByteKeyDeserializer + rawClass == UShort::class.java -> UShortKeyDeserializer + rawClass == UInt::class.java -> UIntKeyDeserializer + rawClass == ULong::class.java -> ULongKeyDeserializer + rawClass.isUnboxableValueClass() -> ValueClassKeyDeserializer.createOrNull(rawClass.kotlin, cache) + else -> null + } } } diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt index 0af625ad4..f09b1707a 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModule.kt @@ -5,7 +5,9 @@ import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyCollection import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyMap +import com.fasterxml.jackson.module.kotlin.KotlinFeature.SingletonSupport import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks +import com.fasterxml.jackson.module.kotlin.KotlinFeature.NewStrictNullChecks import com.fasterxml.jackson.module.kotlin.KotlinFeature.KotlinPropertyNameAsImplicitName import com.fasterxml.jackson.module.kotlin.KotlinFeature.UseJavaDurationConversion import java.util.* @@ -22,8 +24,8 @@ fun Class<*>.isKotlinClass(): Boolean = this.isAnnotationPresent(Metadata::class * map object. * @property nullIsSameAsDefault Default false. Whether to treat null values as absent when deserializing, thereby * using the default value provided in Kotlin. - * @property singletonSupport Default: DISABLED. Mode for singleton handling. - * See {@link com.fasterxml.jackson.module.kotlin.SingletonSupport label} + * @property singletonSupport Default: false. Mode for singleton handling. + * See [KotlinFeature.SingletonSupport] * @property enabledSingletonSupport Default: false. A temporary property that is maintained until the return value of `singletonSupport` is changed. * It will be removed in 2.21. * @property strictNullChecks Default: false. Whether to check deserialized collections. With this disabled, @@ -40,31 +42,46 @@ class KotlinModule private constructor( val nullToEmptyCollection: Boolean = NullToEmptyCollection.enabledByDefault, val nullToEmptyMap: Boolean = NullToEmptyMap.enabledByDefault, val nullIsSameAsDefault: Boolean = NullIsSameAsDefault.enabledByDefault, - @property:Deprecated( - level = DeprecationLevel.ERROR, - message = "The return value will be Boolean in 2.19. Until then, use enabledSingletonSupport.", - replaceWith = ReplaceWith("enabledSingletonSupport") - ) - @Suppress("DEPRECATION_ERROR") - val singletonSupport: SingletonSupport = SingletonSupport.DISABLED, - val strictNullChecks: Boolean = StrictNullChecks.enabledByDefault, - @Deprecated( - level = DeprecationLevel.ERROR, - message = "There was a discrepancy between the property name and the Feature name." + - " To migrate to the correct property name, it will be ERROR in 2.18 and removed in 2.19.", - replaceWith = ReplaceWith("kotlinPropertyNameAsImplicitName") - ) - val useKotlinPropertyNameForGetter: Boolean = KotlinPropertyNameAsImplicitName.enabledByDefault, + val singletonSupport: Boolean = SingletonSupport.enabledByDefault, + strictNullChecks: Boolean = StrictNullChecks.enabledByDefault, + val kotlinPropertyNameAsImplicitName: Boolean = KotlinPropertyNameAsImplicitName.enabledByDefault, val useJavaDurationConversion: Boolean = UseJavaDurationConversion.enabledByDefault, + private val newStrictNullChecks: Boolean = NewStrictNullChecks.enabledByDefault, ) : SimpleModule(KotlinModule::class.java.name, PackageVersion.VERSION) { - @Suppress("DEPRECATION_ERROR") - val kotlinPropertyNameAsImplicitName: Boolean get() = useKotlinPropertyNameForGetter - @Suppress("DEPRECATION_ERROR") - val enabledSingletonSupport: Boolean get() = singletonSupport == SingletonSupport.CANONICALIZE + /* + * Prior to 2.18, an older Enum called SingletonSupport was used to manage feature. + * To deprecate it and replace it with singletonSupport: Boolean, the following steps are in progress. + * + * 1. add enabledSingletonSupport: Boolean property + * 2. delete SingletonSupport class and change the property to singletonSupport: Boolean + * 3. remove the enabledSingletonSupport property + * + * Now that 2 is complete, deprecation is in progress for 3. + */ + @Deprecated( + level = DeprecationLevel.WARNING, + message = "This property is scheduled to be removed in 2.21 or later" + + " in order to unify the use of KotlinFeature.", + replaceWith = ReplaceWith("singletonSupport") + ) + val enabledSingletonSupport: Boolean get() = singletonSupport + + private val oldStrictNullChecks: Boolean = strictNullChecks + + // To reduce the amount of destructive changes, no properties will be added to the public. + val strictNullChecks: Boolean = if (strictNullChecks) { + if (newStrictNullChecks) { + throw IllegalArgumentException("Enabling both StrictNullChecks and NewStrictNullChecks is not permitted.") + } + + true + } else { + newStrictNullChecks + } companion object { // Increment when option is added - private const val serialVersionUID = 2L + private const val serialVersionUID = 3L } @Deprecated( @@ -78,14 +95,11 @@ class KotlinModule private constructor( builder.isEnabled(NullToEmptyCollection), builder.isEnabled(NullToEmptyMap), builder.isEnabled(NullIsSameAsDefault), - @Suppress("DEPRECATION_ERROR") - when { - builder.isEnabled(KotlinFeature.SingletonSupport) -> SingletonSupport.CANONICALIZE - else -> SingletonSupport.DISABLED - }, + builder.isEnabled(SingletonSupport), builder.isEnabled(StrictNullChecks), builder.isEnabled(KotlinPropertyNameAsImplicitName), builder.isEnabled(UseJavaDurationConversion), + builder.isEnabled(NewStrictNullChecks), ) override fun setupModule(context: SetupContext) { @@ -97,9 +111,9 @@ class KotlinModule private constructor( val cache = ReflectionCache(reflectionCacheSize) - context.addValueInstantiators(KotlinInstantiators(cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault, strictNullChecks)) + context.addValueInstantiators(KotlinInstantiators(cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault, oldStrictNullChecks)) - if (enabledSingletonSupport) { + if (singletonSupport) { context.addBeanDeserializerModifier(KotlinBeanDeserializerModifier) } @@ -111,10 +125,12 @@ class KotlinModule private constructor( nullIsSameAsDefault, useJavaDurationConversion )) - context.appendAnnotationIntrospector(KotlinNamesAnnotationIntrospector(cache, kotlinPropertyNameAsImplicitName)) + context.appendAnnotationIntrospector( + KotlinNamesAnnotationIntrospector(cache, newStrictNullChecks, kotlinPropertyNameAsImplicitName) + ) context.addDeserializers(KotlinDeserializers(cache, useJavaDurationConversion)) - context.addKeyDeserializers(KotlinKeyDeserializers) + context.addKeyDeserializers(KotlinKeyDeserializers(cache)) context.addSerializers(KotlinSerializers()) context.addKeySerializers(KotlinKeySerializers()) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt index da4ea6849..b082ad855 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt @@ -1,6 +1,8 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.JsonSetter +import com.fasterxml.jackson.annotation.Nulls import com.fasterxml.jackson.databind.JavaType import com.fasterxml.jackson.databind.cfg.MapperConfig import com.fasterxml.jackson.databind.introspect.Annotated @@ -12,8 +14,10 @@ import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector import com.fasterxml.jackson.databind.introspect.PotentialCreator import java.lang.reflect.Constructor import java.util.Locale +import kotlin.collections.getOrNull import kotlin.reflect.KClass import kotlin.reflect.KFunction +import kotlin.reflect.KParameter import kotlin.reflect.full.hasAnnotation import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor @@ -22,7 +26,8 @@ import kotlin.reflect.jvm.javaType internal class KotlinNamesAnnotationIntrospector( private val cache: ReflectionCache, - private val useKotlinPropertyNameForGetter: Boolean + private val strictNullChecks: Boolean, + private val kotlinPropertyNameAsImplicitName: Boolean ) : NopAnnotationIntrospector() { private fun getterNameFromJava(member: AnnotatedMethod): String? { val name = member.name @@ -61,7 +66,7 @@ internal class KotlinNamesAnnotationIntrospector( return when (member) { is AnnotatedMethod -> if (member.parameterCount == 0) { - if (useKotlinPropertyNameForGetter) { + if (kotlinPropertyNameAsImplicitName) { // Fall back to default if it is a getter-like function getterNameFromKotlin(member) ?: getterNameFromJava(member) } else getterNameFromJava(member) @@ -72,16 +77,26 @@ internal class KotlinNamesAnnotationIntrospector( } override fun refineDeserializationType(config: MapperConfig<*>, a: Annotated, baseType: JavaType): JavaType = - (a as? AnnotatedParameter)?.let { _ -> - cache.findKotlinParameter(a)?.let { param -> - val rawType = a.rawType - (param.type.classifier as? KClass<*>) - ?.java - ?.takeIf { it.isUnboxableValueClass() && it != rawType } - ?.let { config.constructType(it) } - } + findKotlinParameter(a)?.let { param -> + val rawType = a.rawType + (param.type.classifier as? KClass<*>) + ?.java + ?.takeIf { it.isUnboxableValueClass() && it != rawType } + ?.let { config.constructType(it) } } ?: baseType + override fun findSetterInfo(ann: Annotated): JsonSetter.Value = ann.takeIf { strictNullChecks } + ?.let { _ -> + findKotlinParameter(ann)?.let { param -> + if (param.requireStrictNullCheck(ann.type)) { + JsonSetter.Value.forContentNulls(Nulls.FAIL) + } else { + null + } + } + } + ?: super.findSetterInfo(ann) + override fun findDefaultCreator( config: MapperConfig<*>, valueClass: AnnotatedClass, @@ -90,13 +105,11 @@ internal class KotlinNamesAnnotationIntrospector( ): PotentialCreator? { val kClass = valueClass.creatableKotlinClass() ?: return null - val propertyNames = kClass.memberProperties.map { it.name }.toSet() - - val defaultCreator = kClass.let { _ -> - // By default, the primary constructor or the only publicly available constructor may be used - val ctor = kClass.primaryConstructor ?: kClass.constructors.takeIf { it.size == 1 }?.single() - ctor?.takeIf { it.isPossibleCreator(propertyNames) } - } + val defaultCreator = kClass.primarilyConstructor() + ?.takeIf { ctor -> + val propertyNames = kClass.memberProperties.map { it.name }.toSet() + ctor.isPossibleCreator(propertyNames) + } ?: return null return declaredConstructors.find { @@ -107,13 +120,26 @@ internal class KotlinNamesAnnotationIntrospector( } private fun findKotlinParameterName(param: AnnotatedParameter): String? = cache.findKotlinParameter(param)?.name + + private fun findKotlinParameter(param: Annotated) = (param as? AnnotatedParameter) + ?.let { cache.findKotlinParameter(it) } } +private fun KParameter.markedNonNullAt(index: Int) = type.arguments.getOrNull(index)?.type?.isMarkedNullable == false + +private fun KParameter.requireStrictNullCheck(type: JavaType): Boolean = + ((type.isArrayType || type.isCollectionLikeType) && this.markedNonNullAt(0)) || + (type.isMapLikeType && this.markedNonNullAt(1)) + + // If it is not a Kotlin class or an Enum, Creator is not used private fun AnnotatedClass.creatableKotlinClass(): KClass<*>? = annotated .takeIf { it.isKotlinClass() && !it.isEnum } ?.kotlin +// By default, the primary constructor or the only publicly available constructor may be used +private fun KClass<*>.primarilyConstructor() = primaryConstructor ?: constructors.singleOrNull() + private fun KFunction<*>.isPossibleCreator(propertyNames: Set): Boolean = 0 < parameters.size && !isPossibleSingleString(propertyNames) && parameters.none { it.name == null } diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt index 57923a03c..320eb6cfb 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -78,8 +78,8 @@ internal class KotlinValueInstantiator( paramType.isMarkedNullable -> null // Primitive types always try to get from a buffer, considering several settings jsonProp.type.isPrimitive -> buffer.getParameter(jsonProp) - // to get suitable "missing" value provided by deserializer - else -> valueDeserializer?.getAbsentValue(ctxt) + // to get suitable "missing" value provided by nullValueProvider + else -> jsonProp.nullValueProvider?.getAbsentValue(ctxt) } } diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/MethodValueCreator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/MethodValueCreator.kt index ed7f68ecf..b6c8dafb9 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/MethodValueCreator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/MethodValueCreator.kt @@ -1,5 +1,6 @@ package com.fasterxml.jackson.module.kotlin +import com.fasterxml.jackson.databind.util.ClassUtil import kotlin.reflect.KFunction import kotlin.reflect.full.extensionReceiverParameter import kotlin.reflect.full.instanceParameter @@ -39,7 +40,7 @@ internal class MethodValueCreator private constructor( possibleCompanion.java.enclosingClass.declaredFields .firstOrNull { it.type.kotlin.isCompanion } ?.let { - it.isAccessible = true + ClassUtil.checkAndFixAccess(it, false) // If the instance of the companion object cannot be obtained, accessibility will always be false it.get(null) to false diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt index b13ae4918..3960deb5e 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt @@ -1,5 +1,6 @@ package com.fasterxml.jackson.module.kotlin +import com.fasterxml.jackson.annotation.OptBoolean import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor import com.fasterxml.jackson.databind.introspect.AnnotatedMember import com.fasterxml.jackson.databind.introspect.AnnotatedMethod @@ -22,32 +23,12 @@ import kotlin.reflect.jvm.kotlinFunction internal class ReflectionCache(reflectionCacheSize: Int) : Serializable { companion object { // Increment is required when properties that use LRUMap are changed. - private const val serialVersionUID = 3L - } - - sealed class BooleanTriState(val value: Boolean?) { - class True : BooleanTriState(true) - class False : BooleanTriState(false) - class Empty : BooleanTriState(null) - - companion object { - private val TRUE = True() - private val FALSE = False() - private val EMPTY = Empty() - - fun fromBoolean(value: Boolean?): BooleanTriState { - return when (value) { - null -> EMPTY - true -> TRUE - false -> FALSE - } - } - } + private const val serialVersionUID = 4L } private val javaExecutableToKotlin = LRUMap>(reflectionCacheSize, reflectionCacheSize) private val javaExecutableToValueCreator = LRUMap>(reflectionCacheSize, reflectionCacheSize) - private val javaMemberIsRequired = LRUMap(reflectionCacheSize, reflectionCacheSize) + private val javaMemberIsRequired = LRUMap(reflectionCacheSize, reflectionCacheSize) // Initial size is 0 because the value class is not always used private val valueClassReturnTypeCache: LRUMap>> = @@ -102,8 +83,8 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable { ) } // we cannot reflect this method so do the default Java-ish behavior - fun javaMemberIsRequired(key: AnnotatedMember, calc: (AnnotatedMember) -> Boolean?): Boolean? = javaMemberIsRequired.get(key)?.value - ?: calc(key).let { javaMemberIsRequired.putIfAbsent(key, BooleanTriState.fromBoolean(it))?.value ?: it } + fun javaMemberIsRequired(key: AnnotatedMember, calc: (AnnotatedMember) -> Boolean?): Boolean? = javaMemberIsRequired.get(key)?.asBoolean() + ?: calc(key).let { javaMemberIsRequired.putIfAbsent(key, OptBoolean.fromBoolean(it))?.asBoolean() ?: it } private fun AnnotatedMethod.getValueClassReturnType(): KClass<*>? { val getter = this.member.apply { diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/SingletonSupport.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/SingletonSupport.kt deleted file mode 100644 index 7e2d83d9b..000000000 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/SingletonSupport.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.fasterxml.jackson.module.kotlin - -/** - * Special handling for singletons. - */ -@Deprecated( - level = DeprecationLevel.ERROR, - message = "It will be removed in 2.19 to unify with KotlinFeature.", - replaceWith = ReplaceWith("KotlinFeature.SingletonSupport") -) -enum class SingletonSupport { - // No special handling of singletons (pre-2.10 behavior) - // Each time a Singleton object is deserialized a new instance is created. - DISABLED, - // Deserialize then canonicalize (was the default in 2.10) - // Deserializing a singleton overwrites the value of the single instance. - // [jackson-module-kotlin#225]: keep Kotlin singletons as singletons - CANONICALIZE -} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucketTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucketTest.kt index 20155690f..f011a5ea1 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucketTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucketTest.kt @@ -1,24 +1,28 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.annotation.JsonCreator -import org.junit.Ignore -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith +import org.junit.jupiter.api.Nested import kotlin.reflect.KFunction import kotlin.reflect.full.functions import kotlin.reflect.full.hasAnnotation -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue - -@RunWith(Enclosed::class) class ArgumentBucketTest { - class Normal { - @Ignore - data class Constructor(val foo: String, val bar: String) + data class Constructor(val foo: String, val bar: String) + + data class Method(val foo: String, val bar: String) { + companion object { + @JvmStatic + @JsonCreator + fun of(foo: String, bar: String): Method = Method(foo, bar) + } + } + @Nested + inner class Normal { @Test fun constructorTest() { val function: KFunction<*> = ::Constructor @@ -45,15 +49,6 @@ class ArgumentBucketTest { assertEquals("bar", bucket[params[1]]) } - @Ignore - data class Method(val foo: String, val bar: String) { - companion object { - @JvmStatic - @JsonCreator - fun of(foo: String, bar: String): Method = Method(foo, bar) - } - } - @Test fun methodTest() { val function: KFunction<*> = Method.Companion::class.functions.first { it.hasAnnotation() } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/DslTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/DslTest.kt index e1e00ba22..d448a3eff 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/DslTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/DslTest.kt @@ -6,9 +6,9 @@ import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyCollection import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyMap import com.fasterxml.jackson.module.kotlin.KotlinFeature.SingletonSupport -import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks -import org.junit.Assert.assertNotNull -import org.junit.Test +import com.fasterxml.jackson.module.kotlin.KotlinFeature.NewStrictNullChecks +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -35,7 +35,7 @@ class DslTest { enable(NullToEmptyMap) enable(NullIsSameAsDefault) enable(SingletonSupport) - enable(StrictNullChecks) + enable(NewStrictNullChecks) } assertNotNull(module) @@ -43,7 +43,7 @@ class DslTest { assertTrue(module.nullToEmptyCollection) assertTrue(module.nullToEmptyMap) assertTrue(module.nullIsSameAsDefault) - assertTrue(module.enabledSingletonSupport) + assertTrue(module.singletonSupport) assertTrue(module.strictNullChecks) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/JDKSerializabilityTestHelper.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/JDKSerializabilityTestHelper.kt index a5af1dbdf..0c6493218 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/JDKSerializabilityTestHelper.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/JDKSerializabilityTestHelper.kt @@ -1,6 +1,6 @@ package com.fasterxml.jackson.module.kotlin -import junit.framework.TestCase +import org.junit.jupiter.api.Assertions.fail import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.ObjectInputStream @@ -14,14 +14,13 @@ fun jdkSerialize(o: Any): ByteArray { return bytes.toByteArray() } -fun jdkDeserialize(raw: ByteArray): T? { +fun jdkDeserialize(raw: ByteArray): T { val objIn = ObjectInputStream(ByteArrayInputStream(raw)) return try { @Suppress("UNCHECKED_CAST") objIn.readObject() as T } catch (e: ClassNotFoundException) { - TestCase.fail("Missing class: " + e.message) - null + fail("Missing class: " + e.message) } finally { objIn.close() } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt index dad0fc60b..628f5afc8 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinInstantiatorsTest.kt @@ -1,8 +1,8 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator -import org.junit.Assert.* -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test class KotlinInstantiatorsTest { private val mapper = jacksonObjectMapper() diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt index 5b8fd0aa6..5a7e7de79 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/KotlinModuleTest.kt @@ -2,13 +2,28 @@ package com.fasterxml.jackson.module.kotlin import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature.* -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import kotlin.test.assertNotNull class KotlinModuleTest { + // After the final migration is complete, this test will be removed. + @Test + fun strictNullChecksTests() { + assertTrue(kotlinModule { enable(StrictNullChecks) }.strictNullChecks) + assertTrue(kotlinModule { enable(NewStrictNullChecks) }.strictNullChecks) + + assertThrows { + kotlinModule { + enable(StrictNullChecks) + enable(NewStrictNullChecks) + } + } + } + @Test fun builder_Defaults() { val module = KotlinModule.Builder().build() @@ -17,7 +32,7 @@ class KotlinModuleTest { assertFalse(module.nullToEmptyCollection) assertFalse(module.nullToEmptyMap) assertFalse(module.nullIsSameAsDefault) - assertFalse(module.enabledSingletonSupport) + assertFalse(module.singletonSupport) assertFalse(module.strictNullChecks) assertFalse(module.kotlinPropertyNameAsImplicitName) assertFalse(module.useJavaDurationConversion) @@ -31,7 +46,7 @@ class KotlinModuleTest { enable(NullToEmptyMap) enable(NullIsSameAsDefault) enable(SingletonSupport) - enable(StrictNullChecks) + enable(NewStrictNullChecks) enable(KotlinPropertyNameAsImplicitName) enable(UseJavaDurationConversion) }.build() @@ -40,7 +55,7 @@ class KotlinModuleTest { assertTrue(module.nullToEmptyCollection) assertTrue(module.nullToEmptyMap) assertTrue(module.nullIsSameAsDefault) - assertTrue(module.enabledSingletonSupport) + assertTrue(module.singletonSupport) assertTrue(module.strictNullChecks) assertTrue(module.kotlinPropertyNameAsImplicitName) assertTrue(module.useJavaDurationConversion) @@ -79,13 +94,13 @@ class KotlinModuleTest { enable(SingletonSupport) }.build() - assertTrue(module.enabledSingletonSupport) + assertTrue(module.singletonSupport) } @Test fun builder_EnableStrictNullChecks() { val module = KotlinModule.Builder().apply { - enable(StrictNullChecks) + enable(NewStrictNullChecks) }.build() assertTrue(module.strictNullChecks) @@ -99,7 +114,7 @@ class KotlinModuleTest { enable(NullToEmptyMap) enable(NullIsSameAsDefault) enable(SingletonSupport) - enable(StrictNullChecks) + enable(NewStrictNullChecks) }.build() val serialized = jdkSerialize(module) @@ -110,7 +125,7 @@ class KotlinModuleTest { assertTrue(deserialized.nullToEmptyCollection) assertTrue(deserialized.nullToEmptyMap) assertTrue(deserialized.nullIsSameAsDefault) - assertTrue(deserialized.enabledSingletonSupport) + assertTrue(deserialized.singletonSupport) assertTrue(deserialized.strictNullChecks) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/MissingKotlinParameterExceptionTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/MissingKotlinParameterExceptionTest.kt index 2a15fed5b..713cb6614 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/MissingKotlinParameterExceptionTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/MissingKotlinParameterExceptionTest.kt @@ -1,6 +1,6 @@ package com.fasterxml.jackson.module.kotlin -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertNotNull import kotlin.test.assertNull diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValueTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValueTest.kt new file mode 100644 index 000000000..66e524c25 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValueTest.kt @@ -0,0 +1,89 @@ +package com.fasterxml.jackson.module.kotlin + +import com.fasterxml.jackson.databind.RuntimeJsonMappingException +import com.fasterxml.jackson.databind.node.NullNode +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import java.io.StringReader + +class ReadValueTest { + @Nested + inner class CheckTypeMismatchTest { + @Test + fun jsonParser() { + val src = defaultMapper.createParser("null") + assertThrows { + defaultMapper.readValue(src) + } + } + + @Test + fun file() { + val src = createTempJson("null") + assertThrows { + defaultMapper.readValue(src) + } + } + + // Not implemented because a way to test without mocks was not found + // @Test + // fun url() { + // } + + @Test + fun string() { + val src = "null" + assertThrows { + defaultMapper.readValue(src) + } + } + + @Test + fun reader() { + val src = StringReader("null") + assertThrows { + defaultMapper.readValue(src) + } + } + + @Test + fun inputStream() { + val src = "null".byteInputStream() + assertThrows { + defaultMapper.readValue(src) + } + } + + @Test + fun byteArray() { + val src = "null".toByteArray() + assertThrows { + defaultMapper.readValue(src) + } + } + + @Test + fun treeToValueTreeNode() { + assertThrows { + defaultMapper.treeToValue(NullNode.instance) + } + } + + @Test + fun convertValueAny() { + assertThrows { + defaultMapper.convertValue(null) + } + } + + @Test + fun readValueTypedJsonParser() { + val reader = defaultMapper.reader() + val src = reader.createParser("null") + assertThrows { + reader.readValueTyped(src) + } + } + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValuesTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValuesTest.kt new file mode 100644 index 000000000..2d2aa10b5 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReadValuesTest.kt @@ -0,0 +1,65 @@ +package com.fasterxml.jackson.module.kotlin + +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.RuntimeJsonMappingException +import com.fasterxml.jackson.databind.deser.std.StdDeserializer +import com.fasterxml.jackson.databind.module.SimpleModule +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import kotlin.test.assertEquals + +class ReadValuesTest { + class MyStrDeser : StdDeserializer(String::class.java) { + override fun deserialize( + p: JsonParser, + ctxt: DeserializationContext + ): String? = p.valueAsString.takeIf { it != "bar" } + } + + @Nested + inner class CheckTypeMismatchTest { + val mapper = jacksonObjectMapper().registerModule( + object : SimpleModule() { + init { + addDeserializer(String::class.java, MyStrDeser()) + } + } + )!! + + @Test + fun readValuesJsonParserNext() { + val src = mapper.createParser(""""foo"${"\n"}"bar"""") + val itr = mapper.readValues(src) + + assertEquals("foo", itr.next()) + assertThrows { + itr.next() + } + } + + @Test + fun readValuesJsonParserNextValue() { + val src = mapper.createParser(""""foo"${"\n"}"bar"""") + val itr = mapper.readValues(src) + + assertEquals("foo", itr.nextValue()) + assertThrows { + itr.nextValue() + } + } + + @Test + fun readValuesTypedJsonParser() { + val reader = mapper.reader() + val src = reader.createParser(""""foo"${"\n"}"bar"""") + val itr = reader.readValuesTyped(src) + + assertEquals("foo", itr.next()) + assertThrows { + itr.next() + } + } + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCacheTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCacheTest.kt index 7914a402a..1a03a923b 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCacheTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCacheTest.kt @@ -1,6 +1,6 @@ package com.fasterxml.jackson.module.kotlin -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertNotNull class ReflectionCacheTest { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/TestCommons.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/TestCommons.kt index 3bd3e5bd8..327fc3a55 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/TestCommons.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/TestCommons.kt @@ -5,6 +5,10 @@ import com.fasterxml.jackson.core.util.DefaultIndenter import com.fasterxml.jackson.core.util.DefaultPrettyPrinter import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectWriter +import java.io.File +import java.io.FileOutputStream +import java.io.OutputStreamWriter +import java.nio.charset.StandardCharsets import kotlin.reflect.KParameter import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor @@ -30,3 +34,16 @@ internal inline fun assertReflectEquals(expected: T, actual: T assertEquals(it.get(expected), it.get(actual)) } } + +internal fun createTempJson(json: String): File { + val file = File.createTempFile("temp", ".json") + file.deleteOnExit() + OutputStreamWriter( + FileOutputStream(file), + StandardCharsets.UTF_8 + ).use { writer -> + writer.write(json) + writer.flush() + } + return file +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/StrictNullChecksTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/StrictNullChecksTest.kt new file mode 100644 index 000000000..d0d74f09f --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/StrictNullChecksTest.kt @@ -0,0 +1,139 @@ +package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser + +import com.fasterxml.jackson.annotation.JsonSetter +import com.fasterxml.jackson.annotation.Nulls +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.exc.InvalidNullException +import com.fasterxml.jackson.module.kotlin.KotlinFeature +import com.fasterxml.jackson.module.kotlin.KotlinModule +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class StrictNullChecksTest { + val mapper: ObjectMapper = ObjectMapper() + .registerModule( + KotlinModule.Builder() + .enable(KotlinFeature.NewStrictNullChecks) + .build() + ) + + class ArrayWrapper(val value: Array) + data class ListWrapper(val value: List) + data class MapWrapper(val value: Map) + + @Nested + inner class NonNullInput { + @Test + fun array() { + val expected = ArrayWrapper(arrayOf(1)) + val src = mapper.writeValueAsString(expected) + val result = mapper.readValue(src) + + Assertions.assertArrayEquals(expected.value, result.value) + } + + @Test + fun list() { + val expected = ListWrapper(listOf(1)) + val src = mapper.writeValueAsString(expected) + val result = mapper.readValue(src) + + Assertions.assertEquals(expected, result) + } + + @Test + fun map() { + val expected = MapWrapper(mapOf("foo" to 1)) + val src = mapper.writeValueAsString(expected) + val result = mapper.readValue(src) + + Assertions.assertEquals(expected, result) + } + } + + data class AnyWrapper(val value: Any) + + @Nested + inner class NullInput { + @Test + fun array() { + val src = mapper.writeValueAsString(AnyWrapper(arrayOf(null))) + assertThrows { mapper.readValue(src) } + } + + @Test + fun list() { + val src = mapper.writeValueAsString(AnyWrapper(arrayOf(null))) + assertThrows { mapper.readValue(src) } + } + + @Test + fun map() { + val src = mapper.writeValueAsString(AnyWrapper(mapOf("foo" to null))) + assertThrows { mapper.readValue(src) } + } + } + + class ContentNullsSkipArrayWrapper(@JsonSetter(contentNulls = Nulls.SKIP) val value: Array) + data class ContentNullsSkipListWrapper(@JsonSetter(contentNulls = Nulls.SKIP) val value: List) + data class ContentNullsSkipMapWrapper(@JsonSetter(contentNulls = Nulls.SKIP) val value: Map) + + @Nested + inner class CustomByAnnotationTest { + @Test + fun array() { + val expected = ContentNullsSkipArrayWrapper(emptyArray()) + val src = mapper.writeValueAsString(AnyWrapper(arrayOf(null))) + val result = mapper.readValue(src) + + Assertions.assertArrayEquals(expected.value, result.value) + } + + @Test + fun list() { + val expected = ContentNullsSkipListWrapper(emptyList()) + val src = mapper.writeValueAsString(AnyWrapper(listOf(null))) + val result = mapper.readValue(src) + + Assertions.assertEquals(expected, result) + } + + @Test + fun map() { + val expected = ContentNullsSkipMapWrapper(emptyMap()) + val src = mapper.writeValueAsString(AnyWrapper(mapOf("foo" to null))) + val result = mapper.readValue(src) + + Assertions.assertEquals(expected, result) + } + } + + class AnnotatedArrayWrapper(@JsonSetter(nulls = Nulls.SKIP) val value: Array = emptyArray()) + data class AnnotatedListWrapper(@JsonSetter(nulls = Nulls.SKIP) val value: List = emptyList()) + data class AnnotatedMapWrapper(@JsonSetter(nulls = Nulls.SKIP) val value: Map = emptyMap()) + + // If Default is specified by annotation, it is not overridden. + @Nested + inner class AnnotatedNullInput { + @Test + fun array() { + val src = mapper.writeValueAsString(AnyWrapper(arrayOf(null))) + assertThrows { mapper.readValue(src) } + } + + @Test + fun list() { + val src = mapper.writeValueAsString(AnyWrapper(arrayOf(null))) + assertThrows { mapper.readValue(src) } + } + + @Test + fun map() { + val src = mapper.writeValueAsString(AnyWrapper(mapOf("foo" to null))) + assertThrows { mapper.readValue(src) } + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/JacksonInjectTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/JacksonInjectTest.kt index 73468e6eb..5b8e31013 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/JacksonInjectTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/JacksonInjectTest.kt @@ -3,9 +3,9 @@ package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass import com.fasterxml.jackson.annotation.JacksonInject import com.fasterxml.jackson.databind.InjectableValues import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test class JacksonInjectTest { // This is specified as a getter because there is a possibility of problems if it is assigned to a field. @@ -31,24 +31,28 @@ class JacksonInjectTest { @Test fun test() { - val injectables = InjectableValues.Std( - mapOf( - "pNn" to Primitive(0), - "pN" to Primitive(1), - "nnoNn" to NonNullObject("nnoNn"), - "nnoN" to NonNullObject("nnoN"), - "noNnNn" to NullableObject("noNnNn"), - "noNnN" to NullableObject(null), - "noNNn" to NullableObject("noNNn"), - "noNN" to NullableObject(null) - ) + val injectables = mapOf( + "pNn" to Primitive(0), + "pN" to Primitive(1), + "nnoNn" to NonNullObject("nnoNn"), + "nnoN" to NonNullObject("nnoN"), + "noNnNn" to NullableObject("noNnNn"), + "noNnN" to NullableObject(null), + "noNNn" to NullableObject("noNNn"), + "noNN" to NullableObject(null) ) val reader = jacksonObjectMapper() .readerFor(Dto::class.java) - .with(injectables) + .with(InjectableValues.Std(injectables)) + + val result = reader.readValue("{}") + val expected = ::Dto.let { ctor -> + val args = ctor.parameters.associateWith { injectables[it.name] } + ctor.callBy(args) + } - println(reader.readValue("{}")) + assertEquals(expected, result) } data class DataBind4218FailingDto( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/NullableObjectEdgeCases.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/NullableObjectEdgeCases.kt index 66d2d0878..95b44ae6d 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/NullableObjectEdgeCases.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/NullableObjectEdgeCases.kt @@ -10,9 +10,9 @@ import com.fasterxml.jackson.module.kotlin.WrapsNullableValueClassDeserializer import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import kotlin.reflect.jvm.internal.KotlinReflectionInternalError -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows class NullableObjectEdgeCases { @JvmInline @@ -61,7 +61,7 @@ class NullableObjectEdgeCases { // There is a problem with #51, so it is a failing test. @Test fun `Nulls_SKIP works`() { - assertThrows("#761(KT-57357) fixed", KotlinReflectionInternalError::class.java) { + assertThrows("#761(KT-57357) fixed") { val result = jacksonObjectMapper().readValue("""{"nn":null,"n":null}""") assertEquals(NullValue(VC("skip"), VC("skip")), result) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/ValueClasses.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/ValueClasses.kt index 5855a6f1c..1bfb3ccd9 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/ValueClasses.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/ValueClasses.kt @@ -4,12 +4,17 @@ import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.deser.std.StdDeserializer import com.fasterxml.jackson.module.kotlin.WrapsNullableValueClassDeserializer +import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer @JvmInline value class Primitive(val v: Int) { class Deserializer : StdDeserializer(Primitive::class.java) { override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Primitive = Primitive(p.intValue + 100) } + + class KeyDeserializer : JacksonKeyDeserializer() { + override fun deserializeKey(key: String, ctxt: DeserializationContext) = Primitive(key.toInt() + 100) + } } @JvmInline @@ -18,6 +23,10 @@ value class NonNullObject(val v: String) { override fun deserialize(p: JsonParser, ctxt: DeserializationContext): NonNullObject = NonNullObject(p.valueAsString + "-deser") } + + class KeyDeserializer : JacksonKeyDeserializer() { + override fun deserializeKey(key: String, ctxt: DeserializationContext) = NonNullObject("$key-deser") + } } @JvmInline @@ -28,4 +37,8 @@ value class NullableObject(val v: String?) { override fun getBoxedNullValue(): NullableObject = NullableObject("null-value-deser") } + + class KeyDeserializer : JacksonKeyDeserializer() { + override fun deserializeKey(key: String, ctxt: DeserializationContext) = NullableObject("$key-deser") + } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/WithoutCustomDeserializeMethodTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/WithoutCustomDeserializeMethodTest.kt index 79c6a3394..ab5709599 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/WithoutCustomDeserializeMethodTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/WithoutCustomDeserializeMethodTest.kt @@ -3,24 +3,22 @@ package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import org.junit.Assert.assertTrue -import org.junit.Ignore -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.Test import java.lang.reflect.InvocationTargetException -import kotlin.test.Test +import kotlin.test.assertNotEquals -@RunWith(Enclosed::class) class WithoutCustomDeserializeMethodTest { - @Ignore companion object { val mapper = jacksonObjectMapper() val throwable = IllegalArgumentException("test") } - class DirectDeserializeTest { + @Nested + inner class DirectDeserializeTest { @Test fun primitive() { val result = defaultMapper.readValue("1") @@ -34,7 +32,8 @@ class WithoutCustomDeserializeMethodTest { } @Suppress("ClassName") - class NullableObject_ { + @Nested + inner class NullableObject_ { @Test fun value() { val result = defaultMapper.readValue(""""foo"""") @@ -44,32 +43,12 @@ class WithoutCustomDeserializeMethodTest { // failing @Test fun nullString() { - // #209 has been fixed. - assertThrows(NullPointerException::class.java) { - val result = defaultMapper.readValue("null") - assertEquals(NullableObject(null), result) - } + val result = defaultMapper.readValue("null") + assertNotEquals(NullableObject(null), result, "kogera #209 has been fixed.") } } - - @Ignore - @JvmInline - value class HasCheckConstructor(val value: Int) { - init { - if (value < 0) throw throwable - } - } - - @Test - fun callConstructorCheckTest() { - val e = assertThrows(InvocationTargetException::class.java) { - defaultMapper.readValue("-1") - } - assertTrue(e.cause === throwable) - } } - @Ignore data class Dst( val pNn: Primitive, val pN: Primitive?, @@ -79,39 +58,50 @@ class WithoutCustomDeserializeMethodTest { val noN: NullableObject? ) - class InParameterDeserialize { - @Test - fun withoutNull() { - val expected = Dst( - Primitive(1), - Primitive(2), - NonNullObject("foo"), - NonNullObject("bar"), - NullableObject("baz"), - NullableObject("qux") - ) - val src = mapper.writeValueAsString(expected) - val result = mapper.readValue(src) + @Test + fun withoutNull() { + val expected = Dst( + Primitive(1), + Primitive(2), + NonNullObject("foo"), + NonNullObject("bar"), + NullableObject("baz"), + NullableObject("qux") + ) + val src = mapper.writeValueAsString(expected) + val result = mapper.readValue(src) - assertEquals(expected, result) - } + assertEquals(expected, result) + } - @Test - fun withNull() { - val expected = Dst( - Primitive(1), - null, - NonNullObject("foo"), - null, - NullableObject(null), - null - ) - val src = mapper.writeValueAsString(expected) - val result = mapper.readValue(src) + @Test + fun withNull() { + val expected = Dst( + Primitive(1), + null, + NonNullObject("foo"), + null, + NullableObject(null), + null + ) + val src = mapper.writeValueAsString(expected) + val result = mapper.readValue(src) - assertEquals(expected, result) + assertEquals(expected, result) + } + + @JvmInline + value class HasCheckConstructor(val value: Int) { + init { + if (value < 0) throw throwable } } + @Test + fun callConstructorCheckTest() { + val e = assertThrows { defaultMapper.readValue("-1") } + assertTrue(e.cause === throwable) + } + // If all JsonCreator tests are OK, no need to check throws from factory functions. } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NonNullObjectTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NonNullObjectTest.kt index c7124cdd0..4d12664c7 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NonNullObjectTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NonNullObjectTest.kt @@ -4,8 +4,8 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class NonNullObjectTest { companion object { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NullableObjectTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NullableObjectTest.kt index 408f69a95..596a44736 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NullableObjectTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/NullableObjectTest.kt @@ -4,9 +4,9 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test class NullableObjectTest { companion object { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/PrimitiveTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/PrimitiveTest.kt index 1d20a229d..a79794605 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/PrimitiveTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/defaultArgument/PrimitiveTest.kt @@ -4,8 +4,8 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class PrimitiveTest { companion object { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/SpecifiedForObjectMapperTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/SpecifiedForObjectMapperTest.kt index c56c75c2d..643883a74 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/SpecifiedForObjectMapperTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/SpecifiedForObjectMapperTest.kt @@ -6,16 +6,12 @@ import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import org.junit.Ignore -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import kotlin.test.assertNotEquals -@RunWith(Enclosed::class) class SpecifiedForObjectMapperTest { - @Ignore companion object { val mapper = jacksonObjectMapper().apply { val module = SimpleModule().apply { @@ -27,7 +23,8 @@ class SpecifiedForObjectMapperTest { } } - class DirectDeserialize { + @Nested + inner class DirectDeserialize { @Test fun primitive() { val result = mapper.readValue("1") @@ -41,7 +38,8 @@ class SpecifiedForObjectMapperTest { } @Suppress("ClassName") - class NullableObject_ { + @Nested + inner class NullableObject_ { @Test fun value() { val result = mapper.readValue(""""foo"""") @@ -51,16 +49,12 @@ class SpecifiedForObjectMapperTest { // failing @Test fun nullString() { - // #209 has been fixed. - assertThrows(NullPointerException::class.java) { - val result = mapper.readValue("null") - assertEquals(NullableObject("null-value-deser"), result) - } + val result = mapper.readValue("null") + assertNotEquals(NullableObject("null-value-deser"), result, "kogera #209 has been fixed.") } } } - @Ignore data class Dst( val pNn: Primitive, val pN: Primitive?, @@ -70,53 +64,51 @@ class SpecifiedForObjectMapperTest { val noN: NullableObject? ) - class InParameterDeserialize { - @Test - fun nonNull() { - val base = Dst( - Primitive(1), - Primitive(2), - NonNullObject("foo"), - NonNullObject("bar"), - NullableObject("baz"), - NullableObject("qux") - ) - val src = mapper.writeValueAsString(base) - val result = mapper.readValue(src) + @Test + fun nonNull() { + val base = Dst( + Primitive(1), + Primitive(2), + NonNullObject("foo"), + NonNullObject("bar"), + NullableObject("baz"), + NullableObject("qux") + ) + val src = mapper.writeValueAsString(base) + val result = mapper.readValue(src) - val expected = Dst( - Primitive(101), - Primitive(102), - NonNullObject("foo-deser"), - NonNullObject("bar-deser"), - NullableObject("baz-deser"), - NullableObject("qux-deser") - ) - assertEquals(expected, result) - } + val expected = Dst( + Primitive(101), + Primitive(102), + NonNullObject("foo-deser"), + NonNullObject("bar-deser"), + NullableObject("baz-deser"), + NullableObject("qux-deser") + ) + assertEquals(expected, result) + } - @Test - fun withNull() { - val base = Dst( - Primitive(1), - null, - NonNullObject("foo"), - null, - NullableObject(null), - null - ) - val src = mapper.writeValueAsString(base) - val result = mapper.readValue(src) + @Test + fun withNull() { + val base = Dst( + Primitive(1), + null, + NonNullObject("foo"), + null, + NullableObject(null), + null + ) + val src = mapper.writeValueAsString(base) + val result = mapper.readValue(src) - val expected = Dst( - Primitive(101), - null, - NonNullObject("foo-deser"), - null, - NullableObject("null-value-deser"), - null - ) - assertEquals(expected, result) - } + val expected = Dst( + Primitive(101), + null, + NonNullObject("foo-deser"), + null, + NullableObject("null-value-deser"), + null + ) + assertEquals(expected, result) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/SpecifiedForClassTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/SpecifiedForClassTest.kt index 55d42d10b..8b8bcb19f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/SpecifiedForClassTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/SpecifiedForClassTest.kt @@ -6,8 +6,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.databind.deser.std.StdDeserializer import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class SpecifiedForClassTest { @JsonDeserialize(using = Value.Deserializer::class) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NonNullObjectTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NonNullObjectTest.kt index 5cc395415..744de4168 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NonNullObjectTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NonNullObjectTest.kt @@ -4,20 +4,15 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject -import org.junit.Assert.assertEquals -import org.junit.Ignore -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -@RunWith(Enclosed::class) class NonNullObjectTest { - @Ignore companion object { val mapper = jacksonObjectMapper() } - @Ignore data class NonNull( @get:JsonDeserialize(using = NonNullObject.Deserializer::class) val getterAnn: NonNullObject, @@ -25,22 +20,19 @@ class NonNullObjectTest { val fieldAnn: NonNullObject ) - class NonNullTest { - @Test - fun nonNull() { - val result = mapper.readValue( - """ + @Test + fun nonNull() { + val result = mapper.readValue( + """ { "getterAnn" : "foo", "fieldAnn" : "bar" } """.trimIndent() - ) - assertEquals(NonNull(NonNullObject("foo-deser"), NonNullObject("bar-deser")), result) - } + ) + assertEquals(NonNull(NonNullObject("foo-deser"), NonNullObject("bar-deser")), result) } - @Ignore data class Nullable( @get:JsonDeserialize(using = NonNullObject.Deserializer::class) val getterAnn: NonNullObject?, @@ -48,7 +40,8 @@ class NonNullObjectTest { val fieldAnn: NonNullObject? ) - class NullableTest { + @Nested + inner class NullableTest { @Test fun nonNullInput() { val result = mapper.readValue( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NullableObjectTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NullableObjectTest.kt index b9319f938..ad424ad08 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NullableObjectTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/NullableObjectTest.kt @@ -4,20 +4,15 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject -import org.junit.Assert.assertEquals -import org.junit.Ignore -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -@RunWith(Enclosed::class) class NullableObjectTest { - @Ignore companion object { val mapper = jacksonObjectMapper() } - @Ignore data class NonNull( @get:JsonDeserialize(using = NullableObject.DeserializerWrapsNullable::class) val getterAnn: NullableObject, @@ -25,22 +20,19 @@ class NullableObjectTest { val fieldAnn: NullableObject ) - class NonNullTest { - @Test - fun nonNull() { - val result = mapper.readValue( - """ + @Test + fun nonNull() { + val result = mapper.readValue( + """ { "getterAnn" : "foo", "fieldAnn" : "bar" } """.trimIndent() - ) - assertEquals(NonNull(NullableObject("foo-deser"), NullableObject("bar-deser")), result) - } + ) + assertEquals(NonNull(NullableObject("foo-deser"), NullableObject("bar-deser")), result) } - @Ignore data class Nullable( @get:JsonDeserialize(using = NullableObject.DeserializerWrapsNullable::class) val getterAnn: NullableObject?, @@ -48,7 +40,8 @@ class NullableObjectTest { val fieldAnn: NullableObject? ) - class NullableTest { + @Nested + inner class NullableTest { @Test fun nonNullInput() { val result = mapper.readValue( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/PrimitiveTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/PrimitiveTest.kt index 7cdc31b8c..521e762f8 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/PrimitiveTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/deserializer/byAnnotation/specifiedForProperty/PrimitiveTest.kt @@ -4,20 +4,15 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive -import org.junit.Assert.assertEquals -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith -import kotlin.test.Ignore -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -@RunWith(Enclosed::class) class PrimitiveTest { - @Ignore companion object { val mapper = jacksonObjectMapper() } - @Ignore data class NonNull( @get:JsonDeserialize(using = Primitive.Deserializer::class) val getterAnn: Primitive, @@ -25,22 +20,19 @@ class PrimitiveTest { val fieldAnn: Primitive ) - class NonNullTest { - @Test - fun nonNull() { - val result = mapper.readValue( - """ + @Test + fun nonNull() { + val result = mapper.readValue( + """ { "getterAnn" : 1, "fieldAnn" : 2 } """.trimIndent() - ) - assertEquals(NonNull(Primitive(101), Primitive(102)), result) - } + ) + assertEquals(NonNull(Primitive(101), Primitive(102)), result) } - @Ignore data class Nullable( @get:JsonDeserialize(using = Primitive.Deserializer::class) val getterAnn: Primitive?, @@ -48,7 +40,8 @@ class PrimitiveTest { val fieldAnn: Primitive? ) - class NullableTest { + @Nested + inner class NullableTest { @Test fun nonNullInput() { val result = mapper.readValue( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByJacksonTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByJacksonTest.kt index 047f470e9..effeea18f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByJacksonTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByJacksonTest.kt @@ -3,8 +3,8 @@ package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.j import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test // Test for Creator that can be handled by the Jackson mechanism. class HandledByJacksonTest { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByKogeraTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByKogeraTest.kt index 147c9889a..bf76a9c2f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByKogeraTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/HandledByKogeraTest.kt @@ -4,9 +4,9 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.databind.exc.InvalidDefinitionException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test // Test on the case of deserialization by ValueClassBoxDeserializer class HandledByKogeraTest { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/InCreatorArgumentTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/InCreatorArgumentTest.kt index 621a213dd..cd1961622 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/InCreatorArgumentTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/jsonCreator/InCreatorArgumentTest.kt @@ -6,8 +6,8 @@ import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.No import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test private fun Primitive.modify(): Primitive = Primitive(v + 100) private fun NonNullObject.modify(): NonNullObject = NonNullObject("$v-creator") diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/WithoutCustomDeserializeMethodTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/WithoutCustomDeserializeMethodTest.kt new file mode 100644 index 000000000..b80d7375b --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/WithoutCustomDeserializeMethodTest.kt @@ -0,0 +1,117 @@ +package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey + +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonMappingException +import com.fasterxml.jackson.databind.exc.InvalidDefinitionException +import com.fasterxml.jackson.databind.module.SimpleModule +import com.fasterxml.jackson.module.kotlin.defaultMapper +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject +import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject +import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import java.lang.reflect.InvocationTargetException +import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer + +class WithoutCustomDeserializeMethodTest { + companion object { + val throwable = IllegalArgumentException("test") + } + + @Nested + inner class DirectDeserialize { + @Test + fun primitive() { + val result = defaultMapper.readValue>("""{"1":null}""") + assertEquals(mapOf(Primitive(1) to null), result) + } + + @Test + fun nonNullObject() { + val result = defaultMapper.readValue>("""{"foo":null}""") + assertEquals(mapOf(NonNullObject("foo") to null), result) + } + + @Test + fun nullableObject() { + val result = defaultMapper.readValue>("""{"bar":null}""") + assertEquals(mapOf(NullableObject("bar") to null), result) + } + } + + data class Dst( + val p: Map, + val nn: Map, + val n: Map + ) + + @Test + fun wrapped() { + val src = """ + { + "p":{"1":null}, + "nn":{"foo":null}, + "n":{"bar":null} + } + """.trimIndent() + val result = defaultMapper.readValue(src) + val expected = Dst( + mapOf(Primitive(1) to null), + mapOf(NonNullObject("foo") to null), + mapOf(NullableObject("bar") to null) + ) + + assertEquals(expected, result) + } + + @JvmInline + value class HasCheckConstructor(val value: Int) { + init { + if (value < 0) throw throwable + } + } + + @Test + fun callConstructorCheckTest() { + val e = assertThrows { + defaultMapper.readValue>("""{"-1":null}""") + } + assertTrue(e.cause === throwable) + } + + data class Wrapped(val first: String, val second: String) { + class KeyDeserializer : JacksonKeyDeserializer() { + override fun deserializeKey(key: String, ctxt: DeserializationContext) = + key.split("-").let { Wrapped(it[0], it[1]) } + } + } + + @JvmInline + value class Wrapper(val w: Wrapped) + + @Test + fun wrappedCustomObject() { + // If a type that cannot be deserialized is specified, the default is an error. + val thrown = assertThrows { + defaultMapper.readValue>("""{"foo-bar":null}""") + } + assertTrue(thrown.cause is InvalidDefinitionException) + + val mapper = jacksonObjectMapper() + .registerModule( + object : SimpleModule() { + init { addKeyDeserializer(Wrapped::class.java, Wrapped.KeyDeserializer()) } + } + ) + + val result = mapper.readValue>("""{"foo-bar":null}""") + val expected = mapOf(Wrapper(Wrapped("foo", "bar")) to null) + + assertEquals(expected, result) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/SpecifiedForObjectMapperTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/SpecifiedForObjectMapperTest.kt new file mode 100644 index 000000000..e348211f2 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/SpecifiedForObjectMapperTest.kt @@ -0,0 +1,70 @@ +package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer + +import com.fasterxml.jackson.databind.module.SimpleModule +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject +import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject +import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +class SpecifiedForObjectMapperTest { + companion object { + val mapper = jacksonObjectMapper().apply { + val module = SimpleModule().apply { + this.addKeyDeserializer(Primitive::class.java, Primitive.KeyDeserializer()) + this.addKeyDeserializer(NonNullObject::class.java, NonNullObject.KeyDeserializer()) + this.addKeyDeserializer(NullableObject::class.java, NullableObject.KeyDeserializer()) + } + this.registerModule(module) + } + } + + @Nested + inner class DirectDeserialize { + @Test + fun primitive() { + val result = mapper.readValue>("""{"1":null}""") + assertEquals(mapOf(Primitive(101) to null), result) + } + + @Test + fun nonNullObject() { + val result = mapper.readValue>("""{"foo":null}""") + assertEquals(mapOf(NonNullObject("foo-deser") to null), result) + } + + @Test + fun nullableObject() { + val result = mapper.readValue>("""{"bar":null}""") + assertEquals(mapOf(NullableObject("bar-deser") to null), result) + } + } + + data class Dst( + val p: Map, + val nn: Map, + val n: Map + ) + + @Test + fun wrapped() { + val src = """ + { + "p":{"1":null}, + "nn":{"foo":null}, + "n":{"bar":null} + } + """.trimIndent() + val result = mapper.readValue(src) + val expected = Dst( + mapOf(Primitive(101) to null), + mapOf(NonNullObject("foo-deser") to null), + mapOf(NullableObject("bar-deser") to null) + ) + + assertEquals(expected, result) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForClassTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForClassTest.kt new file mode 100644 index 000000000..38642f058 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForClassTest.kt @@ -0,0 +1,37 @@ +package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation + +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.module.kotlin.defaultMapper +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer + +class SpecifiedForClassTest { + @JsonDeserialize(keyUsing = Value.KeyDeserializer::class) + @JvmInline + value class Value(val v: Int) { + class KeyDeserializer : JacksonKeyDeserializer() { + override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100) + } + } + + @Test + fun directDeserTest() { + val result = defaultMapper.readValue>("""{"1":null}""") + + assertEquals(mapOf(Value(101) to null), result) + } + + data class Wrapper(val v: Map) + + @Test + fun paramDeserTest() { + val mapper = jacksonObjectMapper() + val result = mapper.readValue("""{"v":{"1":null}}""") + + assertEquals(Wrapper(mapOf(Value(101) to null)), result) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForPropertyTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForPropertyTest.kt new file mode 100644 index 000000000..5dd383c26 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForPropertyTest.kt @@ -0,0 +1,28 @@ +package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation + +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer + +class SpecifiedForPropertyTest { + @JvmInline + value class Value(val v: Int) { + class KeyDeserializer : JacksonKeyDeserializer() { + override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100) + } + } + + data class Wrapper(@JsonDeserialize(keyUsing = Value.KeyDeserializer::class) val v: Map) + + @Test + fun paramDeserTest() { + val mapper = jacksonObjectMapper() + val result = mapper.readValue("""{"v":{"1":null}}""") + + assertEquals(Wrapper(mapOf(Value(101) to null)), result) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithDefaultArgumentsTest.kt index 89a6ac4bc..8947b7ef6 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithDefaultArgumentsTest.kt @@ -4,9 +4,10 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.assertThrows import kotlin.reflect.jvm.internal.KotlinReflectionInternalError -import kotlin.test.Test +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, @@ -50,12 +51,14 @@ class DeserializeByConstructorWithDefaultArgumentsTest { val p31: NonNullObject = NonNullObject("31") ) - @Test(expected = KotlinReflectionInternalError::class) + @Test fun test32() { - assertEquals(Dst32(), defaultMapper.readValue("{}")) - // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. - // After upgrading to Kotlin 2.0, remove exception-related descriptions. - if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + assertThrows { + assertEquals(Dst32(), defaultMapper.readValue("{}")) + // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. + // After upgrading to Kotlin 2.0, remove exception-related descriptions. + if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + } } data class Dst33( @@ -166,12 +169,14 @@ class DeserializeByConstructorWithDefaultArgumentsTest { val p63: NonNullObject = NonNullObject("63") ) - @Test(expected = KotlinReflectionInternalError::class) + @Test fun test64() { - assertEquals(Dst64(), defaultMapper.readValue("{}")) - // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. - // After upgrading to Kotlin 2.0, remove exception-related descriptions. - if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + assertThrows { + assertEquals(Dst64(), defaultMapper.readValue("{}")) + // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. + // After upgrading to Kotlin 2.0, remove exception-related descriptions. + if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + } } data class Dst65( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt index 8c949770b..03b80e973 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithDefaultArgumentsTest.kt index 809e505c9..612822fdd 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithDefaultArgumentsTest.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt index ebbb90db9..2ba9df1c1 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nonNullObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt @@ -6,8 +6,8 @@ import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test // Convert the property p to q (but not the value) to make it an input to the factory function. private fun replacePQ(src: String) = src.replace(Regex("""p\d+":""")) { "q" + it.value.substring(1) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithDefaultArgumentsTest.kt index 7753b710a..e6a95c3e4 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithDefaultArgumentsTest.kt @@ -4,9 +4,10 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.assertThrows import kotlin.reflect.jvm.internal.KotlinReflectionInternalError -import kotlin.test.Test +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, @@ -50,12 +51,14 @@ class DeserializeByConstructorWithDefaultArgumentsTest { val p31: NullableObject? = NullableObject("31") ) - @Test(expected = KotlinReflectionInternalError::class) + @Test fun test32() { - assertEquals(Dst32(), defaultMapper.readValue("{}")) - // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. - // After upgrading to Kotlin 2.0, remove exception-related descriptions. - if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + assertThrows { + assertEquals(Dst32(), defaultMapper.readValue("{}")) + // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. + // After upgrading to Kotlin 2.0, remove exception-related descriptions. + if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + } } data class Dst33( @@ -166,12 +169,14 @@ class DeserializeByConstructorWithDefaultArgumentsTest { val p63: NullableObject? = NullableObject("63") ) - @Test(expected = KotlinReflectionInternalError::class) + @Test fun test64() { - assertEquals(Dst64(), defaultMapper.readValue("{}")) - // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. - // After upgrading to Kotlin 2.0, remove exception-related descriptions. - if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + assertThrows { + assertEquals(Dst64(), defaultMapper.readValue("{}")) + // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. + // After upgrading to Kotlin 2.0, remove exception-related descriptions. + if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + } } data class Dst65( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt index 4971417cc..b9bbf9129 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByConstructorWithoutDefaultArgumentsTest.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithDefaultArgumentsTest.kt index 15339b9b1..2004745c5 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithDefaultArgumentsTest.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt index f56b2acfe..ad3be7c73 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/nullableObject/DeserializeByFactoryWithoutDefaultArgumentsTest.kt @@ -6,8 +6,8 @@ import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test // Convert the property p to q (but not the value) to make it an input to the factory function. private fun replacePQ(src: String) = src.replace(Regex("""p\d+":""")) { "q" + it.value.substring(1) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithDefaultArgumentsTest.kt index 1fbf39584..c057ed4c1 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithDefaultArgumentsTest.kt @@ -4,9 +4,10 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.assertThrows import kotlin.reflect.jvm.internal.KotlinReflectionInternalError -import kotlin.test.Test +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, @@ -50,12 +51,14 @@ class DeserializeByConstructorWithDefaultArgumentsTest { val p31: Primitive = Primitive(31) ) - @Test(expected = KotlinReflectionInternalError::class) + @Test fun test32() { - assertEquals(Dst32(), defaultMapper.readValue("{}")) - // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. - // After upgrading to Kotlin 2.0, remove exception-related descriptions. - if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + assertThrows { + assertEquals(Dst32(), defaultMapper.readValue("{}")) + // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. + // After upgrading to Kotlin 2.0, remove exception-related descriptions. + if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + } } data class Dst33( @@ -166,12 +169,14 @@ class DeserializeByConstructorWithDefaultArgumentsTest { val p63: Primitive = Primitive(63) ) - @Test(expected = KotlinReflectionInternalError::class) + @Test fun test64() { - assertEquals(Dst64(), defaultMapper.readValue("{}")) - // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. - // After upgrading to Kotlin 2.0, remove exception-related descriptions. - if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + assertThrows { + assertEquals(Dst64(), defaultMapper.readValue("{}")) + // TODO: #762 is resolved after Kotlin 2.0, so the reason why throw is done is to make CI with Kotlin 2.0 succeed. + // After upgrading to Kotlin 2.0, remove exception-related descriptions. + if (KotlinVersion.CURRENT.major >= 2) throw KotlinReflectionInternalError("") + } } data class Dst65( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithoutDefaultArgumentsTest.kt index 9d45f8527..cf2b2cff3 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByConstructorWithoutDefaultArgumentsTest.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithDefaultArgumentsTest.kt index 3963e8448..61a0702b8 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithDefaultArgumentsTest.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test /** * Up to argument size 32 there is one mask argument for the default argument, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithoutDefaultArgumentsTest.kt index bcd5d766e..758a4ba81 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/kogeraIntegration/deser/valueClass/parameterSize/primitive/DeserializeByFactoryWithoutDefaultArgumentsTest.kt @@ -6,8 +6,8 @@ import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive -import org.junit.Assert.assertEquals -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test // Convert the property p to q (but not the value) to make it an input to the factory function. private fun replacePQ(src: String) = src.replace(Regex("""p\d+":""")) { "q" + it.value.substring(1) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/DurationTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/DurationTests.kt index 75db96297..2586728fd 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/DurationTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/DurationTests.kt @@ -11,7 +11,7 @@ import com.fasterxml.jackson.module.kotlin.KotlinFeature.UseJavaDurationConversi import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import java.time.Instant import kotlin.test.assertContentEquals import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ExtensionMethodsTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ExtensionMethodsTests.kt index 6d6216ffa..04dc9b3ae 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ExtensionMethodsTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ExtensionMethodsTests.kt @@ -14,12 +14,11 @@ import com.fasterxml.jackson.module.kotlin.minusAssign import com.fasterxml.jackson.module.kotlin.plusAssign import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.treeToValue -import org.hamcrest.CoreMatchers.`is` -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test -class TestExtensionMethods { +private class TestExtensionMethods { val mapper: ObjectMapper = jacksonObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false) data class BasicPerson(val name: String, val age: Int) @@ -33,9 +32,9 @@ class TestExtensionMethods { val expectedPerson = BasicPerson("John Smith", 30) - assertThat(inferRightSide, equalTo(expectedPerson)) - assertThat(inferLeftSide, equalTo(expectedPerson)) - assertThat(person, equalTo(expectedPerson)) + assertEquals(expectedPerson, inferRightSide) + assertEquals(expectedPerson, inferLeftSide) + assertEquals(expectedPerson, person) } data class MyData(val a: String, val b: Int) @@ -43,7 +42,7 @@ class TestExtensionMethods { @Test fun testStackOverflow33368328() { val jsonStr = """[{"a": "value1", "b": 1}, {"a": "value2", "b": 2}]""" val myList: List = mapper.readValue(jsonStr) - assertThat(myList, equalTo(listOf(MyData("value1", 1), MyData("value2", 2)))) + assertEquals(listOf(MyData("value1", 1), MyData("value2", 2)), myList) } @Test fun testOperatorFunExtensions() { @@ -56,8 +55,8 @@ class TestExtensionMethods { objectNode -= "foo1" objectNode -= listOf("foo2") - assertThat("foo1" !in objectNode, `is`(true)) - assertThat("foo3" in objectNode, `is`(true)) + assertTrue("foo1" !in objectNode) + assertTrue("foo3" in objectNode) val arrayNode = factory.arrayNode() arrayNode += "foo" @@ -66,35 +65,34 @@ class TestExtensionMethods { arrayNode += 1.0 arrayNode += "bar".toByteArray() - assertThat(arrayNode.size(), `is`(5)) + assertEquals(5, arrayNode.size()) (4 downTo 0).forEach { arrayNode -= it } - assertThat(arrayNode.size(), `is`(0)) + assertEquals(0, arrayNode.size()) } - @Test fun noTypeErasure(){ + @Test fun noTypeErasure() { data class Person(val name: String) val source = """[ { "name" : "Neo" } ]""" val tree = mapper.readTree(source) val readValueResult: List = mapper.readValue(source) - assertThat(readValueResult, `is`(listOf(Person("Neo")))) + assertEquals(listOf(Person("Neo")), readValueResult) val treeToValueResult: List = mapper.treeToValue(tree) - assertThat(treeToValueResult, `is`(listOf(Person("Neo")))) + assertEquals(listOf(Person("Neo")), treeToValueResult) val convertValueResult: List = mapper.convertValue(tree) - assertThat(convertValueResult, `is`(listOf(Person("Neo")))) + assertEquals(listOf(Person("Neo")), convertValueResult) } @Test fun mixInExtensionTest() { - data class Person(val name: String) abstract class PersonMixIn { @JsonIgnore var name: String = "" } val mapper: JsonMapper = jsonMapper { addMixIn() } val serializedPerson: String = mapper.writeValueAsString(Person("test")) - assertThat(serializedPerson, `is`("{}")) + assertEquals("{}", serializedPerson) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt index 40fe0eccd..7e97f4304 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt @@ -4,29 +4,47 @@ import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import junit.framework.TestCase.assertEquals -import org.junit.Assert.assertThrows -import kotlin.test.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test class FailNullForPrimitiveTest { - data class Dto( + val mapper = jacksonObjectMapper() + .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + + data class NoDefaultValue( val foo: Int, val bar: Int? ) @Test - fun test() { - val mapper = jacksonObjectMapper() - .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + fun noDefaultValueTest() { + // If no default value is set, it will fail if undefined or null is entered + assertThrows(MismatchedInputException::class.java) { + mapper.readValue("{}") + } assertThrows(MismatchedInputException::class.java) { - mapper.readValue("{}") + mapper.readValue("""{"foo":null}""") } + assertEquals(NoDefaultValue(0, null), mapper.readValue("""{"foo":0}""")) + } + + data class HasDefaultValue( + val foo: Int = -1, + val bar: Int? = -1 + ) + + @Test + fun hasDefaultValueTest() { + // If a default value is set, an input of undefined will succeed, but null will fail + assertEquals(HasDefaultValue(-1, -1), mapper.readValue("{}")) + assertThrows(MismatchedInputException::class.java) { - mapper.readValue("""{"foo":null}""") + mapper.readValue("""{"foo":null}""") } - assertEquals(Dto(0, null), mapper.readValue("""{"foo":0}""")) + assertEquals(HasDefaultValue(0, null), mapper.readValue("""{"foo":0, "bar":null}""")) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/IteratorTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/IteratorTests.kt index abe543553..d2ec75651 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/IteratorTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/IteratorTests.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestIteratorSubclass { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KClassSerializerDeserializerTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KClassSerializerDeserializerTest.kt index 649b8aa63..3a0e7eb0e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KClassSerializerDeserializerTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KClassSerializerDeserializerTest.kt @@ -8,9 +8,8 @@ import com.fasterxml.jackson.module.kotlin.addDeserializer import com.fasterxml.jackson.module.kotlin.addSerializer import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import java.math.BigDecimal import java.math.RoundingMode @@ -26,8 +25,8 @@ class KClassSerializerDeserializerTest { fun `test custom serializer expecting object serialized with rounding serializer applied`() { val jsonString = objectMapper.writeValueAsString(TestDoubleData(nonNullVal = 1.5567, nullVal = 1.5678)) val testResult = objectMapper.readValue(jsonString, TestDoubleData::class.java) - assertThat(testResult.nonNullVal, equalTo(1.56)) - assertThat(testResult.nullVal, equalTo(1.57)) + assertEquals(1.56, testResult.nonNullVal) + assertEquals(1.57, testResult.nullVal) } @Test @@ -38,8 +37,8 @@ class KClassSerializerDeserializerTest { "nullVal":1.5678 } """.trimIndent()) - assertThat(testResult.nonNullVal, equalTo(1.56)) - assertThat(testResult.nullVal, equalTo(1.57)) + assertEquals(1.56, testResult.nonNullVal) + assertEquals(1.57, testResult.nullVal) } } @@ -62,4 +61,4 @@ class RoundingDeserializer : JsonDeserializer() { .setScale(2, RoundingMode.HALF_UP) .toDouble() } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinBuiltinsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinBuiltinsTest.kt index a06f2a8c9..71eb6db9c 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinBuiltinsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinBuiltinsTest.kt @@ -4,9 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class TestJacksonWithKotlinBuiltins { private val mapper: ObjectMapper = jacksonObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false) @@ -17,9 +16,9 @@ class TestJacksonWithKotlinBuiltins { val json = """{"name":{"first":"John","second":"Smith"},"age":30}""" val expected = ClassWithPair(Pair("John", "Smith"), 30) - assertThat(mapper.writeValueAsString(expected), equalTo(json)) + assertEquals(json, mapper.writeValueAsString(expected)) val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(expected)) + assertEquals(expected, stateObj) } private data class ClassWithPairMixedTypes(val person: Pair) @@ -28,9 +27,9 @@ class TestJacksonWithKotlinBuiltins { val json = """{"person":{"first":"John","second":30}}""" val expected = ClassWithPairMixedTypes(Pair("John", 30)) - assertThat(mapper.writeValueAsString(expected), equalTo(json)) + assertEquals(json, mapper.writeValueAsString(expected)) val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(expected)) + assertEquals(expected, stateObj) } private data class ClassWithTriple(val name: Triple, val age: Int) @@ -39,9 +38,9 @@ class TestJacksonWithKotlinBuiltins { val json = """{"name":{"first":"John","second":"Davey","third":"Smith"},"age":30}""" val expected = ClassWithTriple(Triple("John", "Davey", "Smith"), 30) - assertThat(mapper.writeValueAsString(expected), equalTo(json)) + assertEquals(json, mapper.writeValueAsString(expected)) val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(expected)) + assertEquals(expected, stateObj) } private data class ClassWithRanges(val ages: IntRange, val distance: LongRange) @@ -50,9 +49,9 @@ class TestJacksonWithKotlinBuiltins { val json = """{"ages":{"start":18,"end":40},"distance":{"start":5,"end":50}}""" val expected = ClassWithRanges(IntRange(18, 40), LongRange(5, 50)) - assertThat(mapper.writeValueAsString(expected), equalTo(json)) + assertEquals(json, mapper.writeValueAsString(expected)) val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(expected)) + assertEquals(expected, stateObj) } private data class ClassWithPairMixedNullableTypes(val person: Pair) @@ -61,20 +60,20 @@ class TestJacksonWithKotlinBuiltins { val json = """{"person":{"first":"John","second":null}}""" val expected = ClassWithPairMixedNullableTypes(Pair("John", null)) - assertThat(mapper.writeValueAsString(expected), equalTo(json)) + assertEquals(json, mapper.writeValueAsString(expected)) val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(expected)) + assertEquals(expected, stateObj) } - private data class GenericParametersClass(val one: A, val two: B) + private data class GenericParametersClass(val one: A, val two: B) private data class GenericParameterConsumer(val thing: GenericParametersClass) @Test fun testGenericParametersInConstructor() { val json = """{"thing":{"one":null,"two":123}}""" val expected = GenericParameterConsumer(GenericParametersClass(null, 123)) - assertThat(mapper.writeValueAsString(expected), equalTo(json)) + assertEquals(json, mapper.writeValueAsString(expected)) val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(expected)) + assertEquals(expected, stateObj) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinFeatures.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinFeatures.kt index 9c8ce9908..f5b7d6cc5 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinFeatures.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/KotlinFeatures.kt @@ -7,17 +7,15 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.module.kotlin.* import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import kotlin.properties.Delegates import kotlin.test.assertNull import kotlin.test.fail - private data class DataClassPerson(val name: String, val age: Int) -class TestM11Changes { +private class TestM11Changes { val mapper = jacksonObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false) private class Class_With_One_Constructor(val name: String, val age: Int) @@ -27,26 +25,24 @@ class TestM11Changes { val expectedPerson = Class_With_One_Constructor("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson.name, equalTo(expectedPerson.name)) - assertThat(newPerson.age, equalTo(expectedPerson.age)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson.name, newPerson.name) + assertEquals(expectedPerson.age, newPerson.age) } private data class Class_Data_Annotation_With_One_Constructor(val name: String, val age: Int) @Test fun testDataClass_One_Constructor() { - - val expectedJson = """{"name":"John Smith","age":30}""" val expectedPerson = Class_Data_Annotation_With_One_Constructor("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson, equalTo(expectedPerson)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson, newPerson) } private data class Class_With_Init_Constructor(val name: String, val age: Int) { @@ -57,15 +53,14 @@ class TestM11Changes { } @Test fun testDataClass_Init_Constructor() { - val expectedJson = """{"name":"John Smith","age":30,"otherThing":"franky"}""" val expectedPerson = Class_With_Init_Constructor("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson, equalTo(expectedPerson)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson, newPerson) } private data class Class_With_Init_Constructor_And_Ignored_Property(val name: String, val age: Int) { @@ -76,15 +71,14 @@ class TestM11Changes { } @Test fun testDataClass_Init_Constructor_And_Ignored_Property() { - val expectedJson = """{"name":"John Smith","age":30}""" val expectedPerson = Class_With_Init_Constructor_And_Ignored_Property("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson, equalTo(expectedPerson)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson, newPerson) } private class Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter(val name: String, age: Int) { @@ -92,89 +86,84 @@ class TestM11Changes { } @Test fun testDataClass_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter() { - val expectedJson = """{"name":"John Smith","age":30}""" val expectedPerson = Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson.name, equalTo(expectedPerson.name)) - assertThat(newPerson.age, equalTo(expectedPerson.age)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson.name, newPerson.name) + assertEquals(expectedPerson.age, newPerson.age) } private class ClassFor_testDataClass_WithOnlySecondaryConstructor { val name: String val age: Int constructor(name: String, age: Int) { - this.name = name + this.name = name this.age = age } } @Test fun testDataClass_WithOnlySecondaryConstructor() { - val expectedJson = """{"name":"John Smith","age":30}""" val expectedPerson = ClassFor_testDataClass_WithOnlySecondaryConstructor("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson.name, equalTo(expectedPerson.name)) - assertThat(newPerson.age, equalTo(expectedPerson.age)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson.name, newPerson.name) + assertEquals(expectedPerson.age, newPerson.age) } - private class Class_WithPrimaryAndSecondaryConstructor(val name: String, val age: Int) { constructor(nameAndAge: String) : this(nameAndAge.substringBefore(':'), nameAndAge.substringAfter(':').toInt()) { - } } @Test fun testDataClass_WithPrimaryAndSecondaryConstructor() { - val expectedJson = """{"name":"John Smith","age":30}""" val expectedPerson = Class_WithPrimaryAndSecondaryConstructor("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson.name, equalTo(expectedPerson.name)) - assertThat(newPerson.age, equalTo(expectedPerson.age)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson.name, newPerson.name) + assertEquals(expectedPerson.age, newPerson.age) } private class Class_WithPrimaryAndSecondaryConstructorAnnotated(name: String) { val name: String = name var age: Int = 0 + @JsonCreator constructor(name: String, age: Int) : this(name) { this.age = age } } @Test fun testDataClass_WithPrimaryAndSecondaryConstructorBothCouldBeUsedToDeserialize() { - val expectedJson = """{"name":"John Smith","age":30}""" val expectedPerson = Class_WithPrimaryAndSecondaryConstructorAnnotated("John Smith", 30) val actualJson = mapper.writeValueAsString(expectedPerson) - val newPerson = mapper.readValue(actualJson) + val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson.name, equalTo(expectedPerson.name)) - assertThat(newPerson.age, equalTo(expectedPerson.age)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson.name, newPerson.name) + assertEquals(expectedPerson.age, newPerson.age) val jsonWithNoAge = """{"name":"John Smith"}""" - val personNoAge = mapper.readValue(jsonWithNoAge) + val personNoAge = mapper.readValue(jsonWithNoAge) - assertThat(personNoAge.age, equalTo(0)) - assertThat(personNoAge.name, equalTo("John Smith")) + assertEquals(0, personNoAge.age) + assertEquals("John Smith", personNoAge.name) } @JsonInclude(JsonInclude.Include.NON_EMPTY) - class Class_WithPartialFieldsInConstructor(val name: String, @JsonProperty("age") val years: Int) { + class Class_WithPartialFieldsInConstructor(val name: String, @JsonProperty("age") val years: Int) { @JsonProperty("address") var primaryAddress: String = "" var phone: String by Delegates.notNull() } @@ -187,11 +176,11 @@ class TestM11Changes { val actualJson = mapper.writeValueAsString(expectedPerson) val newPerson = mapper.readValue(actualJson) - assertThat(actualJson, equalTo(expectedJson)) - assertThat(newPerson.name, equalTo(expectedPerson.name)) - assertThat(newPerson.years, equalTo(expectedPerson.years)) - assertThat(newPerson.phone, equalTo(expectedPerson.phone)) - assertThat(newPerson.primaryAddress, equalTo(expectedPerson.primaryAddress)) + assertEquals(expectedJson, actualJson) + assertEquals(expectedPerson.name, newPerson.name) + assertEquals(expectedPerson.years, newPerson.years) + assertEquals(expectedPerson.phone, newPerson.phone) + assertEquals(expectedPerson.primaryAddress, newPerson.primaryAddress) val jsonWithNullPhone = """{"name":"John Smith","age":30}""" val person = mapper.readValue(jsonWithNullPhone) @@ -207,4 +196,4 @@ class TestM11Changes { val newPerson = mapper.readValue("null") assertNull(newPerson) } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt index b273e304f..5e8dd818b 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt @@ -5,8 +5,9 @@ import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows class TestNullToDefault { @@ -42,12 +43,12 @@ class TestNullToDefault { }""" ) - Assert.assertEquals(-1, item.sku) - Assert.assertEquals("plain", item.text) - Assert.assertEquals("some image", item.images) - Assert.assertEquals("uk", item.language) - Assert.assertTrue(item.temperature == 24.7) - Assert.assertEquals(true, item.canBeProcessed) + assertEquals(-1, item.sku) + assertEquals("plain", item.text) + assertEquals("some image", item.images) + assertEquals("uk", item.language) + assertTrue(item.temperature == 24.7) + assertEquals(true, item.canBeProcessed) } @Test @@ -63,12 +64,12 @@ class TestNullToDefault { }""" ) - Assert.assertEquals(0, item.sku) - Assert.assertEquals("plain", item.text) - Assert.assertEquals("image1", item.images) - Assert.assertEquals("pl", item.language) - Assert.assertTrue(item.temperature == 0.0) - Assert.assertEquals(false, item.canBeProcessed) + assertEquals(0, item.sku) + assertEquals("plain", item.text) + assertEquals("image1", item.images) + assertEquals("pl", item.language) + assertTrue(item.temperature == 0.0) + assertEquals(false, item.canBeProcessed) } @Test @@ -84,12 +85,12 @@ class TestNullToDefault { }""" ) - Assert.assertEquals(974, item.sku) - Assert.assertEquals("plain", item.text) - Assert.assertEquals(null, item.images) - Assert.assertEquals("pl", item.language) - Assert.assertTrue(item.temperature == 36.6) - Assert.assertEquals(false, item.canBeProcessed) + assertEquals(974, item.sku) + assertEquals("plain", item.text) + assertEquals(null, item.images) + assertEquals("pl", item.language) + assertTrue(item.temperature == 36.6) + assertEquals(false, item.canBeProcessed) } @Test @@ -103,10 +104,10 @@ class TestNullToDefault { }""" ) - Assert.assertEquals(0, item.sku) - Assert.assertEquals("plain", item.text) - Assert.assertTrue(item.temperature == 0.0) - Assert.assertEquals(false, item.canBeProcessed) + assertEquals(0, item.sku) + assertEquals("plain", item.text) + assertTrue(item.temperature == 0.0) + assertEquals(false, item.canBeProcessed) } @Test @@ -117,10 +118,10 @@ class TestNullToDefault { }""" ) - Assert.assertEquals(-1, item.sku) - Assert.assertEquals("plain", item.text) - Assert.assertTrue(item.temperature == 24.7) - Assert.assertEquals(true, item.canBeProcessed) + assertEquals(-1, item.sku) + assertEquals("plain", item.text) + assertTrue(item.temperature == 24.7) + assertEquals(true, item.canBeProcessed) } @Test @@ -131,21 +132,23 @@ class TestNullToDefault { }""" ) - Assert.assertEquals(-1, item.sku) - Assert.assertEquals("plain", item.text) - Assert.assertEquals("some image", item.images) - Assert.assertEquals("uk", item.language) - Assert.assertTrue(item.temperature == 24.7) - Assert.assertEquals(true, item.canBeProcessed) + assertEquals(-1, item.sku) + assertEquals("plain", item.text) + assertEquals("some image", item.images) + assertEquals("uk", item.language) + assertTrue(item.temperature == 24.7) + assertEquals(true, item.canBeProcessed) } - @Test(expected = MissingKotlinParameterException::class) + @Test fun shouldThrowExceptionWhenProvidedNullForNotNullFieldWithoutDefault() { - createMapper(true).readValue( - """{ + assertThrows { + createMapper(true).readValue( + """{ "text": null }""" - ) + ) + } } @Test @@ -160,6 +163,6 @@ class TestNullToDefault { ) val expectedResult = OuterDataClass(InnerDataClass("someString")) - Assert.assertEquals(expectedResult, outerDataClassInstance) + assertEquals(expectedResult, outerDataClassInstance) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyCollectionTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyCollectionTest.kt index 1edc40976..2c81dd2ab 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyCollectionTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyCollectionTest.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyCollection import com.fasterxml.jackson.module.kotlin.kotlinModule -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestNullToEmptyCollection { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyMapTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyMapTest.kt index fa1d09a14..58e6d061e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyMapTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToEmptyMapTest.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyMap import com.fasterxml.jackson.module.kotlin.kotlinModule -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestNullToEmptyMap { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ObjectSingletonTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ObjectSingletonTest.kt index d534ad534..d5f10b3f3 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ObjectSingletonTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ObjectSingletonTest.kt @@ -4,9 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature.SingletonSupport import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test // [module-kotlin#225]: keep Kotlin singletons as singletons class TestObjectSingleton { @@ -22,7 +21,7 @@ class TestObjectSingleton { val js = mapper.writeValueAsString(Singleton) val newSingleton = mapper.readValue(js) - assertThat(newSingleton, equalTo(Singleton)) + assertEquals(Singleton, newSingleton) } @Test @@ -34,22 +33,22 @@ class TestObjectSingleton { // mutate the in-memory singleton state val after = initial + 1 Singleton.content = after - assertThat(Singleton.content, equalTo(after)) + assertEquals(Singleton.content, after) // read back persisted state resets singleton state val newSingleton = mapper.readValue(js) - assertThat(newSingleton.content, equalTo(initial)) - assertThat(Singleton.content, equalTo(initial)) + assertEquals(initial, Singleton.content) + assertEquals(initial, newSingleton.content) } @Test fun deserializedObjectsBehaveLikeSingletons() { val js = mapper.writeValueAsString(Singleton) val newSingleton = mapper.readValue(js) - assertThat(newSingleton.content, equalTo(Singleton.content)) + assertEquals(Singleton.content, newSingleton.content) newSingleton.content += 1 - assertThat(Singleton.content, equalTo(newSingleton.content)) + assertEquals(Singleton.content, newSingleton.content) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt index 0b20c6dac..f16ee7161 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt @@ -6,9 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.PropertyNamingStrategies import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.module.kotlin.* -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Test import java.io.StringWriter import java.util.* import kotlin.properties.Delegates @@ -35,12 +33,12 @@ class TestJacksonWithKotlin { createDtField: Date = createdDt, isNameField: Boolean = isName, ) { - assertThat(nameField, equalTo("Frank")) - assertThat(ageField, equalTo(30)) - assertThat(addressField, equalTo("something here")) - assertThat(wrongNameField, equalTo(true)) - assertThat(createDtField, equalTo(Date(1477419948000))) - assertThat(isNameField, equalTo(false)) + assertEquals("Frank", nameField) + assertEquals(30, ageField) + assertEquals("something here", addressField) + assertEquals(true, wrongNameField) + assertEquals(Date(1477419948000), createDtField) + assertFalse(isNameField) } } @@ -117,7 +115,7 @@ class TestJacksonWithKotlin { val test1out = StringWriter() normalCasedMapper.writeValue(test1out, stateObj) - assertThat(test1out.getBuffer().toString(), equalTo(normalCasedJson)) + assertEquals(normalCasedJson, test1out.getBuffer().toString()) } // ================== @@ -138,7 +136,7 @@ class TestJacksonWithKotlin { stateObj.validate() val test1out = normalCasedMapper.writeValueAsString(stateObj) - assertThat(test1out, equalTo(normalCasedJson)) + assertEquals(normalCasedJson, test1out) } // ================== @@ -206,7 +204,7 @@ class TestJacksonWithKotlin { stateObj.validate() val test1out = pascalCasedMapper.writeValueAsString(stateObj) - assertThat(test1out, equalTo(pascalCasedJson)) + assertEquals(pascalCasedJson, test1out) } private class HasSameParamNameConstructor(val value: Int) { @@ -250,7 +248,7 @@ class TestJacksonWithKotlin { @Test fun findingFactoryMethod() { val stateObj = normalCasedMapper.readValue(normalCasedJson, StateObjectWithFactory::class.java) stateObj.validate() - assertThat(stateObj.factoryUsed, equalTo(true)) + assertEquals(true, stateObj.factoryUsed) } private class StateObjectWithFactoryNoParamAnnotations( @@ -278,8 +276,7 @@ class TestJacksonWithKotlin { @Test fun findingFactoryMethod2() { try { normalCasedMapper.readValue(normalCasedJson, StateObjectWithFactoryNoParamAnnotations::class.java) - } - catch (ex: Exception) { + } catch (ex: Exception) { ex.printStackTrace() fail("Exception not expected") } @@ -313,7 +310,7 @@ class TestJacksonWithKotlin { @Test fun findingFactoryMethod3() { val stateObj = normalCasedMapper.readValue(normalCasedJson, StateObjectWithFactoryOnNamedCompanion::class.java) stateObj.validate() - assertThat(stateObj.factoryUsed, equalTo(true)) + assertTrue(stateObj.factoryUsed) } // GH #14 failing due to this enum type @@ -346,4 +343,3 @@ class TestJacksonWithKotlin { assertTrue(Gh14FailureWithEnum::class.java.isKotlinClass()) } } - diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/PropertyRequirednessTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/PropertyRequirednessTests.kt index 492609f08..ded39ce95 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/PropertyRequirednessTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/PropertyRequirednessTests.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.BeanDescription import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertFalse import kotlin.test.assertTrue diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SealedClassTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SealedClassTest.kt index 97535cfc0..7f482af6d 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SealedClassTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SealedClassTest.kt @@ -7,7 +7,7 @@ import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.test.SealedClassTest.SuperClass.B -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertTrue diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SequenceSerdesTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SequenceSerdesTests.kt index 2f9245c37..d64016bdd 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SequenceSerdesTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SequenceSerdesTests.kt @@ -6,7 +6,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.ser.std.StdSerializer import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTest.kt index 413e046cd..2681e8464 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTest.kt @@ -1,18 +1,19 @@ package com.fasterxml.jackson.module.kotlin.test import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks -import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException +import com.fasterxml.jackson.databind.exc.InvalidNullException +import com.fasterxml.jackson.module.kotlin.KotlinFeature.NewStrictNullChecks import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Ignore -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertArrayEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import kotlin.test.assertNull class StrictNullChecksTest { - private val mapper = ObjectMapper().registerModule(kotlinModule { enable(StrictNullChecks) }) + private val mapper = ObjectMapper().registerModule(kotlinModule { enable(NewStrictNullChecks) }) /** collection tests */ @@ -22,15 +23,17 @@ class StrictNullChecksTest { fun testListOfNullableInt() { val json = """{"samples":[1, null]}""" val stateObj = mapper.readValue(json) - assertThat(stateObj.samples, equalTo(listOf(1, null))) + assertEquals(listOf(1, null), stateObj.samples) } private data class ClassWithListOfInt(val samples: List) - @Test(expected = MissingKotlinParameterException::class) + @Test fun testListOfInt() { - val json = """{"samples":[1, null]}""" - mapper.readValue(json) + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue(json) + } } private data class ClassWithNullableListOfInt(val samples: List?) @@ -50,15 +53,17 @@ class StrictNullChecksTest { fun testArrayOfNullableInt() { val json = """{"samples":[1, null]}""" val stateObj = mapper.readValue(json) - assertThat(stateObj.samples, equalTo(arrayOf(1, null))) + assertArrayEquals(arrayOf(1, null), stateObj.samples) } private data class ClassWithArrayOfInt(val samples: Array) - @Test(expected = MissingKotlinParameterException::class) + @Test fun testArrayOfInt() { - val json = """{"samples":[1, null]}""" - mapper.readValue(json) + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue(json) + } } private data class ClassWithNullableArrayOfInt(val samples: Array?) @@ -78,15 +83,17 @@ class StrictNullChecksTest { fun testMapOfStringToNullableInt() { val json = """{ "samples": { "key": null } }""" val stateObj = mapper.readValue(json) - assertThat(stateObj.samples, equalTo(mapOf("key" to null))) + assertEquals(mapOf("key" to null), stateObj.samples) } private data class ClassWithMapOfStringToInt(val samples: Map) - @Test(expected = MissingKotlinParameterException::class) + @Test fun testMapOfStringToIntWithNullValue() { - val json = """{ "samples": { "key": null } }""" - mapper.readValue(json) + assertThrows { + val json = """{ "samples": { "key": null } }""" + mapper.readValue(json) + } } private data class ClassWithNullableMapOfStringToInt(val samples: Map?) @@ -106,41 +113,47 @@ class StrictNullChecksTest { fun testListOfGeneric() { val json = """{"samples":[1, 2]}""" val stateObj = mapper.readValue>>(json) - assertThat(stateObj.samples, equalTo(listOf(1, 2))) + assertEquals(listOf(1, 2), stateObj.samples) } - @Ignore // this is a hard problem to solve and is currently not addressed - @Test(expected = MissingKotlinParameterException::class) + @Disabled // this is a hard problem to solve and is currently not addressed + @Test fun testListOfGenericWithNullValue() { - val json = """{"samples":[1, null]}""" - mapper.readValue>>(json) + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue>>(json) + } } @Test fun testMapOfGeneric() { val json = """{ "samples": { "key": 1 } }""" val stateObj = mapper.readValue>>(json) - assertThat(stateObj.samples, equalTo(mapOf("key" to 1))) + assertEquals(mapOf("key" to 1), stateObj.samples) } - @Ignore // this is a hard problem to solve and is currently not addressed - @Test(expected = MissingKotlinParameterException::class) + @Disabled // this is a hard problem to solve and is currently not addressed + @Test fun testMapOfGenericWithNullValue() { - val json = """{ "samples": { "key": null } }""" - mapper.readValue>>(json) + assertThrows { + val json = """{ "samples": { "key": null } }""" + mapper.readValue>>(json) + } } @Test fun testArrayOfGeneric() { val json = """{"samples":[1, 2]}""" val stateObj = mapper.readValue>>(json) - assertThat(stateObj.samples, equalTo(arrayOf(1, 2))) + assertArrayEquals(arrayOf(1, 2), stateObj.samples) } - @Ignore // this is a hard problem to solve and is currently not addressed - @Test(expected = MissingKotlinParameterException::class) + @Disabled // this is a hard problem to solve and is currently not addressed + @Test fun testArrayOfGenericWithNullValue() { - val json = """{"samples":[1, null]}""" - mapper.readValue>>(json) + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue>>(json) + } } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTestOld.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTestOld.kt new file mode 100644 index 000000000..ec82ee926 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/StrictNullChecksTestOld.kt @@ -0,0 +1,161 @@ +package com.fasterxml.jackson.module.kotlin.test + +import com.fasterxml.jackson.module.kotlin.KotlinFeature +import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Assertions.assertArrayEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import kotlin.test.assertNull + +class StrictNullChecksTestOld { + private val mapper = jacksonObjectMapper { + disable(KotlinFeature.NewStrictNullChecks) + enable(KotlinFeature.StrictNullChecks) + } + + /** collection tests */ + + private data class ClassWithListOfNullableInt(val samples: List) + + @Test + fun testListOfNullableInt() { + val json = """{"samples":[1, null]}""" + val stateObj = mapper.readValue(json) + assertEquals(listOf(1, null), stateObj.samples) + } + + private data class ClassWithListOfInt(val samples: List) + + @Test + fun testListOfInt() { + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue(json) + } + } + + private data class ClassWithNullableListOfInt(val samples: List?) + + @Test + fun testNullableListOfInt() { + val json = """{"samples": null}""" + val stateObj = mapper.readValue(json) + assertNull(stateObj.samples) + } + + /** array tests */ + + private data class ClassWithArrayOfNullableInt(val samples: Array) + + @Test + fun testArrayOfNullableInt() { + val json = """{"samples":[1, null]}""" + val stateObj = mapper.readValue(json) + assertArrayEquals(arrayOf(1, null), stateObj.samples) + } + + private data class ClassWithArrayOfInt(val samples: Array) + + @Test + fun testArrayOfInt() { + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue(json) + } + } + + private data class ClassWithNullableArrayOfInt(val samples: Array?) + + @Test + fun testNullableArrayOfInt() { + val json = """{"samples": null}""" + val stateObj = mapper.readValue(json) + assertNull(stateObj.samples) + } + + /** map tests */ + + private data class ClassWithMapOfStringToNullableInt(val samples: Map) + + @Test + fun testMapOfStringToNullableInt() { + val json = """{ "samples": { "key": null } }""" + val stateObj = mapper.readValue(json) + assertEquals(mapOf("key" to null), stateObj.samples) + } + + private data class ClassWithMapOfStringToInt(val samples: Map) + + @Test + fun testMapOfStringToIntWithNullValue() { + assertThrows { + val json = """{ "samples": { "key": null } }""" + mapper.readValue(json) + } + } + + private data class ClassWithNullableMapOfStringToInt(val samples: Map?) + + @Test + fun testNullableMapOfStringToInt() { + val json = """{"samples": null}""" + val stateObj = mapper.readValue(json) + assertNull(stateObj.samples) + } + + /** generics test */ + + private data class TestClass(val samples: T) + + @Test + fun testListOfGeneric() { + val json = """{"samples":[1, 2]}""" + val stateObj = mapper.readValue>>(json) + assertEquals(listOf(1, 2), stateObj.samples) + } + + @Disabled // this is a hard problem to solve and is currently not addressed + @Test + fun testListOfGenericWithNullValue() { + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue>>(json) + } + } + + @Test + fun testMapOfGeneric() { + val json = """{ "samples": { "key": 1 } }""" + val stateObj = mapper.readValue>>(json) + assertEquals(mapOf("key" to 1), stateObj.samples) + } + + @Disabled // this is a hard problem to solve and is currently not addressed + @Test + fun testMapOfGenericWithNullValue() { + assertThrows { + val json = """{ "samples": { "key": null } }""" + mapper.readValue>>(json) + } + } + + @Test + fun testArrayOfGeneric() { + val json = """{"samples":[1, 2]}""" + val stateObj = mapper.readValue>>(json) + assertArrayEquals(arrayOf(1, 2), stateObj.samples) + } + + @Disabled // this is a hard problem to solve and is currently not addressed + @Test + fun testArrayOfGenericWithNullValue() { + assertThrows { + val json = """{"samples":[1, null]}""" + mapper.readValue>>(json) + } + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/TestHelpersTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/TestHelpersTest.kt index 1195e1d11..57c0089d3 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/TestHelpersTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/TestHelpersTest.kt @@ -1,7 +1,7 @@ package com.fasterxml.jackson.module.kotlin.test -import org.junit.Assert.assertThrows -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestHelpersTest { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersOnKeyTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersOnKeyTest.kt index 186067fb9..28b038d62 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersOnKeyTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersOnKeyTest.kt @@ -3,8 +3,8 @@ package com.fasterxml.jackson.module.kotlin.test import com.fasterxml.jackson.core.exc.InputCoercionException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertThrows -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test import java.math.BigInteger import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersTests.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersTests.kt index 845e6196c..ed48991d1 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersTests.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersTests.kt @@ -4,11 +4,10 @@ import com.fasterxml.jackson.core.exc.InputCoercionException import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import java.math.BigInteger -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Assert.assertThrows +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.assertThrows internal class UnsignedNumbersTests { @@ -18,76 +17,76 @@ internal class UnsignedNumbersTests { fun `test UByte`() { val json = mapper.writeValueAsString(UByte.MAX_VALUE) val deserialized = mapper.readValue(json) - assertThat(deserialized, equalTo(UByte.MAX_VALUE)) + assertEquals(UByte.MAX_VALUE, deserialized) } @Test fun `test UByte overflow`() { val json = mapper.writeValueAsString(UByte.MAX_VALUE + 1u) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test UByte underflow`() { val json = mapper.writeValueAsString(-1) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test UShort`() { val json = mapper.writeValueAsString(UShort.MAX_VALUE) val deserialized = mapper.readValue(json) - assertThat(deserialized, equalTo(UShort.MAX_VALUE)) + assertEquals(UShort.MAX_VALUE, deserialized) } @Test fun `test UShort overflow`() { val json = mapper.writeValueAsString(UShort.MAX_VALUE + 1u) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test UShort underflow`() { val json = mapper.writeValueAsString(-1) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test UInt`() { val json = mapper.writeValueAsString(UInt.MAX_VALUE) val deserialized = mapper.readValue(json) - assertThat(deserialized, equalTo(UInt.MAX_VALUE)) + assertEquals(UInt.MAX_VALUE, deserialized) } @Test fun `test UInt overflow`() { val json = mapper.writeValueAsString(UInt.MAX_VALUE.toULong() + 1u) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test UInt underflow`() { val json = mapper.writeValueAsString(-1) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test ULong`() { val json = mapper.writeValueAsString(ULong.MAX_VALUE) val deserialized = mapper.readValue(json) - assertThat(deserialized, equalTo(ULong.MAX_VALUE)) + assertEquals(ULong.MAX_VALUE, deserialized) } @Test fun `test ULong overflow`() { val value = BigInteger(ULong.MAX_VALUE.toString()) + BigInteger.ONE val json = mapper.writeValueAsString(value) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } @Test fun `test ULong underflow`() { val json = mapper.writeValueAsString(-1) - assertThrows(InputCoercionException::class.java) { mapper.readValue(json) } + assertThrows { mapper.readValue(json) } } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/VarargDeserTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/VarargDeserTest.kt index 0e9c067ba..cf6fb24c3 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/VarargDeserTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/VarargDeserTest.kt @@ -2,25 +2,19 @@ package com.fasterxml.jackson.module.kotlin.test import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import junit.framework.TestCase.assertEquals -import junit.framework.TestCase.assertTrue -import org.junit.Ignore -import org.junit.experimental.runners.Enclosed -import org.junit.runner.RunWith -import kotlin.test.Test - -// from https://github.com/ProjectMapK/jackson-module-kogera/blob/0631cd3b07c7fb6971a00ac1f6811b4367a1720e/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zIntegration/deser/VarargTest.kt#L1 -@RunWith(Enclosed::class) + import org.junit.jupiter.api.Assertions.assertEquals + import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +// from https://github.com/ProjectMapK/jackson-module-kogera/blob/7872116052c9a4744c6d4e84ddd5cab6bb525024/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zIntegration/deser/VarargTest.kt class VarargDeserTest { - @Ignore - companion object { - val mapper = jacksonObjectMapper() - } + val mapper = jacksonObjectMapper() - @Ignore class OnlyVararg(vararg val v: Int) - class OnlyVarargTest { + @Nested + inner class OnlyVarargTest { @Test fun hasArgs() { val r = mapper.readValue("""{"v":[1,2,3]}""") @@ -40,10 +34,10 @@ class VarargDeserTest { } } - @Ignore class HeadVararg(vararg val v: Int?, val i: Int) - class HeadVarargTest { + @Nested + inner class HeadVarargTest { @Test fun hasArgs() { val r = mapper.readValue("""{"i":0,"v":[1,2,null]}""") @@ -66,10 +60,10 @@ class VarargDeserTest { } } - @Ignore class TailVararg(val i: Int, vararg val v: String) - class TailVarargTest { + @Nested + inner class TailVarargTest { @Test fun hasArgs() { val r = mapper.readValue("""{"i":0,"v":["foo","bar","baz"]}""") @@ -92,10 +86,10 @@ class VarargDeserTest { } } - @Ignore class HasDefaultVararg(vararg val v: String? = arrayOf("foo", "bar")) - class HasDefaultVarargTest { + @Nested + inner class HasDefaultVarargTest { @Test fun hasArgs() { val r = mapper.readValue("""{"v":["foo","bar",null]}""") diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub314.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub314.kt new file mode 100644 index 000000000..c01ef0c84 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub314.kt @@ -0,0 +1,29 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.databind.MapperFeature +import com.fasterxml.jackson.module.kotlin.jsonMapper +import com.fasterxml.jackson.module.kotlin.kotlinModule +import kotlin.test.Test +import kotlin.test.assertEquals + +class GitHub314 { + // Since Nothing? is compiled as a Void, it can be serialized by specifying ALLOW_VOID_VALUED_PROPERTIES + data object NothingData { + val data: Nothing? = null + } + + @Test + fun test() { + val expected = """{"data":null}""" + + val withoutKotlinModule = jsonMapper { enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES) } + assertEquals(expected, withoutKotlinModule.writeValueAsString(NothingData)) + + val withKotlinModule = jsonMapper { + enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES) + addModule(kotlinModule()) + } + + assertEquals(expected, withKotlinModule.writeValueAsString(NothingData)) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub338.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub338.kt new file mode 100644 index 000000000..1f513c471 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub338.kt @@ -0,0 +1,86 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.annotation.JsonCreator +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.dataformat.xml.XmlMapper +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement +import com.fasterxml.jackson.module.kotlin.readValue +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class GitHub338 { + data class Properties( + @JacksonXmlProperty(localName = "NEW_DATE") + val newDate: String?, + @JacksonXmlProperty(localName = "BC_1MONTH") + val oneMonth: String? + ) + + data class Content( + val properties: Properties + ) + + data class Entry( + val id: String, + val updated: String, + val content: Content + ) + + @JacksonXmlRootElement(namespace = "http://www.w3.org/2005/Atom", localName = "feed") + data class Feed @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) constructor( + @JacksonXmlElementWrapper(useWrapping = false) + val entry: List + ) + + val xml = """ + + + http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(1) + 2020-05-08T22:36:11Z + + + 1997-01-02T00:00:00 + + + + + + http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(2) + 2020-05-08T22:36:11Z + + + 1996-12-31T00:00:00 + + + + + + """.trimIndent() + + @Test + fun test() { + val mapper = XmlMapper() + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .registerKotlinModule() + val expected = Feed( + listOf( + Entry( + "http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(1)", + "2020-05-08T22:36:11Z", + Content(Properties("1997-01-02T00:00:00", "")) + ), + Entry( + "http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(2)", + "2020-05-08T22:36:11Z", + Content(Properties("1996-12-31T00:00:00", "")) + ) + ) + ) + val actual = mapper.readValue(xml) + + assertEquals(expected, actual) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub524.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub524.kt index 76b0677fb..b97f7ac80 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub524.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub524.kt @@ -8,7 +8,7 @@ import com.fasterxml.jackson.databind.ser.std.StdSerializer import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.testPrettyWriter -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals // Most of the current behavior has been tested on GitHub464, so only serializer-related behavior is tested here. diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub530.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub530.kt index a824108d1..22df63a23 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub530.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub530.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonValue import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.testPrettyWriter import kotlin.test.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Test class GitHub530 { // At the moment, the output is the same with or without `JsonValue`, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub618.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub618.kt index b099408dd..e9329d3fe 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub618.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub618.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.ser.std.StdSerializer import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class GitHub618 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub625.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub625.kt index e91827873..a03415d05 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub625.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub625.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.testPrettyWriter -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub757.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub757.kt index 558060b33..0245c9b85 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub757.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub757.kt @@ -4,14 +4,14 @@ import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.convertValue -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertNull class GitHub757 { @Test fun test() { val kotlinModule = KotlinModule.Builder() - .enable(KotlinFeature.StrictNullChecks) + .enable(KotlinFeature.NewStrictNullChecks) .build() val mapper = JsonMapper.builder() .addModule(kotlinModule) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub800.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub800.kt new file mode 100644 index 000000000..097e076dd --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub800.kt @@ -0,0 +1,103 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.ObjectReader +import com.fasterxml.jackson.databind.module.SimpleModule +import com.fasterxml.jackson.dataformat.csv.CsvMapper +import com.fasterxml.jackson.dataformat.csv.CsvParser +import com.fasterxml.jackson.dataformat.csv.CsvSchema +import com.fasterxml.jackson.module.kotlin.kotlinModule +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class GitHub800 { + val CSV = """ + BAR_HEADER,BAZ_HEADER + bar1,baz1 + """.trimIndent() + + val MAPPER = + CsvMapper + .builder() + .addModule( + SimpleModule() + .addDeserializer(BazValueClass::class.java, object : JsonDeserializer() { + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = BazValueClass(p.valueAsString) + }) + .addDeserializer(BazDataClass::class.java, object : JsonDeserializer() { + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = BazDataClass(p.valueAsString) + }) + ) + .addModule(kotlinModule()) + .build() + + inline fun createSchema(columnReordering: Boolean): CsvSchema = + MAPPER + .schemaFor(T::class.java) + .withColumnReordering(columnReordering) + .withHeader() + + inline fun createReader(columnReordering: Boolean): ObjectReader = + MAPPER + .readerFor(T::class.java) + .with(CsvParser.Feature.WRAP_AS_ARRAY) + .with(createSchema(columnReordering)) + + data class FooWithValueClass( + @field:JsonProperty("BAR_HEADER") + val bar: String?, + @field:JsonProperty("BAZ_HEADER") + val baz: BazValueClass?, + ) + + data class FooWithDataClass( + @field:JsonProperty("BAR_HEADER") + val bar: String?, + @field:JsonProperty("BAZ_HEADER") + val baz: BazDataClass?, + ) + + @JvmInline + value class BazValueClass(val value: String) + + data class BazDataClass(val value: String) + + @Test + fun valueClassWithoutColumnReordering() { + val actual = createReader(false) + .readValues(CSV) + .readAll() + + assertEquals(listOf(FooWithValueClass("bar1", BazValueClass("baz1"))), actual) + } + + @Test + fun valueClassWithColumnReordering() { + val actual = createReader(true) + .readValues(CSV) + .readAll() + + assertEquals(listOf(FooWithValueClass("bar1", BazValueClass("baz1"))), actual) + } + + @Test + fun dataClassWithoutColumnReordering() { + val actual = createReader(false) + .readValues(CSV) + .readAll() + + assertEquals(listOf(FooWithDataClass("bar1", BazDataClass("baz1"))), actual) + } + + @Test + fun dataClassWithColumnReordering() { + val actual = createReader(true) + .readValues(CSV) + .readAll() + + assertEquals(listOf(FooWithDataClass("bar1", BazDataClass("baz1"))), actual) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub832.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub832.kt index 542cc9061..a325b22ed 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub832.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub832.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonAnySetter import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class GitHub832 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub841.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub841.kt index 0bb0ec512..25951b057 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub841.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub841.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.registerKotlinModule -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class GitHub841 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub844.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub844.kt index 7ee202d67..dbaa4c744 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub844.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub844.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.registerKotlinModule -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "_type") diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub876.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub876.kt new file mode 100644 index 000000000..d3c419426 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub876.kt @@ -0,0 +1,153 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.annotation.JsonSetter +import com.fasterxml.jackson.annotation.Nulls +import com.fasterxml.jackson.module.kotlin.configOverride +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class GitHub876 { + data class WithAnnotationWithoutDefault( + @JsonSetter(nulls = Nulls.AS_EMPTY) + val list: List, + @JsonSetter(nulls = Nulls.AS_EMPTY) + val map: Map, + @JsonSetter(nulls = Nulls.AS_EMPTY) + val string: String + ) + + @Nested + inner class WithAnnotationWithoutDefaultTest { + val mapper = jacksonObjectMapper() + + @Test + fun nullInput() { + val input = """{"list": null, "map": null, "string": null}""" + val expected = WithAnnotationWithoutDefault(emptyList(), emptyMap(), "") + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + + @Test + fun undefinedInput() { + val input = """{}""" + val expected = WithAnnotationWithoutDefault(emptyList(), emptyMap(), "") + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + } + + data class WithAnnotationWithDefault( + @JsonSetter(nulls = Nulls.AS_EMPTY) + val list: List = listOf("default"), + @JsonSetter(nulls = Nulls.AS_EMPTY) + val map: Map = mapOf("default" to "default"), + @JsonSetter(nulls = Nulls.AS_EMPTY) + val string: String = "default" + ) + + @Nested + inner class WithAnnotationWithDefaultTest { + val mapper = jacksonObjectMapper() + + @Test + fun nullInput() { + // If null is explicitly specified, the default value is not used + val input = """{"list": null, "map": null, "string": null}""" + val expected = WithAnnotationWithDefault(emptyList(), emptyMap(), "") + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + + @Test + fun undefinedInput() { + // If the input is undefined, the default value is used + val input = """{}""" + val expected = WithAnnotationWithDefault() + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + } + + // If it is set by configOverride, it is treated in the same way as if it were set by annotation + data class WithoutAnnotationWithoutDefault( + val list: List, + val map: Map, + val string: String + ) + + @Nested + inner class WithoutAnnotationWithoutDefaultTest { + val mapper = jacksonObjectMapper().apply { + configOverride>().setterInfo = JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) + configOverride>().setterInfo = JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) + configOverride().setterInfo = JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) + } + + @Test + fun nullInput() { + val input = """{"list": null, "map": null, "string": null}""" + val expected = WithoutAnnotationWithoutDefault(emptyList(), emptyMap(), "") + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + + @Test + fun undefinedInput() { + val input = """{}""" + val expected = WithoutAnnotationWithoutDefault(emptyList(), emptyMap(), "") + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + } + + data class WithoutAnnotationWithDefault( + val list: List = listOf("default"), + val map: Map = mapOf("default" to "default"), + val string: String = "default" + ) + + @Nested + inner class WithoutAnnotationWithDefaultTest { + val mapper = jacksonObjectMapper().apply { + configOverride>().setterInfo = JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) + configOverride>().setterInfo = JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) + configOverride().setterInfo = JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) + } + + @Test + fun nullInput() { + val input = """{"list": null, "map": null, "string": null}""" + val expected = WithoutAnnotationWithDefault(emptyList(), emptyMap(), "") + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + + @Test + fun undefinedInput() { + val input = """{}""" + val expected = WithoutAnnotationWithDefault() + + val actual = mapper.readValue(input) + + assertEquals(expected, actual) + } + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub917.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub917.kt new file mode 100644 index 000000000..30e46e702 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub917.kt @@ -0,0 +1,39 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.annotation.JsonInclude +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.OptBoolean +import com.fasterxml.jackson.databind.exc.InvalidNullException +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import kotlin.test.assertEquals + +class GitHub917 { + data class Failing(val data: T) + + val mapper = jacksonObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL) + + @Test + fun failing() { + val value = Failing(null) + val json = mapper.writeValueAsString(value) + + assertThrows { + val deserializedValue = mapper.readValue>(json) + assertEquals(value ,deserializedValue) + } + } + + data class WorkAround(@JsonProperty(isRequired = OptBoolean.FALSE) val data: T) + + @Test + fun workAround() { + val value = WorkAround(null) + val json = mapper.writeValueAsString(value) + + val deserializedValue = mapper.readValue>(json) + assertEquals(value ,deserializedValue) + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub922.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub922.kt index 9150504bf..472fcf842 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub922.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub922.kt @@ -1,13 +1,25 @@ package com.fasterxml.jackson.module.kotlin.test.github +import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.OptBoolean import com.fasterxml.jackson.databind.BeanDescription import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature +import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import kotlin.reflect.full.memberProperties import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertTrue class GitHub922 { + companion object { + val nullToEmptyMapper = jacksonObjectMapper { + enable(KotlinFeature.NullToEmptyCollection) + enable(KotlinFeature.NullToEmptyMap) + } + } + private inline fun ObjectMapper.introspectSerialization(): BeanDescription = serializationConfig.introspect(serializationConfig.constructType(T::class.java)) @@ -19,14 +31,145 @@ class GitHub922 { @Test fun `nullToEmpty does not override specification by Java annotation`() { - val mapper = jacksonObjectMapper { - enable(KotlinFeature.NullToEmptyCollection) - enable(KotlinFeature.NullToEmptyMap) - } + val defaultDesc = defaultMapper.introspectDeserialization() + + assertTrue(defaultDesc.isRequired("list")) + assertTrue(defaultDesc.isRequired("map")) + + val nullToEmptyDesc = nullToEmptyMapper.introspectDeserialization() + + assertTrue(nullToEmptyDesc.isRequired("list")) + assertTrue(nullToEmptyDesc.isRequired("map")) + } + + data class RequiredCollectionsDto1( + @JsonProperty(required = true) val list: List, + @JsonProperty(required = true) val map: Map + ) - val desc = mapper.introspectDeserialization() + data class RequiredCollectionsDto2( + @JsonProperty(isRequired = OptBoolean.TRUE) val list: List, + @JsonProperty(isRequired = OptBoolean.TRUE) val map: Map + ) + + @Test + fun `nullToEmpty does not override specification by annotation`() { + val defaultDesc1 = defaultMapper.introspectDeserialization() + + assertTrue(defaultDesc1.isRequired("list")) + assertTrue(defaultDesc1.isRequired("map")) + + val nullToEmptyDesc1 = nullToEmptyMapper.introspectDeserialization() + + assertTrue(nullToEmptyDesc1.isRequired("list")) + assertTrue(nullToEmptyDesc1.isRequired("map")) + + val defaultDesc2 = defaultMapper.introspectDeserialization() + + assertTrue(defaultDesc2.isRequired("list")) + assertTrue(defaultDesc2.isRequired("map")) + + val nullToEmptyDesc2 = nullToEmptyMapper.introspectDeserialization() + + assertTrue(nullToEmptyDesc2.isRequired("list")) + assertTrue(nullToEmptyDesc2.isRequired("map")) + } + + data class CollectionsDto(val list: List, val map: Map) + + @Test + fun `nullToEmpty does not affect for serialization`() { + val defaultDesc = defaultMapper.introspectSerialization() - assertTrue(desc.isRequired("list")) - assertTrue(desc.isRequired("map")) + assertTrue(defaultDesc.isRequired("list")) + assertTrue(defaultDesc.isRequired("map")) + + val nullToEmptyDesc = nullToEmptyMapper.introspectSerialization() + + assertTrue(nullToEmptyDesc.isRequired("list")) + assertTrue(nullToEmptyDesc.isRequired("map")) + } + + class SetterCollectionsDto { + lateinit var list: List + lateinit var map: Map + } + + @Test + fun `nullToEmpty does not affect for setter`() { + val defaultDesc = defaultMapper.introspectDeserialization() + + assertTrue(defaultDesc.isRequired("list")) + assertTrue(defaultDesc.isRequired("map")) + + val nullToEmptyDesc = nullToEmptyMapper.introspectDeserialization() + + assertTrue(nullToEmptyDesc.isRequired("list")) + assertTrue(nullToEmptyDesc.isRequired("map")) + } + + class FieldCollectionsDto { + @JvmField + var list: List = emptyList() + @JvmField + var map: Map = emptyMap() + } + + @Test + fun `nullToEmpty does not affect for field`() { + val defaultDesc = defaultMapper.introspectDeserialization() + + assertTrue(defaultDesc.isRequired("list")) + assertTrue(defaultDesc.isRequired("map")) + + val nullToEmptyDesc = nullToEmptyMapper.introspectDeserialization() + + assertTrue(nullToEmptyDesc.isRequired("list")) + assertTrue(nullToEmptyDesc.isRequired("map")) + } + + // isRequired_required_nullability_expected + @Suppress("PropertyName") + data class IsRequiredDto( + // region: isRequired takes precedence + @JsonProperty(isRequired = OptBoolean.FALSE, required = false) + val FALSE_false_nullable_false: String?, + @JsonProperty(isRequired = OptBoolean.FALSE, required = false) + val FALSE_false_nonNull_false: String, + @JsonProperty(isRequired = OptBoolean.FALSE, required = true) + val FALSE_true_nullable_false: String?, + @JsonProperty(isRequired = OptBoolean.FALSE, required = true) + val FALSE_true_nonNull_false: String, + @JsonProperty(isRequired = OptBoolean.TRUE, required = false) + val TRUE_false_nullable_true: String?, + @JsonProperty(isRequired = OptBoolean.TRUE, required = false) + val TRUE_false_nonNull_true: String, + @JsonProperty(isRequired = OptBoolean.TRUE, required = true) + val TRUE_true_nullable_true: String?, + @JsonProperty(isRequired = OptBoolean.TRUE, required = true) + val TRUE_true_nonNull_true: String, + // endregion + // region: If isRequired is the default, only overrides by required = true will work. + @JsonProperty(isRequired = OptBoolean.DEFAULT, required = false) + val DEFAULT_false_nullable_false: String?, + @JsonProperty(isRequired = OptBoolean.DEFAULT, required = false) + val DEFAULT_false_nonNull_true: String, + @JsonProperty(isRequired = OptBoolean.DEFAULT, required = true) + val DEFAULT_true_nullable_true: String?, + @JsonProperty(isRequired = OptBoolean.DEFAULT, required = true) + val DEFAULT_true_nonNull_true: String, + // endregion + ) + + @Test + fun `JsonProperty properly overrides required`() { + val desc = defaultMapper.introspectDeserialization() + + IsRequiredDto::class.memberProperties.forEach { prop -> + val name = prop.name + val expected = name.split("_").last().toBoolean() + + assertEquals(expected, desc.isRequired(name), name) + } } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub976.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub976.kt new file mode 100644 index 000000000..27e2c6435 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub976.kt @@ -0,0 +1,31 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.databind.exc.InvalidNullException +import com.fasterxml.jackson.module.kotlin.KotlinFeature +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.junit.jupiter.api.assertThrows +import kotlin.test.Test + +class GitHub976 { + data class PrimitiveList(val list: List) + + @Test + fun strictNullChecks() { + val om = jacksonObjectMapper { + enable(KotlinFeature.StrictNullChecks) + } + assertThrows { + om.readValue("""{"list": [""] }""".toByteArray(), PrimitiveList::class.java) + } + } + + @Test + fun newStrictNullChecksRegression() { + val om = jacksonObjectMapper { + enable(KotlinFeature.NewStrictNullChecks) + } + assertThrows { + om.readValue("""{"list": [""] }""".toByteArray(), PrimitiveList::class.java) + } + } +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github101.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github101.kt index 772e1be79..de01e095c 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github101.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github101.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JacksonInject import com.fasterxml.jackson.databind.InjectableValues import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import java.util.UUID import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github104.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github104.kt index 9aa2184e8..4272e6d6d 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github104.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github104.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub104 { abstract class SuperClass(val name: String) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github114.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github114.kt index 70c18b6d8..a6b95a2b1 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github114.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github114.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.reflect.jvm.javaMethod import kotlin.reflect.jvm.kotlinFunction import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github120.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github120.kt index 28ce4d17c..f00c068bf 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github120.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github120.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonValue import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub120 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github124.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github124.kt index 5df56d763..5db277bf3 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github124.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github124.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub124 { // test for [module-kotlin#124]: broken in 2.9.3, fixed in 2.9.6 diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github131.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github131.kt index 1f1974794..6d73abe65 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github131.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github131.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub131 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github145.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github145.kt index d190f7341..126f20283 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github145.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github145.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test @Suppress("UNUSED_VARIABLE") class TestGithub145 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github148.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github148.kt index 20b6b0648..b4904fe8c 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github148.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github148.kt @@ -1,7 +1,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub148 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github149.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github149.kt index 2215f1a49..1c6d78f8e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github149.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github149.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonBackReference import com.fasterxml.jackson.annotation.JsonManagedReference import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test @Suppress("UNUSED_VARIABLE") class TestGithub149 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github15.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github15.kt index e5909b18d..5836adf91 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github15.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github15.kt @@ -1,7 +1,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.* -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub15 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github153.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github153.kt index 28188ee5c..14e837772 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github153.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github153.kt @@ -8,7 +8,7 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.test.expectFailure import com.fasterxml.jackson.module.kotlin.test.github.TestGithub153.MyPojo -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub153 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github155.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github155.kt index bf7751455..d82fd6b2f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github155.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github155.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub155 { data class Foo @JvmOverloads constructor( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github158.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github158.kt index 321146132..0763fefca 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github158.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github158.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub158 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github161.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github161.kt index c80b457e5..0908d1709 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github161.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github161.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.fail class TestGithub161 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github165.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github165.kt index 3b41bcf2d..ce054964e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github165.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github165.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonSetter import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github167.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github167.kt index 3cb769a7c..854d5cc47 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github167.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github167.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import java.util.function.IntSupplier diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github168.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github168.kt index e2975f004..114bba16a 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github168.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github168.kt @@ -4,7 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import kotlin.test.assertEquals class TestGithub168 { @@ -17,10 +18,12 @@ class TestGithub168 { assertEquals("whatever", obj.baz) } - @Test(expected = MissingKotlinParameterException::class) + @Test fun testIfRequiredIsReallyRequiredWhenAbsent() { - val obj = jacksonObjectMapper().readValue("""{"baz":"whatever"}""") - assertEquals("whatever", obj.baz) + assertThrows { + val obj = jacksonObjectMapper().readValue("""{"baz":"whatever"}""") + assertEquals("whatever", obj.baz) + } } @Test diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github179.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github179.kt index 45bed1758..794b925e9 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github179.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github179.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals // verifying work around for this issue, no bug present diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github180.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github180.kt index 4f159e9c0..f7d565f80 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github180.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github180.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertNull class TestGithub180 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github181.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github181.kt index 4a10d41b3..df3ad5ea8 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github181.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github181.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub181 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github194.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github194.kt index 90a093bda..85316bdc6 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github194.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github194.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonIdentityInfo import com.fasterxml.jackson.annotation.ObjectIdGenerators import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import java.util.UUID import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github196.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github196.kt index 977dfdddc..0f2a78180 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github196.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github196.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertSame /** diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github207.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github207.kt index 98d4cb1e3..71b003870 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github207.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github207.kt @@ -1,11 +1,10 @@ package com.fasterxml.jackson.module.kotlin.test.github - import com.fasterxml.jackson.annotation.JsonValue import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test class TestGithub207 { open class Wrapper(@JsonValue val value: String) { @@ -42,7 +41,7 @@ class TestGithub207 { val deserialized = objectMapper.readValue(json) // then - Assert.assertEquals(Wrapper("foo"), deserialized) + assertEquals(Wrapper("foo"), deserialized) } @Test @@ -54,7 +53,7 @@ class TestGithub207 { val serialized = objectMapper.writeValueAsString(wrapperObject) // then - Assert.assertEquals("\"foo\"", serialized) + assertEquals("\"foo\"", serialized) } @Test @@ -66,7 +65,7 @@ class TestGithub207 { val deserialized = objectMapper.readValue(json) // then - Assert.assertEquals(ExtendedWrapper("foo"), deserialized) + assertEquals(ExtendedWrapper("foo"), deserialized) } @Test @@ -78,7 +77,7 @@ class TestGithub207 { val serialized = objectMapper.writeValueAsString(wrapperObject) // then - Assert.assertEquals("\"foo\"", serialized) + assertEquals("\"foo\"", serialized) } } \ No newline at end of file diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github210.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github210.kt index 7e57f8f8c..e883840c4 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github210.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github210.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub210 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github22.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github22.kt index 91c985a04..076d8f651 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github22.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github22.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonValue import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub22 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github239.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github239.kt index 41a23b706..45ce82bf5 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github239.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github239.kt @@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.JsonTypeName import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github25.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github25.kt index dbe307b56..cc30f9d98 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github25.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github25.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonPropertyOrder import com.fasterxml.jackson.module.kotlin.* -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.properties.Delegates import kotlin.test.assertEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github26.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github26.kt index 6df848df7..3ef440c8f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github26.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github26.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals data class ClassWithPrimitivesWithDefaults(val i: Int = 5, val x: Int) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github269.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github269.kt index 4b22e35b8..fe5aceb21 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github269.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github269.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.ser.std.ToStringSerializer import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub269 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github27.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github27.kt index 8c4079bcc..c13ba6a3c 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github27.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github27.kt @@ -6,13 +6,12 @@ import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.hamcrest.CoreMatchers.equalTo -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import kotlin.test.assertTrue import kotlin.test.fail - class TestGithub27 { val mapper: ObjectMapper = jacksonObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false) @@ -21,7 +20,7 @@ class TestGithub27 { @Test fun testNullableInt() { val json = """{"sample":null}""" val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(ClassWithNullableInt(null))) + assertEquals(ClassWithNullableInt(null), stateObj) } private data class ClassWithInt(val sample: Int) @@ -29,7 +28,7 @@ class TestGithub27 { @Test fun testInt() { val json = """{"sample":null}""" val stateObj = mapper.readValue(json) - assertThat(stateObj, equalTo(ClassWithInt(0))) + assertEquals(ClassWithInt(0), stateObj) } private data class ClassWithListOfNullableInt(val samples: List) @@ -37,7 +36,7 @@ class TestGithub27 { @Test fun testListOfNullableInt() { val json = """{"samples":[1, null]}""" val stateObj = mapper.readValue(json) - assertThat(stateObj.samples, equalTo(listOf(1, null))) + assertEquals(listOf(1, null), stateObj.samples) } private data class ClassWithListOfInt(val samples: List) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github270.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github270.kt index db7011249..3c4efe9a9 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github270.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github270.kt @@ -1,7 +1,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub270 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github29.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github29.kt index e956a06be..2b4eb9f17 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github29.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github29.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub29 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt index eb58cb2a4..d649197e7 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github308.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github32.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github32.kt index 3008edbff..c0f5c016e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github32.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github32.kt @@ -1,22 +1,14 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.JsonMappingException -import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException +import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CustomTypeSafeMatcher -import org.junit.Rule -import org.junit.Test -import org.junit.rules.ExpectedException -import kotlin.reflect.KParameter - - -class TestGithub32 { - - @Rule - @JvmField - var thrown: ExpectedException = ExpectedException.none() +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +private class TestGithub32 { @Test fun `valid mandatory data class constructor param`() { jacksonObjectMapper().readValue(""" { @@ -27,74 +19,89 @@ class TestGithub32 { } @Test fun `missing mandatory data class constructor param`() { - thrown.expect(missingFirstNameParameter()) - thrown.expect(pathMatches("firstName")) - thrown.expect(location(line = 3, column = 1)) - jacksonObjectMapper().readValue(""" - { - "lastName": "Bond" + val thrown = assertThrows( + "MissingKotlinParameterException with missing `firstName` parameter" + ) { + jacksonObjectMapper().readValue(""" + { + "lastName": "Bond" + } + """.trimIndent()) } - """.trimIndent()) + + assertEquals("firstName", thrown.getHumanReadablePath()) + assertEquals(3, thrown.location?.lineNr) + assertEquals(1, thrown.location?.columnNr) } @Test fun `null mandatory data class constructor param`() { - thrown.expect(missingFirstNameParameter()) - thrown.expect(pathMatches("firstName")) - thrown.expect(location(line = 4, column = 1)) - jacksonObjectMapper().readValue(""" - { - "firstName": null, - "lastName": "Bond" + val thrown = assertThrows { + jacksonObjectMapper().readValue(""" + { + "firstName": null, + "lastName": "Bond" + } + """.trimIndent()) } - """.trimIndent()) + + assertEquals("firstName", thrown.getHumanReadablePath()) + assertEquals(4, thrown.location?.lineNr) + assertEquals(1, thrown.location?.columnNr) } @Test fun `missing mandatory constructor param - nested in class with default constructor`() { - thrown.expect(missingFirstNameParameter()) - thrown.expect(pathMatches("person.firstName")) - thrown.expect(location(line = 4, column = 5)) - jacksonObjectMapper().readValue(""" - { - "person": { - "lastName": "Bond" + val thrown = assertThrows { + jacksonObjectMapper().readValue(""" + { + "person": { + "lastName": "Bond" + } } + """.trimIndent()) } - """.trimIndent()) + + assertEquals("person.firstName", thrown.getHumanReadablePath()) + assertEquals(4, thrown.location?.lineNr) + assertEquals(5, thrown.location?.columnNr) } @Test fun `missing mandatory constructor param - nested in class with single arg constructor`() { - thrown.expect(missingFirstNameParameter()) - thrown.expect(pathMatches("person.firstName")) - thrown.expect(location(line = 4, column = 5)) - jacksonObjectMapper().readValue(""" - { - "person": { - "lastName": "Bond" + val thrown = assertThrows { + jacksonObjectMapper().readValue(""" + { + "person": { + "lastName": "Bond" + } } + """.trimIndent()) } - """.trimIndent()) + + assertEquals("person.firstName", thrown.getHumanReadablePath()) + assertEquals(4, thrown.location?.lineNr) + assertEquals(5, thrown.location?.columnNr) } @Test fun `missing mandatory constructor param - nested in class with List arg constructor`() { - thrown.expect(missingFirstNameParameter()) - thrown.expect(pathMatches("people[0].firstName")) - thrown.expect(location(line = 7, column = 9)) - jacksonObjectMapper().readValue(""" - { - "people": [ - { - "person": { - "lastName": "Bond" + val thrown = assertThrows { + jacksonObjectMapper().readValue(""" + { + "people": [ + { + "person": { + "lastName": "Bond" + } } - } - ] + ] + } + """.trimIndent()) } - """.trimIndent()) - } + assertEquals("people[0].firstName", thrown.getHumanReadablePath()) + assertEquals(7, thrown.location?.lineNr) + assertEquals(9, thrown.location?.columnNr) + } } - private data class Person(val firstName: String, val lastName: String) private data class WrapperWithArgsContructor(val person: Person) @@ -103,23 +110,6 @@ private data class WrapperWithDefaultContructor(val person: Person? = null) private data class Crowd(val people: List) - -private fun missingFirstNameParameter() = missingConstructorParam(::Person.parameters[0]) - -private fun missingConstructorParam(param: KParameter) = object : CustomTypeSafeMatcher("MissingKotlinParameterException with missing `${param.name}` parameter") { - override fun matchesSafely(e: MissingKotlinParameterException): Boolean = e.parameter.equals(param) -} - -private fun pathMatches(path: String) = object : CustomTypeSafeMatcher("MissingKotlinParameterException with path `$path`") { - override fun matchesSafely(e: MissingKotlinParameterException): Boolean = e.getHumanReadablePath().equals(path) -} - -private fun location(line: Int, column: Int) = object : CustomTypeSafeMatcher("MissingKotlinParameterException with location (line=$line, column=$column)") { - override fun matchesSafely(e: MissingKotlinParameterException): Boolean { - return e.location != null && line.equals(e.location.lineNr) && column.equals(e.location.columnNr) - } -} - private fun JsonMappingException.getHumanReadablePath(): String { val builder = StringBuilder() this.path.forEachIndexed { i, reference -> @@ -127,8 +117,8 @@ private fun JsonMappingException.getHumanReadablePath(): String { builder.append("[${reference.index}]") } else { if (i > 0) builder.append(".") - builder.append("${reference.fieldName}") + builder.append(reference.fieldName) } } return builder.toString() -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github335.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github335.kt index f5bf6fd7f..31e8dce3d 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github335.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github335.kt @@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo.As import com.fasterxml.jackson.annotation.JsonTypeInfo.Id import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class Github335Test { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github340.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github340.kt index 7d38cbd36..7d3cdb06d 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github340.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github340.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class OwnerRequestTest { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github356.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github356.kt index 885d74e48..8b3ff353e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github356.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github356.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub356 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github396.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github396.kt index 33572b9a5..4def9cc36 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github396.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github396.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.dataformat.xml.XmlMapper import com.fasterxml.jackson.module.kotlin.registerKotlinModule -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class Github396 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github42.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github42.kt index 323aea294..02a7462f4 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github42.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github42.kt @@ -4,11 +4,9 @@ import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.JsonMappingException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Rule -import org.junit.Test -import org.junit.rules.ExpectedException -import kotlin.test.assertEquals - +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows class TestGithub42_FailOnNullForPrimitives { @@ -16,10 +14,6 @@ class TestGithub42_FailOnNullForPrimitives { val mapper = jacksonObjectMapper().enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) - @Rule - @JvmField - var thrown: ExpectedException = ExpectedException.none() - @Test fun `optional primitive parameter defaulted if not in json when FAIL_ON_NULL_FOR_PRIMITIVES is true`() { val actualObj: OptionalIntRequiredBoolean = mapper.readValue(""" { @@ -31,11 +25,9 @@ class TestGithub42_FailOnNullForPrimitives { } @Test fun `Exception thrown if required primitive parameter not in json when FAIL_ON_NULL_FOR_PRIMITIVES is true`() { - thrown.expect(JsonMappingException::class.java) - - mapper.readValue(""" - {"optInt": 2} - """) + assertThrows { + mapper.readValue("""{"optInt": 2}""") + } } @Test fun `optional parameter has json value if provided when FAIL_ON_NULL_FOR_PRIMITIVES is true`() { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github46.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github46.kt index 4c08f431a..d3b01602f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github46.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github46.kt @@ -1,8 +1,8 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.* -import org.junit.Assert -import org.junit.Test +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test import kotlin.reflect.full.primaryConstructor import kotlin.test.assertEquals @@ -19,7 +19,7 @@ class TestGithub46 { val rejson = mapper.writeValueAsString(data) // then - Assert.assertEquals(json, rejson) + assertEquals(json, rejson) } @Test fun byReflectionDo32() { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github464.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github464.kt index d9cf815ee..766d9dd27 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github464.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github464.kt @@ -13,60 +13,70 @@ import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.testPrettyWriter -import org.junit.Ignore -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertNotEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class Github464 { - class UnboxTest { - object NullValueClassKeySerializer : StdSerializer(ValueClass::class.java) { - override fun serialize(value: ValueClass?, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeFieldName("null-key") - } + object NullValueClassKeySerializer : StdSerializer(ValueClass::class.java) { + override fun serialize(value: ValueClass?, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeFieldName("null-key") } + } - interface IValue - @JvmInline - value class ValueClass(val value: Int?) : IValue - data class WrapperClass(val inlineField: ValueClass) + interface IValue - abstract class AbstractGetter { - abstract val qux: T + @JvmInline + value class ValueClass(val value: Int?) : IValue + data class WrapperClass(val inlineField: ValueClass) - fun getPlugh() = qux - } - interface IGetter { - val quux: T + abstract class AbstractGetter { + abstract val qux: T + + fun getPlugh() = qux + } + interface IGetter { + val quux: T - fun getXyzzy() = quux + fun getXyzzy() = quux + } + + @JsonPropertyOrder( + "foo", + "bar", + "baz", + "qux", + "quux", + "corge", + "grault", + "garply", + "waldo", + "fred", + "plugh", + "xyzzy" + ) + class Poko( + val foo: ValueClass, + val bar: ValueClass?, + val baz: IValue, + override val qux: ValueClass, + override val quux: ValueClass, + val corge: Collection, + val grault: Array, + val garply: WrapperClass, + val waldo: WrapperClass?, + val fred: Map + ) : AbstractGetter(), IGetter + + object NullValueSerializer : StdSerializer(Any::class.java) { + override fun serialize(value: Any?, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeString("null-value") } + } - @JsonPropertyOrder( - "foo", - "bar", - "baz", - "qux", - "quux", - "corge", - "grault", - "garply", - "waldo", - "fred", - "plugh", - "xyzzy" - ) - class Poko( - val foo: ValueClass, - val bar: ValueClass?, - val baz: IValue, - override val qux: ValueClass, - override val quux: ValueClass, - val corge: Collection, - val grault: Array, - val garply: WrapperClass, - val waldo: WrapperClass?, - val fred: Map - ) : AbstractGetter(), IGetter + @Nested + inner class UnboxTest { private val zeroValue = ValueClass(0) private val oneValue = ValueClass(1) @@ -119,12 +129,6 @@ class Github464 { ) } - object NullValueSerializer : StdSerializer(Any::class.java) { - override fun serialize(value: Any?, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeString("null-value") - } - } - @Test fun nullValueSerializerTest() { @Suppress("UNCHECKED_CAST") @@ -162,20 +166,22 @@ class Github464 { } } - class SerializerPriorityTest { - @JvmInline - value class ValueBySerializer(val value: Int) + @JvmInline + value class ValueBySerializer(val value: Int) - object Serializer : StdSerializer(ValueBySerializer::class.java) { - override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeString(value.value.toString()) - } + object Serializer : StdSerializer(ValueBySerializer::class.java) { + override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeString(value.value.toString()) } - object KeySerializer : StdSerializer(ValueBySerializer::class.java) { - override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeFieldName(value.value.toString()) - } + } + object KeySerializer : StdSerializer(ValueBySerializer::class.java) { + override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeFieldName(value.value.toString()) } + } + + @Nested + inner class SerializerPriorityTest { private val target = mapOf(ValueBySerializer(1) to ValueBySerializer(2)) private val sm = SimpleModule() @@ -192,7 +198,6 @@ class Github464 { // Currently, there is a situation where the serialization results are different depending on the registration order of the modules. // This problem is not addressed because the serializer registered by the user has priority over Extensions.kt, // since KotlinModule is basically registered first. - @Ignore @Test fun priorityTest() { val km = KotlinModule.Builder().build() @@ -201,7 +206,7 @@ class Github464 { // om1(collect) -> """{"1":"2"}""" // om2(broken) -> """{"1":2}""" - assertEquals(om1.writeValueAsString(target), om2.writeValueAsString(target)) + assertNotEquals(om1.writeValueAsString(target), om2.writeValueAsString(target)) } } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github47.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github47.kt index 61ecb5ef9..61f5488ba 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github47.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github47.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.MapperFeature import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub47 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt index 38fe26109..fbba60d7e 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt @@ -4,9 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.NullNode import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.hamcrest.CoreMatchers -import org.hamcrest.MatcherAssert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Test class TestGithub490 { val mapper = jacksonObjectMapper() @@ -18,55 +18,45 @@ class TestGithub490 { @Test fun testKotlinDeserialization_intValue() { - assertThat( - "Nullable missing Int value should be deserialized as null", - value.intValue, - CoreMatchers.nullValue() - ) + assertNull(value.intValue, "Nullable missing Int value should be deserialized as null") } @Test fun testKotlinDeserialization_stringValue() { - assertThat( - "Nullable missing String value should be deserialized as null", - value.stringValue, - CoreMatchers.nullValue() - ) + assertNull(value.stringValue, "Nullable missing String value should be deserialized as null") } @Test fun testKotlinDeserialization_jsonNodeValue() { - assertThat( - "Nullable missing JsonNode value should be deserialized as null and not as NullNode", + assertNull( value.jsonNodeValue, - CoreMatchers.nullValue() + "Nullable missing JsonNode value should be deserialized as null and not as NullNode" ) } @Test fun testKotlinDeserialization_jsonNodeValueProvidedNull() { - assertThat( - "Nullable JsonNode value provided as null should be deserialized as NullNode", + assertEquals( + NullNode.instance, value.jsonNodeValueProvidedNull, - CoreMatchers.equalTo(NullNode.instance) + "Nullable JsonNode value provided as null should be deserialized as NullNode" ) } @Test fun testKotlinDeserialization_jsonNodeValueWithNullAsDefault() { - assertThat( - "Nullable by default missing JsonNode value should be deserialized as null and not as NullNode", + assertNull( value.jsonNodeValueWithNullAsDefault, - CoreMatchers.nullValue() + "Nullable by default missing JsonNode value should be deserialized as null and not as NullNode" ) } @Test fun testKotlinDeserialization_jsonNodeValueWithNullAsDefaultProvidedNull() { - assertThat( - "Nullable by default JsonNode with provided null value in payload should be deserialized as NullNode", + assertEquals( + NullNode.instance, value.jsonNodeValueWithNullAsDefaultProvidedNull, - CoreMatchers.equalTo(NullNode.instance) + "Nullable by default JsonNode with provided null value in payload should be deserialized as NullNode" ) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github50.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github50.kt similarity index 65% rename from src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github50.kt rename to src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github50.kt index 715e5275f..5f1d442ba 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github50.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github50.kt @@ -1,11 +1,11 @@ -package com.fasterxml.jackson.module.kotlin.test.github.failing +package com.fasterxml.jackson.module.kotlin.test.github; import com.fasterxml.jackson.annotation.JsonUnwrapped import com.fasterxml.jackson.databind.exc.InvalidDefinitionException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub50 { @@ -19,10 +19,8 @@ class TestGithub50 { @Test fun testGithub50UnwrappedError() { val json = """{"firstName":"John","lastName":"Smith","position":"Manager"}""" - expectFailure("GitHub #50 has been fixed!") { - val obj: Employee = jacksonObjectMapper().readValue(json) - assertEquals(Name("John", "Smith"), obj.name) - assertEquals("Manager", obj.position) - } + val obj: Employee = jacksonObjectMapper().readValue(json) + assertEquals(Name("John", "Smith"), obj.name) + assertEquals("Manager", obj.position) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github52.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github52.kt index 962bd80fd..ad85d4e7b 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github52.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github52.kt @@ -3,12 +3,13 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub52 { private val mapper = jacksonObjectMapper() - @org.junit.Test + @Test fun testBooleanPropertyInConstructor() { data class BooleanPropertyInConstructor( @JsonProperty("is_bar") @@ -18,7 +19,7 @@ class TestGithub52 { assertEquals("""{"is_bar":true}""", mapper.writeValueAsString(BooleanPropertyInConstructor())) } - @org.junit.Test + @Test fun testIsPrefixedBooleanPropertyInConstructor() { data class IsPrefixedBooleanPropertyInConstructor( @JsonProperty("is_bar2") @@ -28,7 +29,7 @@ class TestGithub52 { assertEquals("""{"is_bar2":true}""", mapper.writeValueAsString(IsPrefixedBooleanPropertyInConstructor())) } - @org.junit.Test + @Test fun testIsPrefixedStringPropertyInConstructor() { data class IsPrefixedStringPropertyInConstructor( @JsonProperty("is_lol") @@ -38,7 +39,7 @@ class TestGithub52 { assertEquals("""{"is_lol":"sdf"}""", mapper.writeValueAsString(IsPrefixedStringPropertyInConstructor())) } - @org.junit.Test + @Test fun testBooleanPropertyInBody() { data class BooleanPropertyInBody( @JsonIgnore val placeholder: String = "placeholder" @@ -50,7 +51,7 @@ class TestGithub52 { assertEquals("""{"is_foo":true}""", mapper.writeValueAsString(BooleanPropertyInBody())) } - @org.junit.Test + @Test fun testIsPrefixedBooleanPropertyInBody() { data class IsPrefixedBooleanPropertyInBody( @JsonIgnore val placeholder: String = "placeholder" diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github526.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github526.kt index 1ef7eac27..2dfbff641 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github526.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github526.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonSetter import com.fasterxml.jackson.annotation.Nulls import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class Github526 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github536.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github536.kt index eb82e56ec..4eca1cf92 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github536.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github536.kt @@ -3,8 +3,8 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonKey import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.testPrettyWriter -import junit.framework.TestCase.assertEquals -import org.junit.Test + import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class Github536 { @JvmInline diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github56.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github56.kt index aff873c44..b7a35c7ab 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github56.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github56.kt @@ -3,14 +3,11 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.JsonUnwrapped import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.databind.exc.InvalidDefinitionException import com.fasterxml.jackson.module.kotlin.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals - -class TestGithub56 { +private class TestGithub56 { private data class TestGalleryWidget_BAD( val widgetReferenceId: String, @@ -21,7 +18,6 @@ class TestGithub56 { @JsonUnwrapped lateinit var gallery: TestGallery } - @JsonInclude(JsonInclude.Include.NON_EMPTY) private data class TestGallery( val id: String? = null, @@ -40,7 +36,7 @@ class TestGithub56 { val crops: Map? = null ) - lateinit var mapper: ObjectMapper + val mapper: ObjectMapper = jacksonObjectMapper() private val gallery = TestGallery( id = "id", @@ -56,24 +52,22 @@ class TestGithub56 { {"widgetReferenceId":"widgetReferenceId","id":"id","headline":"headline","intro":"intro","role":"role","images":[{"id":"testImage1"},{"id":"testImage2"}]} """.trim() - @Before - fun setUp() { - mapper = jacksonObjectMapper() - } - @Test fun serializes() { val result = mapper.writeValueAsString(TestGalleryWidget_BAD("widgetReferenceId", gallery)) assertEquals(validJson, result) } - @Test(expected = InvalidDefinitionException::class) - fun deserializesWithError() { - mapper.readValue(validJson) + @Test + fun deserializesSuccessful() { + val obj = mapper.readValue(validJson) + assertEquals("widgetReferenceId", obj.widgetReferenceId) + assertEquals(gallery, obj.gallery) + } @Test fun deserializesCorrectly() { mapper.readValue(validJson) } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github57.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github57.kt index 3ccbd1001..b4f585b44 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github57.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github57.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.KeyDeserializer import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.* -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub57 { data class Github57Data(val map: Map, String>) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github62.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github62.kt index 77dbb8279..3a3e46623 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github62.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github62.kt @@ -1,7 +1,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub62 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github630.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github630.kt index a5fb7b633..d25d1a50a 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github630.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github630.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature import com.fasterxml.jackson.module.kotlin.KotlinModule -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class Github630 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github710.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github710.kt index d956fb8d8..b608653ed 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github710.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github710.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinFeature import com.fasterxml.jackson.module.kotlin.KotlinModule -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class Github710 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt index 344abb3d1..4ce93214c 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.InjectableValues import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import kotlin.math.exp -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt index 348a8fd66..2104aaafe 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt @@ -5,8 +5,8 @@ import com.fasterxml.jackson.annotation.Nulls import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertThrows -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test class Github738 { data class D(@JsonSetter(nulls = Nulls.FAIL) val v: Int) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github80.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github80.kt index 7601e7107..bab26ffff 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github80.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github80.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub80 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github88.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github88.kt index 63400c223..2eba29f9d 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github88.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github88.kt @@ -1,7 +1,7 @@ package com.fasterxml.jackson.module.kotlin.test.github import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub88 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github91.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github91.kt index ce5b03407..274d2ec39 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github91.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github91.kt @@ -4,8 +4,8 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonValue import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class TestGithub91 { data class DataClass1(val name: String, val content: DataClass2) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1005Test.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1005Test.kt index ca64ad0e3..5fec8cba2 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1005Test.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1005Test.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithubDatabind1005 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1328.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1328.kt index 428674a7e..46e49ed0f 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1328.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GithubDatabind1328.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo import com.fasterxml.jackson.annotation.JsonTypeName import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithubDatabind1328 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/TestCasesFromSlack.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/TestCasesFromSlack.kt index caadf73c4..9ff0c6b69 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/TestCasesFromSlack.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/TestCasesFromSlack.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.* -import org.junit.Test +import org.junit.jupiter.api.Test class TestCasesFromSlack1 { data class Host @JsonCreator constructor( diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub337.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub337.kt index cc3cfbea3..40ed6d3d2 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub337.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub337.kt @@ -5,8 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.MapperFeature.SORT_PROPERTIES_ALPHABETICALLY import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.testPrettyWriter -import org.junit.Ignore -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals /** @@ -20,7 +19,6 @@ class TestGitHub337 { private val writer = mapper.testPrettyWriter() @Test - @Ignore fun test_ClassWithIsFields() { data class ClassWithIsFields( val isBooleanField: Boolean, @@ -37,7 +35,6 @@ class TestGitHub337 { } @Test - @Ignore fun test_AnnotatedClassWithIsFields() { data class ClassWithIsFields( @JsonProperty("isBooleanField") val isBooleanField: Boolean, diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub451.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub451.kt index 94f8fe192..66308750a 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub451.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub451.kt @@ -2,8 +2,9 @@ package com.fasterxml.jackson.module.kotlin.test.github.failing import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.ComparisonFailure -import org.junit.Test +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.opentest4j.AssertionFailedError import kotlin.test.assertEquals class GitHub451 { @@ -25,7 +26,7 @@ class GitHub451 { val src = Target("a", "b") val json = mapper.writeValueAsString(src) - expectFailure("GitHub #451 has been fixed!") { + assertThrows("GitHub #451 has been fixed!") { assertEquals(expected, json) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub478.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub478.kt index 11066e073..ce5174485 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub478.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GitHub478.kt @@ -5,8 +5,7 @@ import com.fasterxml.jackson.module.kotlin.jsonMapper import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.ComparisonFailure -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class GitHub478Test { @@ -19,14 +18,14 @@ class GitHub478Test { @Test fun omitsDefaultValueWhenSerializing() { - expectFailure("GitHub478 has been fixed!") { + expectFailure("GitHub478 has been fixed!") { assertEquals("""{}""", mapper.writeValueAsString(Data())) } } @Test fun serializesNonDefaultValue() { - expectFailure("GitHub478 has been fixed!") { + expectFailure("GitHub478 has been fixed!") { assertEquals("""{"flag": false}""", mapper.writeValueAsString(Data(flag = false))) } } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github138.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github138.kt index 03489336a..497293bff 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github138.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github138.kt @@ -9,7 +9,7 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.registerKotlinModule import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub138 { @JacksonXmlRootElement(localName = "sms") diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github160DisableAnnotations.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github160DisableAnnotations.kt index 3d1c1f422..a4af7e5ce 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github160DisableAnnotations.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github160DisableAnnotations.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub160 { data class DataClass(val blah: String) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github242.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github242.kt index 5a2e9a70b..95eb8bdbe 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github242.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github242.kt @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.kotlin.test.github.failing import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals // Also see https://github.com/FasterXML/jackson-databind/issues/3392 diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github271AlphaSortProperties.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github271AlphaSortProperties.kt index 6f4802f0e..e0f411b76 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github271AlphaSortProperties.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github271AlphaSortProperties.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github.failing import com.fasterxml.jackson.annotation.JsonPropertyOrder import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub271 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github474.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github474.kt index 305672713..8d64977fb 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github474.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github474.kt @@ -3,8 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github.failing import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.ComparisonFailure -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub474 { @@ -13,7 +12,7 @@ class TestGithub474 { open class Parent(@JsonProperty("parent-prop") val parent: String) class Child(@JsonProperty("child-prop") val child: String) : Parent(child) - expectFailure("GitHub #474 has been fixed!") { + expectFailure("GitHub #474 has been fixed!") { assertEquals( """{"child-prop":"foo","parent-prop":"foo"}""", jacksonObjectMapper().writeValueAsString(Child("foo")) diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github518.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github518.kt index 4d91d8d59..f680c72a5 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github518.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github518.kt @@ -7,7 +7,7 @@ import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure import kotlin.test.assertSame -import org.junit.Test +import org.junit.jupiter.api.Test /** * An empty object should be deserialized as *the* Unit instance for a nullable Unit reference Type. diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github54.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github54.kt index 29878694e..78d2f0654 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github54.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github54.kt @@ -6,7 +6,7 @@ import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test class TestGithub54 { @Test diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github611.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github611.kt index cb147fe66..f8bb69afd 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github611.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github611.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github.failing import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub611 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github71.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github71.kt index 5c92ea451..c1210e300 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github71.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github71.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.github.failing import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals class TestGithub71 { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GithubDatabind1329.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GithubDatabind1329.kt index 775b4f3c1..cac4e097a 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GithubDatabind1329.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/GithubDatabind1329.kt @@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.JsonTypeName import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.test.expectFailure -import org.junit.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNull diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithDefaultArgumentsTest.kt index 6453aeee3..6d24a7b78 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithDefaultArgumentsTest.kt @@ -3,7 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.parameterSize import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals /** diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithoutDefaultArgumentsTest.kt index 60c80deef..4464218fd 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByConstructorWithoutDefaultArgumentsTest.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals /** diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithDefaultArgumentsTest.kt index 24a33da90..e0d2ba995 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithDefaultArgumentsTest.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals /** diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithoutDefaultArgumentsTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithoutDefaultArgumentsTest.kt index b0f1af2c7..39ce3c252 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithoutDefaultArgumentsTest.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/parameterSize/DeserializeByFactoryWithoutDefaultArgumentsTest.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.module.kotlin.assertReflectEquals import com.fasterxml.jackson.module.kotlin.callPrimaryConstructor import com.fasterxml.jackson.module.kotlin.defaultMapper import com.fasterxml.jackson.module.kotlin.readValue -import kotlin.test.Test +import org.junit.jupiter.api.Test import kotlin.test.assertEquals // Convert the property p to q (but not the value) to make it an input to the factory function.