Skip to content

Convert Nested Object #15

Description

@andreluiz1987

Hi.
Congratulations on the lib, I was looking for a solution similar to Java's mapstruct for typeScript and I found yours.

I have a problem and maybe you can help me. When I have a nested object I can't mapper correctly.

My code:

class Event {
    event_id: string = "xpto";
    product: Product = new Product;
}

class Product {
    id: Number = 0;
    name: string = "any-name";
}

class Template {
    id: Number = 0;
    name: string = "";
}

@Mapper({ sourceType: Product, targetType: Template }, [
    { source: "name", target: "name" }
])
class ProductToTemplateMapper extends Converter<Product, Template> {}

@Mapper({ sourceType: Event, targetType: Template }, [
    { source: "event_id", target: "id"},
    { source: "product", target: "name", converter: ProductToTemplateMapper}
])
class EventToTemplateMapper extends Converter<Event, Template> {}

const event = new Event();
const mapper = new EventToTemplateMapper();
const template = mapper.convert(event);
console.log(template);

the result is this:
{ id: 'xpto', name: Template { id: 0, name: 'any-name' } }

but I would like it to be:
{ id: 'xpto', name: 'any-name' } }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions