Skip to content

Commit e5b7028

Browse files
committed
feat(core): add custom properties to isolate forMember configuration
ISSUE CLOSED: #497
1 parent 5f7080b commit e5b7028

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

packages/core/src/lib/mapping-configurations/for-member.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function forMember<
108108
];
109109
}
110110

111-
mapping[MappingClassId.properties].push([
111+
mapping[MappingClassId.customProperties].push([
112112
memberPath,
113113
mappingProperty,
114114
nestedMappingPair,

packages/core/src/lib/mappings/create-initial-mapping.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export function createInitialMapping<
7474
[source, destination],
7575
[sourceObject as TSource, destinationObject as TDestination],
7676
[],
77+
[],
7778
mapper,
7879
destinationConstructor,
7980
];
@@ -89,7 +90,8 @@ export function createInitialMapping<
8990
const destinationPaths = getPathRecursive(destinationObject);
9091

9192
const mappingProperties = mapping[MappingClassId.properties];
92-
const hasCustomMappingProperties = mappingProperties.length > 0;
93+
const customMappingProperties = mapping[MappingClassId.customProperties];
94+
const hasCustomMappingProperties = customMappingProperties.length > 0;
9395

9496
const namingConventions = mapping[MappingClassId.namingConventions];
9597

@@ -104,7 +106,7 @@ export function createInitialMapping<
104106
// for this destination path, skip it
105107
if (
106108
hasCustomMappingProperties &&
107-
mappingProperties.some((property) =>
109+
customMappingProperties.some((property) =>
108110
isPrimitiveArrayEqual(
109111
property[MappingPropertiesClassId.path],
110112
destinationPath
@@ -213,12 +215,17 @@ export function createInitialMapping<
213215
}
214216
}
215217

216-
mappingProperties.unshift([
218+
mappingProperties.push([
217219
destinationPath,
218220
[destinationPath, transformation],
219221
nestedMappingPair,
220222
]);
221223
}
222224

225+
// consolidate mapping properties
226+
for (const customMappingProperty of customMappingProperties) {
227+
mappingProperties.push(customMappingProperty);
228+
}
229+
223230
return mapping;
224231
}

packages/core/src/lib/mappings/map.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export function map<
113113
[sourceIdentifier, destinationIdentifier],
114114
[, destinationWithMetadata],
115115
propsToMap,
116+
,
116117
mapper,
117118
destinationConstructor,
118119
,

packages/core/src/lib/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ export const enum MappingClassId {
497497
identifiers,
498498
identifierMetadata,
499499
properties,
500+
customProperties,
500501
mapper,
501502
destinationConstructor,
502503
typeConverters,
@@ -527,6 +528,20 @@ export type Mapping<
527528
]
528529
]
529530
>,
531+
customProperties: Array<
532+
[
533+
path: string[],
534+
mappingProperty: MappingProperty<
535+
TSource,
536+
TDestination,
537+
SelectorReturn<TDestination>
538+
>,
539+
nestedMappingPair?: [
540+
destination: MetadataIdentifier | Primitive | Date,
541+
source: MetadataIdentifier | Primitive | Date
542+
]
543+
]
544+
>,
530545
mapper: Mapper,
531546
destinationConstructor: DestinationConstructor<TSource, TDestination>,
532547
typeConverters?: Map<
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { AutoMap, classes } from '@automapper/classes';
2+
import {
3+
CamelCaseNamingConvention,
4+
createMap,
5+
createMapper,
6+
} from '@automapper/core';
7+
8+
export class UserEntity {
9+
@AutoMap()
10+
id!: number;
11+
12+
@AutoMap()
13+
name!: string;
14+
15+
@AutoMap()
16+
sharedInfo!: string;
17+
18+
password!: string;
19+
}
20+
21+
export class UserResponse {
22+
@AutoMap()
23+
id!: number;
24+
25+
@AutoMap()
26+
name!: string;
27+
28+
@AutoMap()
29+
sharedInfo!: string;
30+
}
31+
32+
describe('Issue 497', () => {
33+
const mapper = createMapper({
34+
strategyInitializer: classes(),
35+
namingConventions: new CamelCaseNamingConvention(),
36+
});
37+
38+
it('should map properly', () => {
39+
createMap(mapper, UserEntity, UserResponse);
40+
41+
const user = new UserEntity();
42+
user.id = 123;
43+
user.name = 'Chau';
44+
user.sharedInfo = 'info';
45+
46+
const responses = mapper.mapArray([user], UserEntity, UserResponse);
47+
expect(responses).toEqual([
48+
{ id: 123, name: 'Chau', sharedInfo: 'info' },
49+
]);
50+
});
51+
});

0 commit comments

Comments
 (0)