Skip to content

question: plainToInstance not creating an instance in a basic example from docs? #1798

Open
@moltco

Description

@moltco

I was trying to... serialize/deserialize a class with some transformation (rounding numbers to 3rd digit) and serialize/deserialize class that contains plain class inside to test if transformation in a nested class will be picked up automatically.

The problem:
When I serialize the class, transformations work. However, when I try to deserialize using plainToInstance as per this repo's readme file, I do not receive instance of the object but rather a string back which is not as the documentation suggests.

This is pretty similar to the basic usage scenario from the documentation so I am puzzled why is it not transforming into an object, perhaps I am missing something and/or the docs need an update?

A simple code demonstrating what I done is here https://stackblitz.com/edit/class-transformer-playground-3u1swce5?file=index.ts
In case the stackblitz does not work for some reason here is the code as well

/* eslint-disable @typescript-eslint/no-explicit-any */
import 'reflect-metadata';
import {
  Expose,
  instanceToPlain,
  plainToInstance,
  serialize,
  Transform,
  Type,
} from 'class-transformer';

// Simple class with rounding when serialized (but not when deserialized)
class Point {
  @Transform(({ value }) => parseFloat(value.toFixed(3)), { toPlainOnly: true })
  x: number;
  @Transform(({ value }) => parseFloat(value.toFixed(3)), { toPlainOnly: true })
  y: number;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }

  getMe() {
    console.log('Point: getMe', this.x, this.y);
  }
}

// class using subclass with transformations and a method
class TwoPoints {
  @Expose()
  @Type(() => Point)
  one: Point;
  @Expose()
  @Type(() => Point)
  two: Point;

  constructor(one: Point, two: Point) {
    this.one = one;
    this.two = two;
  }

  getMe() {
    console.log('TwoPoint: getMe', this.one, this.two);
  }
}
// first test rounding when serializing the simple Point - works OK
const p = new Point(3.4559, 1.4449);
const plainJson = JSON.stringify(p);
const result1 = serialize(p);
const result2 = JSON.stringify(instanceToPlain(p));
console.log('Point', result1, 'or', result2);

// now let's test if rounding works when we serialize complex TwoPoints - works OK
const p2 = new Point(10.9999, 11.3234);
const twoPoints = new TwoPoints(p, p2);
const result3 = serialize(twoPoints);
const result4 = JSON.stringify(instanceToPlain(twoPoints));
const result5 = JSON.stringify(twoPoints);
console.log('Two points', result3, 'or', result4);

// now let's try to deserialize Point to instance - FAILS
console.log('Point source', plainJson);
const newPoint = plainToInstance(Point, plainJson);
console.log('Point result', newPoint instanceof Point, newPoint); // FAIL: not an instanceof
newPoint.getMe(); // Fails: Not a function

// deserialize TwoPoint to instance - FAILS
console.log('Source', result5); 
const newTwoPoint = plainToInstance(TwoPoints, result5);
console.log('Result', newTwoPoint instanceof TwoPoints, newTwoPoint); // FAIL: not an instanceof
newTwoPoint.getMe(); // Fails: Not a function

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions