Skip to content

Commit

Permalink
docs(classes): add comment about limitation of array properties (#326)
Browse files Browse the repository at this point in the history
Co-authored-by: huybn5776 <>
  • Loading branch information
huybn5776 authored Aug 1, 2021
1 parent 5f528ac commit 866e311
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs-site/docs/getting-started/introduce-to-automapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ We did the following:
- Removed the mapping logic in the DTOs
- Decorated all the properties with the same name with `@AutoMap()`. For nested model like `BioDto`, we provide a `typeFn` to `@AutoMap()`

> You can omit `typeFn` for non-array nested model, but for array property it's required. For more detail please check [Classes Limitations](plugins-system/classes-limitations/#nested-array-property)
## Create a `Mapping<User, UserDto>`

We've prepped our models with `@AutoMap()`, it is time to create the mappings. The mappings are created once and can be separated from the rest of other business logic code.
Expand Down
47 changes: 47 additions & 0 deletions docs-site/docs/plugins-system/classes-limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,50 @@ export class User {
```

This is due to **Weak Reflection** when `strict` mode is enabled.

## Nested Array property

If your model contain array property and does not specify any `forMember()` rule for them, then you *MUST* provide `typeFn` for it.

```ts
class User {
@AutoMap()
firstName!: string;
@AutoMap()
lastName!: string;
@AutoMap()
profile!: UserProfile;
@AutoMap({ typeFn: () => Job }) // <- it's required for nested array property
jobs!: Job[];
}

mapper.createMap(User, UserVm);
```

AutoMapper will attempt to get the data type of the property through reflection. In most cases, it will work without any problem, but for array property, it will only result an `Array` type instead of `Job[]` above. It's currently an [open issue](https://github.com/microsoft/TypeScript/issues/7169) of TypeScript.

Or you can also use `mapWith()` to let AutoMapper know its type.

```ts
class User {
@AutoMap()
firstName!: string;
@AutoMap()
lastName!: string;
@AutoMap()
profile!: UserProfile;
@AutoMap() // <- omit typeFn
jobs!: Job[];
}

mapper
.createMap(User, UserVm)
.forMember(
(d) => d.jobs,
mapWith(
() => JobVm,
(s) => s.profile,
() => Job
)
);
```

0 comments on commit 866e311

Please sign in to comment.