wufe/mapper

Bidirectional mapping

Closed this issue · 2 comments

When I use unidirectional mapping from a model entity to dto, it works perfectly. But I want to use a bidirectional mapping between model entities and dtos so I can map an incoming dto to an entity in addition. I don't know if the error occurs in the odata framework or in the mapper. Is a bidirectional mapping possible? Is there a bugor am I using the mapper incorrectly?

@mapTo(UserDTO)
export default class User {
    private _email: string;

    get email(): string {
        return this._email;
    }

    set email(value: string) {
        this._email = value;
    }
}
@mapTo(User)
export default class UserDTO {
    @Edm.Key
    @Edm.String
    public _email: string;
}
@odata.type(UserDTO)
export default class UserController extends ODataController {
    @odata.GET
    public get(@odata.key email: string) {
        let user: User = UserServiceFacade.getInstance().get(email);

        return map<User, UserDTO>(user);
    }

    @odata.POST
    public add(@odata.body body: any) {
        let userDTO: UserDTO = deserialize<UserDTO>(UserDTO, body);
        let user: User = map<UserDTO, User>(userDTO);
        UserServiceFacade.getInstance().add(user);

        return userDTO;
    }
}

HTTP GET User Errorlog:

{
    "error": {
        "code": 500,
        "message": "t.destination is not a constructor",
        "stack": "TypeError: t.destination is not a constructor\n    at i.Map.internalMap 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\node_modules\\@wufe\\mapper\\dist\\mapper.js:1:2426)\n    at i.Map.map 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\node_modules\\@wufe\\mapper\\dist\\mapper.js:1:1761)\n    at i.Mapper.map 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\node_modules\\@wufe\\mapper\\dist\\mapper.js:1:5215)\n    at Object.map 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\src\\util\\Mappy.ts:12:19)\n    at Function.get 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\src\\odata\\controller\\UserController.ts:24:16)\n    at Function.fnCaller 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\node_modules\\odata-v4-server\\src\\lib\\processor.ts:145:15)\n    at ODataProcessor.<anonymous> 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\node_modules\\odata-v4-server\\src\\lib\\processor.ts:902:50)\n    at Generator.next (<anonymous>)\n    at fulfilled 
            (C:\\Users\\.\\workspace\\boilerblate-odata\\node_modules\\tslib\\tslib.js:107:62)\n    at process._tickCallback (internal/process/next_tick.js:68:7)"
    }

wufe commented

Try to use @mapto(User, true).
The second argument should include a reverse map.

Thanks, it works like a charm.