Lombok @Builder interferes with Jackson @JsonUnwrapped
See original GitHub issueSpringfox 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:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Following workaround seems to work:
Im not very familiar with how lombok serialization works, great you found a workaround, sorry I couldnt be of much help