Skip to content

Commit

Permalink
fix(core): ensure properties that aren't on destination won't be mapp…
Browse files Browse the repository at this point in the history
…ed from forSelf
  • Loading branch information
Chau Tran authored and Chau Tran committed Aug 30, 2021
1 parent 66d410f commit 390b8bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/core/src/lib/create-mapper/create-map-for-self.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function createMapForSelf<TSource, TDestination>(
selector: Selector<TSource>
) {
const mapper = mapping[MappingClassId.mapper];
const [, destinationInstance] = mapping[MappingClassId.mappings];

const [sourceInstance] = mapper.plugin.instantiate(source);
const sourceInstanceKeys = Object.keys(sourceInstance);
Expand All @@ -26,6 +27,7 @@ export function createMapForSelf<TSource, TDestination>(
);

if (!foundMapProperty) {
if (!(key in destinationInstance)) continue;
mapping[MappingClassId.properties].push([
[key],
[
Expand All @@ -39,20 +41,21 @@ export function createMapForSelf<TSource, TDestination>(
],
],
]);
} else {
if (
foundMapProperty[MappingPropertiesClassId.property][
MappingPropertyClassId.transformation
][MappingTransformationClassId.memberMapFn][MapFnClassId.type] !==
TransformationType.MapInitialize
)
continue;
continue;
}

if (
foundMapProperty[MappingPropertiesClassId.property][
MappingPropertyClassId.transformation
][MappingTransformationClassId.memberMapFn] = [
TransformationType.MapInitialize,
(originalSource) => get(selector(originalSource), [key]),
];
}
][MappingTransformationClassId.memberMapFn][MapFnClassId.type] !==
TransformationType.MapInitialize
)
continue;
foundMapProperty[MappingPropertiesClassId.property][
MappingPropertyClassId.transformation
][MappingTransformationClassId.memberMapFn] = [
TransformationType.MapInitialize,
(originalSource) => get(selector(originalSource), [key]),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe('Map - Self', () => {
name: string;
@AutoMap()
price: number;
@AutoMap()
stock: number;
}

class CartItem {
Expand Down Expand Up @@ -43,6 +45,7 @@ describe('Map - Self', () => {
const item = new Item();
item.name = 'item1';
item.price = 123;
item.stock = 456;
const cartItem = new CartItem();
cartItem.item = item;
cartItem.quantity = 10;
Expand All @@ -52,6 +55,7 @@ describe('Map - Self', () => {
expect(dto.price).toEqual(item.price);
expect(dto.quantity).toEqual(cartItem.quantity);
expect(dto.total).toEqual(cartItem.quantity * item.price);
expect(dto['stock']).toBeUndefined();
});

it('should map correctly for forMember that overrides forSelf', () => {
Expand All @@ -73,6 +77,7 @@ describe('Map - Self', () => {
expect(dto.price).toEqual(item.price);
expect(dto.quantity).toEqual(cartItem.quantity);
expect(dto.total).toEqual(cartItem.quantity * item.price);
expect(dto['stock']).toBeUndefined();
});

it('should map correctly for forMember that overrides BEFORE forSelf', () => {
Expand All @@ -94,5 +99,6 @@ describe('Map - Self', () => {
expect(dto.price).toEqual(item.price);
expect(dto.quantity).toEqual(cartItem.quantity);
expect(dto.total).toEqual(cartItem.quantity * item.price);
expect(dto['stock']).toBeUndefined();
});
});

0 comments on commit 390b8bb

Please sign in to comment.