AutoMapper icon indicating copy to clipboard operation
AutoMapper copied to clipboard

New instance of nested object is not created instead the original is referred in the destination

Open mamukundan opened this issue 7 years ago • 1 comments

In the below example, a new Address object is not created and populated with values and then attached to the personDTO object. Instead, the address object, that is attached to the person is referred in the personDTO object as well.

'use strict';

import { expect } from "chai"; import { } from "automapper-ts";

it('should map nested objects correctly', () => { class Person{ name: string; age: number; address: Address; } class PersonDTO{ name: string; address: Address; } class Address{ street: string; pincode: number; } let address: Address = new Address(); address.street = "Seaside Lane"; address.pincode = 51241; let person: Person = new Person(); person.name = "John Doe"; person.address = address; let sourceKey = "Person"; let destinationKey = "PersonDTO"; automapper.createMap(sourceKey, destinationKey); let personDTO: PersonDTO = automapper.map(sourceKey, destinationKey, person); console.log(person.address.pincode); console.log(personDTO.address.pincode); personDTO.address.pincode=0; //if i change the address in personDTO it impacts address in person as well console.log(person.address.pincode); console.log(personDTO.address.pincode); expect(personDTO.name).to.equal(person.name); expect(personDTO.address.street).to.equal(person.address.street); expect(personDTO.address.pincode).to.equal(person.address.pincode); });

mamukundan avatar Oct 09 '17 13:10 mamukundan

Eeeks, can you format this @mamukundan and show the output?

oehm-smith avatar May 18 '19 06:05 oehm-smith