Skip to content

Commit 303c1c7

Browse files
authored
Merge pull request #438 from k163377/fix_on_named_companion
Fixed mapping failure when `private` `companion object` is nammed.
2 parents 7c6c55c + 73ba479 commit 303c1c7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal class KotlinValueInstantiator(
7171
possibleCompanion.objectInstance
7272
} catch (ex: IllegalAccessException) {
7373
// fallback for when an odd access exception happens through Kotlin reflection
74-
val companionField = possibleCompanion.java.enclosingClass.fields.firstOrNull { it.name == "Companion" }
74+
val companionField = possibleCompanion.java.enclosingClass.fields.firstOrNull { it.type.kotlin.isCompanion }
7575
?: throw ex
7676
val accessible = companionField.isAccessible
7777
if ((!accessible && ctxt.config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) ||

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/ParameterNameTests.kt

+17
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ class TestJacksonWithKotlin {
197197
}
198198
}
199199

200+
private class StateObjectWithFactoryOnNamedCompanion private constructor (override val name: String, override val age: Int, override val primaryAddress: String, override val wrongName: Boolean, override val createdDt: Date) : TestFields {
201+
var factoryUsed: Boolean = false
202+
companion object Named {
203+
@JvmStatic @JsonCreator fun create(@JsonProperty("name") nameThing: String, @JsonProperty("age") age: Int, @JsonProperty("primaryAddress") primaryAddress: String, @JsonProperty("renamed") wrongName: Boolean, @JsonProperty("createdDt") createdDt: Date): StateObjectWithFactoryOnNamedCompanion {
204+
val obj = StateObjectWithFactoryOnNamedCompanion(nameThing, age, primaryAddress, wrongName, createdDt)
205+
obj.factoryUsed = true
206+
return obj
207+
}
208+
}
209+
}
210+
211+
@Test fun findingFactoryMethod3() {
212+
val stateObj = normalCasedMapper.readValue(normalCasedJson, StateObjectWithFactoryOnNamedCompanion::class.java)
213+
stateObj.validate()
214+
assertThat(stateObj.factoryUsed, equalTo(true))
215+
}
216+
200217
// GH #14 failing due to this enum type
201218
data class Gh14FailureWithEnum(var something: String = "hi", var someEnum: LaunchType = LaunchType.ACTIVITY)
202219

0 commit comments

Comments
 (0)