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

MapperFeature.REQUIRE_SETTERS_FOR_GETTERS has no effect #736

Closed
migel opened this issue Mar 27, 2015 · 5 comments · Fixed by #4257
Closed

MapperFeature.REQUIRE_SETTERS_FOR_GETTERS has no effect #736

migel opened this issue Mar 27, 2015 · 5 comments · Fixed by #4257
Labels
2.17 Issues planned at earliest for 2.17 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Milestone

Comments

@migel
Copy link

migel commented Mar 27, 2015

Hi, I've tried the code below to serialize properties that have both a getter and a setter. However the output is: {"readonly":1,"readwrite":2} while I expected it to be: {"readwrite":2}.

public class Main {

    public static class DataB {
        private int readonly;
        private int readwrite;

        public DataB() {
            readonly = 1;
            readwrite = 2;
        }

        public int getReadwrite() {
            return readwrite;
        }
        public void setReadwrite(int readwrite) {
            this.readwrite = readwrite;
        }
        public int getReadonly() {
            return readonly;
        }
    }

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper(); 
        mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY);
        mapper.setVisibility(PropertyAccessor.SETTER, Visibility.PUBLIC_ONLY);
        mapper.enable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS);
        DataB dataB = new DataB();
        try {
            String json = mapper.writeValueAsString(dataB);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
@cowtowncoder
Copy link
Member

cowtowncoder commented Mar 27, 2015

Interesting. Sounds like a bug; I am guessing existence of the matching field might be mistaken as indication of a setter. Which version is this with?

@migel
Copy link
Author

migel commented Mar 27, 2015

2.5.1

@cowtowncoder
Copy link
Member

Ok, I can reproduce this. And it does look like problem with a field.

But I'll have to handle this carefully: as per its Javadoc, REQUIRE_SETTERS_FOR_GETTERS does considers fields as well as getter methods ("determines whether getters (getter methods) can be auto-detected if there is no matching mutator (setter, constructor parameter or field) or not").

@migel
Copy link
Author

migel commented Mar 28, 2015

Thanks. If I understand correctly, in this case the matching mutator (private int readonly) is a field but it is marked private so it wasn't suppose to detect this mutator since auto detection it is limited to public setters.

@cowtowncoder
Copy link
Member

cowtowncoder commented Mar 29, 2015

@migel Yes, for getter action. But for "setter" action lower visibility is accepted. However, you have defined NONE as the baseline, so it should not be discovered even then.

If you want to try setting PropertyAccessor.FIELD directly to Visibility.NONE, although it should not make difference here.

So, yes, I think there is a problem here. I am just not fully sure how to tackle it.

As a work-around on short term you may want to use explicit @JsonIgnore, in case you were blocked.

cowtowncoder added a commit that referenced this issue May 30, 2015
@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Jul 17, 2018
@cowtowncoder cowtowncoder added 2.12 and removed 2.10 labels Apr 3, 2020
@cowtowncoder cowtowncoder added 2.13 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue and removed 2.12 labels Oct 27, 2020
@cowtowncoder cowtowncoder added 2.14 and removed 2.13 labels Jul 15, 2021
@cowtowncoder cowtowncoder added 2.17 Issues planned at earliest for 2.17 and removed 2.14 labels Dec 15, 2023
@cowtowncoder cowtowncoder added this to the 2.17.0 milestone Dec 15, 2023
@cowtowncoder cowtowncoder changed the title REQUIRE_SETTERS_FOR_GETTERS has no effect MapperFeature.REQUIRE_SETTERS_FOR_GETTERS has no effect Dec 15, 2023
cowtowncoder added a commit that referenced this issue Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.17 Issues planned at earliest for 2.17 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants