Mapping recursively problem
anrolmar opened this issue · 7 comments
Hi, I have the next dto object:
{
type: 'html',
data: [
{
type: 'body',
data: [
{
type: 'div',
data: [
{
text: 'This is the text of the div',
inlineStyleRange: [],
truth: true,
data: [],
id: 'fd90dbdb-7f1b-49d5-8ee6-9178d2bb96a5',
},
],
blockKey: 'v8z2j_0',
},
],
blockKey: 'wiyy6_109',
},
],
blockKey: 'at9qq_0',
}
I want to map this json object to a model class. I use this profile:
@Profile()
export class SectionProfile extends ProfileBase {
constructor() {
super();
}
configure(mapper: AutoMapper) {
mapper
.createMap(Section, SectionDto)
.reverseMap();
}
}
But, when I try to map it, it throws an error with the next message:
Metadata for data is a primitive or Array. Consider manual map this property
Any solutions?
You can find the code in the https://github.com/anrolmar/automapper-recursively
I took a look at your repo and I think you didn't provide enough information for @AutoMap()
. Here's my stackblitz showing the above dto should be mapped properly. https://stackblitz.com/edit/typescript-u8tzdr
But what happens with the AttributeDto
?
If I add to the SectionDto
class:
@AutoMap(() => AttributeDto)
attr?: AttributeDto;
add I add to the Section class:
@AutoMap(() => Attribute)
attr?: Attribute;
and the json is
{
type: 'html',
data: [{
type: 'body',
data: [{
type: 'div',
data: [{
text: 'This is the text of the div',
inlineStyleRanges: [],
truth: true,
data: [],
id: 'fd90dbdb-7f1b-49d5-8ee6-9178d2bb96a5',
}, ],
blockKey: 'v8z2j_0',
},
{
type: 'div',
data: [{
type: 'table',
data: [{
type: 'tr',
data: [{
type: 'td',
data: [{
type: 'div',
data: [{
attr: {
class: 'font3',
},
type: 'span',
data: [],
blockKey: 'iac6i_111',
},
{
text: '',
inlineStyleRanges: [],
truth: true,
data: [],
id: '51ed0427-07b6-4a62-b20e-7484eca778e6',
},
],
blockKey: 'mu66h_111',
}, ],
blockKey: '7yb69_110',
text: '\n',
inlineStyleRanges: [],
id: 'd7d0c9ea-049f-4eb1-8e3b-c1e2e58e2fed',
}, ],
}, ],
blockKey: 'e1lle_110',
}, ],
blockKey: '69zqg_114',
},
],
blockKey: 'wiyy6_109',
}, ],
blockKey: 'at9qq_0',
}
I create an AttributeProfile
and I add the import into app.module.ts
The message is:
Mapping not found for source class AttributeDto {} and destination class Attribute {}
@anrolmar I just updated the stackblitz to include attr
and it works just fine. Make sure you have either of the following:
mapper.createMap(Attribute, AttributeDto).reverseMap();
// OR
mapper.createMap(AttrbuteDto, Attribute);
I added the first option. But it doesn't work for me. I've updated my repository.
Thanks in advance.
Ok. It works. I forgot to add the createMap
into the constructor method.
Thanks
Glad that it worked for you.