question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Different @JsonSetter vs @JsonGetter breaks Swagger example model

See original GitHub issue

Swagger version 2.7.0 Java 1.8.0

I currently have an API mapping:

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public List<TestObject> reportsTest() {
        return new ArrayList<>();
    }

and TestObject is defined as:

    public class TestObject {

        private String var1;

        public TestObject() {
        }

        @JsonGetter("var")
        public String getVar1() {
            return var1;
        }

        @JsonSetter("var")
        public void setVar1(String var1) {
            this.var1 = var1;
        }
    }

Everything works as expected, the Swagger UI docs example model looks good:

image

Now, if I change the @JsonGetter to something else (not matching the @JsonSetter) because I want the serialization to be mapped differently than the input like so:

    public class TestObject {

        private String var1;

        public TestObject() {
        }

        @JsonGetter("var")
        public String getVar1() {
            return var1;
        }

        @JsonSetter("newvar") // <-- change made here
        public void setVar1(String var1) {
            this.var1 = var1;
        }
    }

The example model no longer works and I’m stuck with this: image

Is there a workaround for this? I’ve tried setting @JsonProperty on the variable definition but that didn’t work either…

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bonjourchen123commented, May 6, 2020

try this un use @JsonGetter only @JsonSetter

 public class TestObject {

        //private String var1;
        @JsonSetter("newVar")
        private String var;

        public TestObject() {
        }

        //@JsonGetter("var")
        public String getVar() {
            return var;
        }

        public void seVar(String var) {
            this.var = var;
        }
    }
1reaction
saravanantpspcommented, Sep 19, 2018

Hello , I am also facing the same issue. Can you please let me know if the fix has been release?

Thanks, Saravanan

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson Not Overriding Getter with @JsonProperty
The issue was when deserializing the JSON response from our DB into Java class object, our code didn't have @JsonSetter on the class'...
Read more >
Jackson JSON - Using @JsonProperty, @JsonSetter and ...
Following example shows how to use @JsonProperty annotation to rename properties. This annotation can be used on fields or getters or setters.
Read more >
com.fasterxml.jackson.annotation.JsonSetter.value java code ...
public static String propertyName(Method method) { Optional<JsonGetter> jsonGetterAnnotation ... origin: com.mangofactory/swagger-models ...
Read more >
Overview (Apache Juneau 8.2.0)
org.apache.juneau.dto.swagger.ui ... Examples. org.apache.juneau.examples.core.json. Examples ... Non-Tree Models and Recursion Detection.
Read more >
About @JsonProperty, @JsonGetter and @JsonSetter
And that only has @JsonProperty on the getters, what is supposed to be the expected serialization/deserialization behaviour? What about:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found