-
Notifications
You must be signed in to change notification settings - Fork 175
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
Duplicate creator error when using combination of JsonProperty
and JsonIgnore
#692
Comments
In It looks like you need to set a default argument for the parameter |
The default value was indeed missing from my sample - the error also occurs with a default value set. |
It was an incorrect reference to the default argument. When reproduced in import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
public class Temp {
public static class Foo {
private final String foo;
@JsonIgnore
private final String bar;
@JsonCreator
public Foo(@JsonProperty("bar") String foo, @JsonProperty("bar") String bar) {
this.foo = foo;
this.bar = bar;
}
}
@Test
public void test() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.readValue("{\"bar\":\"bar\"}", Foo.class);
}
} |
How would I solve the issue in my Kotlin code? My current workaround is to put |
I don't understand what you are trying to do, but a simple rewrite would be as follows class Foo (
@JsonProperty("bar")
val alternateBar: String
) {
@JsonIgnore
val bar: String = "noDeserialization"
} class Foo (
val alternateBar: String,
@JsonIgnore
val bar: String,
) {
@JsonCreator
constructor(@JsonProperty("bar") alternateBar: String) : this(alternateBar, "noDeserialization")
} |
My specific usecase is that I have a data class implementing an interface that defines a property ("bar") that corresponds to one of my JSON fields by name, but that should not be mapped. So I want to put that property in my constructor (to give it the value semantics), but prevent it from being mapped by Jackson, so I give it the So essentially: class Impl (
@JsonProperty("bar")
val alternateBar: String,
@JsonIgnore
override val bar: String = "noDeserialization",
) : BarInterface Then I would expect this to just work out of the box, without doing manual constructor magic or losing data class semantics. I understand now that this has to do with constructor/property mapping magic in Kotlin, but the weird thing is that the following does work properly: class Impl (
@JsonProperty("bar")
val alternateBar: String,
@JsonIgnore
@JsonProperty("ignoredBar")
override val bar: String = "noDeserialization",
) : BarInterface So to me it looked like the ignore annotation did not get registered in some way, while the property annotation does seem to have effect. That's why I registered this as a bug. |
At least this is not a bug. Therefore, this issue is closed. |
Describe the bug
When using both
JsonProperty
andJsonIgnore
at the same time, creator resolving does not seem to succeed. This does work for Java, so I guess it's a specific thing with Kotlin properties, getters and/or setters.To Reproduce
Trying to deserialize this results in the following error:
Expected behavior
I expect the deserialization to succeed, with the value
bar
in fieldalternateBar
.Versions
Kotlin: 1.8.21
Jackson-module-kotlin: 2.15.2
Jackson-databind: 2.15.2
Additional context
Maybe this is already solved in #630? The linked issues there did not seem the same, but it might touch the same areas of code. Furthermore, this seems to have been in issue in the Java library at some point (https://groups.google.com/g/jackson-user/c/sXDF767Flj8), but in Java it now works with the following class definition:
The text was updated successfully, but these errors were encountered: