Skip to content

Commit

Permalink
fix(core): skip key that is a method in getPathRecursive
Browse files Browse the repository at this point in the history
Class methods are stored on the prototype of the class. Hence, it does not make sense to try to
automap any class methods. Naive approach is to check if the type of the property is function, then
we skip it

fix #396
  • Loading branch information
nartc committed Jan 10, 2022
1 parent 9645929 commit 5a56529
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ export function getPathRecursive(
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
const path: string[] = [...prefix, key];
const child = node[key];

if (typeof child === 'function') {
continue;
}
result.push(path);

const child = node[key];
if (typeof child === 'object') {
const queue = Array.isArray(child) ? child : [child];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { AutoMap } from '@automapper/classes';

export class NestedOptionalClass {
@AutoMap()
name?: string;

public getFullName(): string {
return this.name;
}
}

export class Source {
@AutoMap()
description?: string;
@AutoMap({ typeFn: () => NestedOptionalClass })
options?: NestedOptionalClass;
}

export class SourceChild extends Source {
@AutoMap()
another?: string;
}

export class Destination {
@AutoMap()
description?: string;
@AutoMap({ typeFn: () => NestedOptionalClass })
options?: NestedOptionalClass;
}

export class DestinationChild extends Destination {
@AutoMap()
another?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { mapFrom, MappingProfile } from '@automapper/core';
import {
Destination,
DestinationChild,
Source,
SourceChild,
} from '../models/issue-396';

export const issue396Profile: MappingProfile = (mapper) => {
mapper.createMap(Source, Destination).forMember(
(d) => d.options,
mapFrom((s) => s.options)
);
mapper.createMap(SourceChild, DestinationChild, {
extends: [mapper.getMapping(Source, Destination)],
});
};
32 changes: 32 additions & 0 deletions packages/integration-test/src/lib/with-classes/issue-396.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { setupClasses } from '../setup.spec';
import { CamelCaseNamingConvention } from '@automapper/core';
import { issue396Profile } from './fixtures/profiles/issue-396.profile';
import {
DestinationChild,
NestedOptionalClass,
SourceChild,
} from './fixtures/models/issue-396';

describe('Issue 396', () => {
const [mapper] = setupClasses('issue-396', new CamelCaseNamingConvention());

it('should map', () => {
mapper.addProfile(issue396Profile);

const options = new NestedOptionalClass();
options.name = 'name';

const input1 = new SourceChild();
input1.description = 'description';
input1.options = options;

const output1 = mapper.map(input1, DestinationChild, SourceChild);
expect(output1).toBeTruthy();

const input2 = new SourceChild();
input2.description = 'description 2';

const output2 = mapper.map(input2, DestinationChild, SourceChild);
expect(output2).toBeTruthy();
});
});

0 comments on commit 5a56529

Please sign in to comment.