orika-mapper/orika

ScoringClassMapBuilder generated the wrong map

shenzhaoquan opened this issue · 0 comments

  1. I defined two entities as shown below
    public static class A {
    public String title;
    public String content;
    }

    public static class B {
    public String title;
    public Integer lengthTitle;
    public String content;
    public Integer lengthContent;
    }

  2. Add test case
    @test
    public void testScoringMatchingBug() throws Throwable {
    MapperFactory factory = new DefaultMapperFactory.Builder().classMapBuilderFactory(new ScoringClassMapBuilder.Factory()).build();

     ClassMap<A, B> map = factory.classMap(A.class, B.class).byDefault().toClassMap();
     Map<String, String> mapping = new HashMap<>();
     for (FieldMap f : map.getFieldsMapping()) {
         mapping.put(f.getSource().getExpression(), f.getDestination().getExpression());
     }
    
     /*
      * Check that properties we expect were mapped
      */
     Assert.assertEquals("title", mapping.get("title"));
     Assert.assertEquals("content", mapping.get("content"));
    

    }

  3. result
    Unit test failed. Title is incorrectly mapped to lengthTitle and should be mapped to title first, content is the same

  4. expect result
    Unit test passed.