-
Notifications
You must be signed in to change notification settings - Fork 181
Kotlin boolean property setters are skipped if isX prefixed #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
EDIT: I got things mixed up. The issue here is with Jackson, not SPQR. See my comment below.
|
Would such a module for SPQR solve this specific case? |
Ignore (almost) everything I said. I got confused 🤦♂️ |
jackson-module-kotlin has already been added to my project. |
Huh. SPQR normally calls generator.withValueMapperFactory(JacksonValueMapperFactory.builder()
.withPrototype(new ObjectMapper()) //customize ObjectMapper as you please, e.g. add a module
.build()) If you're using SPQR with Spring, you might want to use the Spring-provided |
If that doesn't help... then Jackson is weird about Kotlin? |
I tired this quickly* and I indeed do not get "Hello world" printed: class MyTest {
var isAlive: Boolean? = null
set(value) {
field = value
println("Hello world")
}
}
fun main(args:Array<String>) {
val clazz: Class<MyTest> = MyTest::class.java
ObjectMapper().registerModules(KotlinModule.Builder()
.enable(KotlinFeature.KotlinPropertyNameAsImplicitName).build())
.readValue("{ \"isAlive\": true }", clazz)
} So either I did something wrong (did I?) or Jackson did... *not actually quickly 😶 |
Actually... the more I look at this the more sense it makes. |
The easiest you can do is: class MyTest {
var isAlive: Boolean? = null
@JsonSetter("isAlive")
set(value) {
field = value
println("Hello world")
}
}
fun main(args:Array<String>) {
val clazz: Class<MyTest> = MyTest::class.java
ObjectMapper() //No KotlinPropertyNameAsImplicitName
.readValue("{ \"isAlive\": true }", clazz)
} It prints "Hello world" as expected. You could probably achieve the same with |
I actually tried this myself some time ago and reached the same conclusion. |
Actually didn't see your last 2 messages. So i think the JavaBean convention probably makes sense for Java. Your @JsonSetter solution is interesting, i may use that as a fallback. |
Wow, I encountered the same issue on my project, too. I am gonna apply the way that you guys suggested. Thanks a lot 👍 |
@IceBlizz6 Thanks for this issue. However, I don't think that this issue should be handled in this library. As @kaqqao said, this issue is related to Java Bean model Convention and Kotlin language convert rules. I think we can resolve by customizing each repos like the way that @kaqqao guided. He also introduced |
I agree that the solution for this probably does not belong in this library. For reference here is the issue that i created in the jackson-module-kotlin repo. |
Hello
Another interesting Kotlin problem here.
I'm not sure if this issue should be reported to graphql-java or here.
I will try here first, let me know if it belongs elsewhere.
Look at the following code:
This is comparable to a backing field with a setter and a getter.
javap displays it like this:
Making a call to makeTest like this
query { makeTest(input: { isAlive: true }) }
I expect it to print "Hello world", but it does not.
I believe this is because it may assign the value through the backing field directly instead.
Now i rename the field to 'alive'
javap displays it like this:
If i call it from GraphQL now then it will print as expected.
So it seems like boolean fields prefixed with isX will be set through backing field directly.
But boolean fields without the prefix will be set through the setter method.
I expected the setter to be used regardless of property name.
The text was updated successfully, but these errors were encountered: