-
Notifications
You must be signed in to change notification settings - Fork 96
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
@JsonbProperty on setter is broken 1.0.5 #355
Comments
hi @tsfullman, thanks for reporting this issue. Are you able to provide a test case to reproduce the issue? |
Looking through the changes I believe this PR may have been related: #271 to the behavior change |
Ok so after looking at this more I don't think it's related to the custom deserialization or arrays at all. What happened was I had a setter that didn't match the name of the variable. However, I annotated it with @JsonbProperty and it was still ignored. The getter still registers the @JsonbProperty as expected. For example: import java.util.Objects;
import javax.json.bind.annotation.JsonbProperty;
public class Bob {
private String apple;
public Bob() {
}
public Bob(final String test) {
setTest(test);
}
@JsonbProperty("apple")
public String getTest() {
return apple;
}
@JsonbProperty("apple")
public void setTest(String test) {
this.apple = test;
}
@Override
public int hashCode() {
return Objects.hash(apple);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bob other = (Bob) obj;
return Objects.equals(apple, other.apple);
}
} which causes this unit test to fail on the assertEquals(b,b1); because the string apple is null in the deserialized object which passed with 1.0.4: @Test
public void test() {
final Bob b = new Bob("hi");
final String h = JsonbProvider.provider().create().build().toJson(b);
final String expectedJson = "{\"apple\":\"hi\"}";
assertEquals(expectedJson, h); //this passes
final Bob b1 = JsonbProvider.provider().create().build().fromJson(h, Bob.class);
assertNotNull(b1.getTest()); //this fails but passes in 1.0.4
} |
The bug started happening after #288, if I revert 'propertyModel.getPropertyName()' to 'propertyModel.getReadName()' the test passes again, but then 2 other tests break. |
Printing out the names (in properties.forEach) it looks like the annotation is getting ignored somewhere when setting the PropertyName: |
thanks for the investigation @Degubi, it looks like you are on the right track. Are you going to continue investigating this one? Just want to make sure we don't duplicate work |
Yes I do @aguibert, I was just waiting for some feedback before continuing. :) |
Actually I don't think I have the right idea to fix this @aguibert. I was poking around in ClassParser::toPropertyMethod, I found that this is the method that creates the names used in Property.java. I thought that the problem was that the check was missing for the JsonbProperty annotation (in ClassParser::toPropertyMethod), but then other tests broke when I put in the annotation handling code... so had to put in more checks to see if the class has a declared private field as the value of the JsonbProperty annotation. Now the broken and all the original tests pass, but I don't think this is the right solution... just seems wrong to me. (sorry for the bad wording, my English is not my native language) |
I think you were sort of on the right track @Degubi, the main issue is that we had 2 separate PropertyModel instances for the class when we should only have had one. In PR #357 I added some extra code to process the PropertyModel list for a class and merge duplicate PropertyModels as needed. I'm considering two PropertyModels duplicate if their readName and writeName attributes are equal. |
After updating from 1.0.4 to 1.0.5, my unit test breaks which now deserializes my 2d array to null every time. It previously deserialized correctly with 1.0.4 and passed the test. It only doesn't work when there is a custom deserializer associated with the class.
I am deserializing to an interface which my class implements. The interface has the custom deserializer annotated on it for multiple types.
The text was updated successfully, but these errors were encountered: