AutoMapper icon indicating copy to clipboard operation
AutoMapper copied to clipboard

Property type changes at DestinationType

Open lucaritossa opened this issue 8 years ago • 1 comments

I think this is to be considered as a bug...

Based on unit test 'should ignore properties on source object missing on destination object type Definition' defined into automapper-map-specs.ts

I found that the keep property of DestinationType class changed from string to boolean after the mapping.

In a "typed world" (I'm coming from C#) this is not accepted.

I know the magic world of javascript, this is permitted... but working with typescript this could create a lot of problems.

I tested it adding this line:

expect(typeof` toObject.keep).toBe('string');

This is the snippet code complete with the assert at the end of the function...

        it('should ignore properties on source object missing on destination object type Definition', () => {
            // arrange
            class DestinationType {
                public keep: string = null;
            }

            var fromObject = {
                keep: true,
                remove: true
            };

            var fromKey = '{60D9DB56-D6E1-48FF-9BAC-0805FCAF91B7}';
            var toKey = '{AC6D5B97-9AE3-4267-BD60-A5FED17E541A}';

            automapper.createMap(fromKey, toKey).convertToType(DestinationType);

            // act
            var toObject = automapper.map(fromKey, toKey, fromObject);
            // assert
            expect(toObject).toBeDefined();
            expect(toObject.remove).not.toBeDefined();

            expect(typeof toObject.keep).toBe('string');  // --> this assert will fail
        });

lucaritossa avatar Aug 25 '17 09:08 lucaritossa

Hi @lucaritossa , thanks for discovering this bug. Although it is 'valid' to produce this kind of functionality in JavaScript, I also tend to consider it a bug. I will give it a thought and will try to fix this behavior in an upcoming version of the library if the performance penalty is not too high (probably by trying to compare runtime types). Cheers, Bert

loedeman avatar Sep 02 '17 14:09 loedeman