atos1990 / orika

Automatically exported from code.google.com/p/orika
0 stars 0 forks source link

Sub-class fields not set when parent class was converted before #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Have a class "Host" with two fields. First field of type A, second field of 
type B which extends A. It is important that the B field (and getter) name is 
alphabetically after parent-class field.
2. When converting instances of "Host", fields new to B are ignored.

Expecting that B and A instances are converted fully. Instead the B instance is 
converted like a A instance. a B instance is created, but only A fields are set 
on the object. Use this JUnit test class to reproduce the behavior:

-----------------------------------------------------------
package test.package;

import static org.junit.Assert.assertEquals;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import org.junit.Test;

public class ResultConverterTest {
    @Test
    public void subClassSetterOrikaTest() {
        final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        final MapperFacade mapper = mapperFactory.getMapperFacade();

        final SearchRecord sr = new SearchRecord();
        sr.setScore(88);

        final Result result = new Result();
        result.setBaseRecords(new Record());
        result.setResult(sr);

        final Result2 mappedResult = mapper.map(result, Result2.class);

        assertEquals(88, mappedResult.getResult().getScore());
    }

    public static class Record {}

    public static class SearchRecord extends Record {
        private int score;

        public int getScore() {
            return score;
        }

        public void setScore(final int score) {
            this.score = score;
        }
    }

    public static class Result {
        private Record          baseRecords;
        private SearchRecord    result;

        public void setBaseRecords(final Record baseRecords) {
            this.baseRecords = baseRecords;
        }

        public Record getBaseRecords() {
            return baseRecords;
        }

        public SearchRecord getResult() {
            return result;
        }

        public void setResult(final SearchRecord result) {
            this.result = result;
        }

    }

    public static class Record2 {}

    public static class SearchRecord2 extends Record2 {
        private int score;

        public int getScore() {
            return score;
        }

        public void setScore(final int score) {
            this.score = score;
        }
    }

    public static class Result2 {
        private Record2         baseRecords;
        private SearchRecord2   result;

        public Record2 getBaseRecords() {
            return baseRecords;
        }

        public void setBaseRecords(final Record2 baseRecords) {
            this.baseRecords = baseRecords;
        }

        public SearchRecord2 getResult() {
            return result;
        }

        public void setResult(final SearchRecord2 result) {
            this.result = result;
        }
    }
}
-----------------------------------------------------------

Using orika 1.3.5.

Original issue reported on code.google.com by wirch.ed...@gmail.com on 20 Nov 2012 at 4:24

GoogleCodeExporter commented 9 years ago
Please try just to add 
mapperFactory.classMap(SearchRecord.class, 
SearchRecord2.class).byDefault().register();
otherwise Orika will choose the previous parent generated mapper.

Original comment by elaat...@gmail.com on 21 Nov 2012 at 12:06

GoogleCodeExporter commented 9 years ago
Why do you close it? There is a work around, ok. But the behavior is obviously 
inconsistent. Rename the "getBaseRecords()" method to "getZBaseRecords()" and 
it will work as expected without custom mapping.

Besides, the project site says your motivation is "to build a comprehensive, 
efficient and robust Java bean mapping solution. Orika focuses on automating as 
much as possible, while providing customization through configuration and 
extension where needed.". Automation is possible here.

Original comment by wirch.ed...@gmail.com on 21 Nov 2012 at 12:35

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Sorry, I missed your point.

Original comment by elaat...@gmail.com on 21 Nov 2012 at 2:01

GoogleCodeExporter commented 9 years ago
In fact this was known bug, Matt was reporting it 
https://code.google.com/p/orika/issues/detail?id=53
Please take a look and confirm.

And Thanks for the TestCase :)

Kindest regards

Original comment by elaat...@gmail.com on 21 Nov 2012 at 4:15

GoogleCodeExporter commented 9 years ago
Yes, looks like the same.

Original comment by wirch.ed...@gmail.com on 22 Nov 2012 at 7:49

GoogleCodeExporter commented 9 years ago
Merging into issue #53

Original comment by matt.deb...@gmail.com on 22 Nov 2012 at 8:53