Skip to content

@JsonNaming on value class not applied to use via Builder #2712

Closed as not planned
@gbaso

Description

@gbaso

Given an input json string like "{\"Id\":1,\"Title\":\"title\",\"ShortDescription\":\"desc\"}", I'd like to keep java naming conventions on my class, and use PropertyNamingStrategy.UpperCamelCaseStrategy for deserialization:

    @lombok.Data
    @lombok.Builder
    @lombok.NoArgsConstructor
    @lombok.AllArgsConstructor(access = AccessLevel.PACKAGE)
    @JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
    public class Foo {
        private Long   id;
        private String title;
        private String shortDescription;
    }

While this works as intended, if I wanted to make the class (sort of) immutable I'd have to remove the no args constructor, and use the builder to deserialize:

    @lombok.Value
    @lombok.Builder
    @lombok.AllArgsConstructor(access = AccessLevel.PACKAGE)
    @JsonDeserialize(builder = Foo.FooBuilder.class)
    @JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
    public class Foo {
        private Long   id;
        private String title;
        private String shortDescription;
    }

but this results in an empty bean. The builder setters are never invoked, Jackson cannot match them to the input properties:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String json = "{\"Id\":1,\"Title\":\"title\",\"ShortDescription\":\"desc\"}";
mapper.readValue(json, Foo.class);
//Foo(id=null, title=null, shortDescription=null)

Currently on Jackson 2.10.3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    builder-relatedIssue related to handling of Builder-style deserialization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions