Skip to content

Improve CustomObjectMarshaller resolution #1271

Description

@fjtirado

If two custom marshalles has the same priority and the classes returned getObjectClass() are assignable to the class of the object being marshalled, the one that should be applied is undefined right now, while it should be the one with the "closer" class in the class hierarchy.

See modified unit test that currenlty fails in the last assert

@Test
void testCustomMarshallers() {
  CustomObjectMarshaller personMarshaller = Mockito.spy(CustomObjectMarshaller.class);
  Mockito.when(personMarshaller.priority()).thenReturn(1);
  Mockito.when(personMarshaller.getObjectClass()).thenReturn(Person.class);
  CustomObjectMarshaller employeeMarshaller = Mockito.spy(CustomObjectMarshaller.class);
  Mockito.when(employeeMarshaller.priority()).thenReturn(2);
  Mockito.when(employeeMarshaller.getObjectClass()).thenReturn(Employee.class);
  CustomObjectMarshaller objectMarshaller = Mockito.spy(CustomObjectMarshaller.class);
  Mockito.when(objectMarshaller.priority()).thenReturn(1);
  Mockito.when(objectMarshaller.getObjectClass()).thenReturn(Object.class);
  Object employee = new Employee();
  Object student = new Student();
  Object person = new Person();
  Object other = new byte[2];

  List<CustomObjectMarshaller> marshallers =
      Stream.of(objectMarshaller, employeeMarshaller, personMarshaller)
          .sorted()
          .collect(Collectors.toList());
  assertThat(marshallers).containsExactly(objectMarshaller, personMarshaller, employeeMarshaller);
  assertThat(MarshallingUtils.getCustomMarshaller(marshallers, employee.getClass()))
      .isEqualTo(employeeMarshaller);
  assertThat(MarshallingUtils.getCustomMarshaller(marshallers, person.getClass()))
      .isEqualTo(personMarshaller);
  assertThat(MarshallingUtils.getCustomMarshaller(marshallers, other.getClass()))
      .isEqualTo(objectMarshaller);
  assertThat(MarshallingUtils.getCustomMarshaller(marshallers, student.getClass()))
  .isEqualTo(employeeMarshaller);
}
}

Activity

  1. self-assigned this
    on Mar 26, 2026
  2. added 13 commits that reference this issue on Mar 26, 2026
    4379a2d
    16c7138
    53b01cb
    216d9bd
    05bc17d
    1cc059b
    dfdaea6
    e015f79
    751857e
    ac2fdd1
    9a771eb
    eb70195
    315ea30
  3. added theissue type on Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions