Skip to content

Improve CustomObjectMarshaller resolution #1271

@fjtirado

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);
}
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions