Skip to content

InvalidDefinitionException on classes using @JacksonXmlElementWrapper and @JacksonXmlProperty #853

Description

@soc

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Consider this pattern where serialization to XML is customized for properties of collections ...

public class GroupDto {
  @JacksonXmlElementWrapper(localName = "users")
  @JacksonXmlProperty(localName = "user")
  List<String> users;
  // other fields

  public GroupDto(List<String> users, /* other arguments */) {}

  List<String> getUsers() { return users; }
  // other getters
}

... such that the particular XML fragment looks like this:

<users>
  <user>A</user>
  <user>B</user>
  <user>C</user>
</users>
<!-- other elements -->

This worked on Jackson 2.x, but fails on Jackson 3 with ...

InvalidDefinitionException: Invalid definition for property 'users' (of type `GroupDto`): Could not find creator property with name 'users' (known Creator properties: [user, /* other property names */])

Note the users/user difference in the message.

  • Adding a @JsonProperty to the field fails with an error about ambiguous property names instead.
  • Changing the argument name in the constructor to the one seemingly expected (user) does not help.

There appear to be two workarounds:

  1. Adding a no-arg constructor with @JsonCreator (or removing all existing arg-constructors) and setters.
  2. Adding @JsonIgnore to the field, and @JsonPropertys to the field's getters and setters.

Both workarounds are undesirable, as they make the class mutable and therefore prevent a migration to a record.

Version Information

Jackson 3.1.2

Reproduction

See code snippet above.

Expected behavior

There should be a way to deserialize types with fields that are annotated with @JacksonXmlElementWrapper and @JacksonXmlProperty, without making the class mutable (which prevents the use of records altogether).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions