Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: classToPlain provoke "'constructor' of null" error when used on null class members that defines the discriminator option #790

Open
bpeab opened this issue Jun 24, 2021 · 2 comments · May be fixed by #1118
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@bpeab
Copy link

bpeab commented Jun 24, 2021

Description

Both classToPlain and serialize are provoking the following error TypeError: Cannot read property 'constructor' of null when called on classes that have members with the @Type decorator defining the discriminator option and that's null or undefined once initialized.

Minimal code-snippet showcasing the problem

class Photo {
  @Expose()
  type?: Landscape | Portrait;
  // ...
}

class Landscape extends Photo {
  @Expose()
  type = Landscape;
  // ...
}

class Portrait extends Photo {
  @Expose()
  type = Portrait;
  // ...
}

class Camera {
  @Expose({ name: 'foto' })
  @Type(() => Photo, {
    discriminator: {
      property: 'type',
      subTypes: [
        {
          name: 'Landscape',
          value: Landscape,
        },
        {
          name: 'Portrait',
          value: Portrait,
        },
      ],
    },
  })
  photo?: Landscape | Portrait;
}

const camera = plainToClass(Camera, {});
// or.. new Camera()
console.log(classToPlain(camera));
// or.. console.log(serialize(camera))

Expected behavior

The functions classToPlain and serialize shouldn't treat class members that defines @Type decorator with discriminator option when they are null or undefined.

Actual behavior

The functions classToPlain and serialize fire the following error: TypeError: Cannot read property 'constructor' of null. It's likely that the logic doesn't verify if there's a value before proceeding with the transformation.

The following code snippet (TransformOperationExecutor.js, line 209) should verify if there's value before proceeding (perhaps a simple && subValue added to the condition does the trick).

if (this_1.transformationType === TransformationType.CLASS_TO_PLAIN) {
  subValue[metadata_1.options.discriminator.property] = metadata_1.options.discriminator.subTypes.find(function (subType) { return subType.value === subValue.constructor; }).name;
}

It's likely that is not the only place where it should be fixed.

@bpeab bpeab added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Jun 24, 2021
@bpeab
Copy link
Author

bpeab commented Jun 24, 2021

I saw that the issue was already solved with #521 but not released. Any ETA on when that could be done ? Thanks

@kol-93 kol-93 linked a pull request Feb 21, 2022 that will close this issue
6 tasks
@balkarov
Copy link

I have the same problem. Any workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

Successfully merging a pull request may close this issue.

2 participants