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.

Lombok @Builder interferes with Jackson @JsonUnwrapped

See original GitHub issue

Springfox version [2.8.0] com.fasterxml.jackson version [2.9.2] Spring 1.5.9

Language {JAVA}

A combination of Lombok @Builder and the request sample in swagger-ui makes the Jackson @JsonUnwrapped annotation not be followed

A Spring resource class with the following method

    @ApiResponses({
            @ApiResponse(code = 201, message = "A new jill is initiated",
                    response = Jill.class),
            @ApiResponse(code = 400, message = "Wrong data")
    })
    @PostMapping(
            value="/jill",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            headers = "Accept=application/json"
    )
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    public Jill jill(@ApiParam(value = "data", required = true)
                    @Valid @RequestBody Jack jack) {
        return Jill.builder()
                .amt(ParentType.builder()
                        .parent(ChildType.builder()
                                .value("one")
                                .currency("EUR")
                                .build())
                        .build())
                .build();
    } 

and Data classes “request” Jack.class:

@Data
@Builder
public class Jack {

    @JsonUnwrapped
    @JsonProperty("amount")
    private ParentType amt;

    @JsonUnwrapped
    public void setAmt(ParentType amt) {
        this.amt = amt;
    }

    @JsonUnwrapped
    public ParentType getAmt() {
        return amt;
    }

}

ParentType.class

@Data
@Builder
public class ParentType {

    @JsonProperty("parent")
    private ChildType parent;
}

ChildType.class

@Data
@Builder
public class ChildType {

    @JsonProperty("value")
    private String value;

    @JsonProperty("currency")
    private String currency;

}

Response Jill.Class that apparently works:

@Data
@Builder
public class Jill {

    @JsonUnwrapped
    @JsonProperty("amount")
    private ParentType amt;

}

swagger-ui outputs in the “request” example field:

{
  "amount": {
    "parent": {
      "value": "string",
      "currency": "string"
    }
  }
}

and the “response” example:

{
  "parent": {
    "value": "string",
    "currency": "string"
  }
}

I can remove the @Builder from the request Jack.class and it will output the correct json model with the correct unwrapped object

I tried to overwrite the inner Builder class in the Jack.clas but I failed to succeed.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
gnespolinocommented, Jun 22, 2018

Following workaround seems to work:

@Getter
@Builder
@RequiredArgsConstructor // Jackson will deserialize using this and then invoking setter
@AllArgsConstructor(onConstructor = @__(@JsonIgnore)) // lombok's builder will use this
public class Parent {

    @Setter(PRIVATE) // to keep immutability
    @JsonUnwrapped
    private Child child;
    private final String anotherField;

}
1reaction
dilipkrishcommented, Apr 17, 2018

Im not very familiar with how lombok serialization works, great you found a workaround, sorry I couldnt be of much help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Immutable Lombok annotated class with Jackson
As described in the linked documentation, this annotation: configures Jackson to use the builder for deserialisation,; copies field-specific ...
Read more >
@Jacksonized - Project Lombok
The @Jacksonized annotation is an add-on annotation for @Builder and @SuperBuilder ... the generated builder class to be used by Jackson's deserialization.
Read more >
Builders and JSON – A Lombok and Jackson Love Story
In my opinion, builders make code more readable and fluent as well as simplifying the simplifying initialization of scenarios for testeded class ...
Read more >
FasterXML/jackson-databind - Gitter
I am using Lombok and have tried the annotation @ToString. ... Hello, I have a problem jackson TypeFactory can't process this string config....
Read more >
Jackson JSON - Using @JsonAnySetter to deserialize ...
@JsonAnySetter annotation can be used to define "any setter" mutator. It is used on a non-static two-argument method; first argument is name ...
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