Skip to content
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

Closed
tsfullman opened this issue Oct 22, 2019 · 9 comments · Fixed by #357
Closed

@JsonbProperty on setter is broken 1.0.5 #355

tsfullman opened this issue Oct 22, 2019 · 9 comments · Fixed by #357
Labels
bug Something isn't working right
Milestone

Comments

@tsfullman
Copy link

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.

@aguibert aguibert added the bug Something isn't working right label Oct 23, 2019
@aguibert
Copy link
Member

hi @tsfullman, thanks for reporting this issue. Are you able to provide a test case to reproduce the issue?

@aguibert
Copy link
Member

Looking through the changes I believe this PR may have been related: #271 to the behavior change

@tsfullman
Copy link
Author

tsfullman commented Oct 23, 2019

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
    }

@tsfullman tsfullman changed the title de-serializing arrays is broken 1.0.5 @JsonbProperty on setter is broken 1.0.5 Oct 25, 2019
@Degubi
Copy link
Contributor

Degubi commented Oct 29, 2019

The bug started happening after #288, if I revert 'propertyModel.getPropertyName()' to 'propertyModel.getReadName()' the test passes again, but then 2 other tests break.

@Degubi
Copy link
Contributor

Degubi commented Oct 29, 2019

Printing out the names (in properties.forEach) it looks like the annotation is getting ignored somewhere when setting the PropertyName:
PROPNAME: test (name of the setter, was 'setTest')
READNAME: apple (name of the field)

@aguibert
Copy link
Member

aguibert commented Nov 4, 2019

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

@Degubi
Copy link
Contributor

Degubi commented Nov 4, 2019

Yes I do @aguibert, I was just waiting for some feedback before continuing. :)

@Degubi
Copy link
Contributor

Degubi commented Nov 4, 2019

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)

@aguibert
Copy link
Member

aguibert commented Nov 5, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants