Skip to content

Commit

Permalink
fix(core): extend uses customProperties instead of properties because…
Browse files Browse the repository at this point in the history
… it is custom
  • Loading branch information
Chau Tran authored and Chau Tran committed Oct 25, 2022
1 parent 65c6a74 commit b29f2ef
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/lib/mapping-configurations/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export function extend<
propToExtendMappingProp,
propToExtendNestedMapping,
] = propsToExtend[i];
const existProp = mapping[MappingClassId.properties].find(
const existProp = mapping[MappingClassId.customProperties].find(
([pKey]) => isSamePath(pKey, propToExtendKey)
);
if (existProp) continue;
mapping[MappingClassId.properties].push([
mapping[MappingClassId.customProperties].push([
propToExtendKey,
propToExtendMappingProp as MappingProperty<
TSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ function getMappingProperty(path = 'foo') {
describe(extend.name, () => {
it('should extend another mapping', () => {
const mapping = [] as unknown as Mapping;
mapping[MappingClassId.properties] = [];
mapping[MappingClassId.customProperties] = [];
const mappingToExtend = [] as unknown as Mapping;
mappingToExtend[MappingClassId.properties] = [getMappingProperty()];

extend(mappingToExtend)(mapping);
expect(mapping[MappingClassId.properties]).toMatchSnapshot();
expect(mapping[MappingClassId.customProperties]).toMatchSnapshot();
});

it('should skip existing mapping properties on extended mapping', () => {
const mapping = [] as unknown as Mapping;
mapping[MappingClassId.properties] = [getMappingProperty()];
mapping[MappingClassId.customProperties] = [getMappingProperty()];
const mappingToExtend = [] as unknown as Mapping;
mappingToExtend[MappingClassId.properties] = [
getMappingProperty(),
getMappingProperty('bar'),
];

extend(mappingToExtend)(mapping);
expect(mapping[MappingClassId.properties]).toMatchSnapshot();
expect(mapping[MappingClassId.customProperties]).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe(mapWith.name, () => {
expect(mapper.map).toHaveBeenCalledWith(
{},
withDestination,
withSource
withSource,
undefined
);
});

Expand All @@ -35,7 +36,8 @@ describe(mapWith.name, () => {
expect(mapper.mapArray).toHaveBeenCalledWith(
[],
withDestination,
withSource
withSource,
undefined
);
});
});
47 changes: 47 additions & 0 deletions packages/integration-test/src/classes/issues/503/503.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { AutoMap, classes } from '@automapper/classes';
import {
CamelCaseNamingConvention,
createMap,
createMapper,
extend,
forMember,
nullSubstitution,
} from '@automapper/core';

export class Source {
@AutoMap(() => String)
foo!: string | null;
}

export class Destination {
@AutoMap()
foo?: string;
}

export class Destination2 extends Destination {}

describe('Issue 503', () => {
const mapper = createMapper({
strategyInitializer: classes(),
namingConventions: new CamelCaseNamingConvention(),
});

it('should map properly', () => {
const mapping = createMap(
mapper,
Source,
Destination,
forMember((d) => d.foo, nullSubstitution(undefined))
);
createMap(mapper, Source, Destination2, extend(mapping));

const source = new Source();
source.foo = null;

const destination = mapper.map(source, Source, Destination);
expect(destination.foo).toEqual(undefined);

const destination2 = mapper.map(source, Source, Destination2);
expect(destination2.foo).toEqual(undefined);
});
});

0 comments on commit b29f2ef

Please sign in to comment.