Skip to content

Different creator parameter null behavior for 2.17.3 and 2.18.2 #874

@hykilpikonna

Description

@hykilpikonna

Search before asking

  • I searched in the issues and found nothing similar.
  • I have confirmed that the same problem is not reproduced if I exclude the KotlinModule.
  • I searched in the issues of databind and other modules used and found nothing similar.
  • I have confirmed that the problem does not reproduce in Java and only occurs when using Kotlin and KotlinModule.

Describe the bug

After upgrading from jackson 2.17.3 to 2.18.1, the following error occurs when trying to deserialize null into an inherited field with a default value that also has a required constructor.

Exception in thread "main" com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class icu.samnyan.aqua.sega.chusan.model.userdata.UserActivity] value failed for JSON property user due to missing (therefore NULL) value for creator parameter user which is a non-nullable type
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 76] (through reference chain: icu.samnyan.aqua.sega.chusan.model.userdata.UserActivity["user"])
	at com.fasterxml.jackson.module.kotlin.KotlinValueInstantiator.createFromObjectWith(KotlinValueInstantiator.kt:97)
	at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:214)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:541)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1497)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4917)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3843)
	at test.JsonTestKt.main(JsonTest.kt:25)

To Reproduce

Here are my model definitions. Note that the user field in the parent has a default value, but the UserActivity class also has a constructor parameter user that doesn't have a default value.

@MappedSuperclass
open class BaseEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    open var id: Long = 0
)

@MappedSuperclass
open class Chu3UserEntity : BaseEntity(), IUserEntity<Chu3UserData> {
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "user_id")
    public override var user: Chu3UserData = Chu3UserData()
}

@Entity(name = "ChusanUserActivity")
@Table(name = "chusan_user_activity")
class UserActivity(user: Chu3UserData) : Chu3UserEntity() {
    init { this.user = user }

    var kind = 0
    @JsonProperty("id")
    @Column(name = "activity_id")
    var activityId = 0
    var sortNumber = 0
}

The following code will run successfully on Jackson (+kotlin module) 2.15 but fail on 2.18:

fun main(args: Array<String>) {
    val mapper = jacksonObjectMapper().apply {
        configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true)
        findAndRegisterModules()
    }
    val json = mapper.writeValueAsString(UserActivity(Chu3UserData()))
    mapper.readValue<UserActivity>(json)
}

Expected behavior

I do not expect null-handling behavior to subtly change when upgrading between minor versions of jackson-module-kotlin.

Versions

Kotlin: 2.1.0
Jackson-module-kotlin: 2.18.2
Jackson-databind: 2.18.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions