Skip to content

Commit 09d09ee

Browse files
author
Umed Khudoiberdiev
committed
removed exception when type is missing
1 parent f77b29c commit 09d09ee

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Class-transformer allows you to transform plain object to some instance of class
1212
Also it allows to serialize / deserialize object based on criteria.
1313
This tool is super useful on both frontend and backend.
1414

15-
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R). Source code is [here](https://github.com/pleerock/class-transformer-demo).
15+
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R).
16+
Source code is [here](https://github.com/pleerock/class-transformer-demo).
1617

1718
## Installation
1819

@@ -564,7 +565,7 @@ import {plainToClass} from "class-transformer";
564565
this.http
565566
.get("users.json")
566567
.map(res => res.json())
567-
.map(res => plainToClass(User, res))
568+
.map(res => plainToClass(User, res as Object[]))
568569
.subscribe(users => {
569570
// now "users" is type of User[] and each user have getName() and isAdult() methods available
570571
console.log(users);
@@ -573,6 +574,9 @@ this.http
573574

574575
You can also inject a class `ClassTransformer` as a service in `providers`, and use its methods.
575576

577+
Example how to use with angular 2 in [plunker](http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R).
578+
Source code is [here](https://github.com/pleerock/class-transformer-demo).
579+
576580
## Samples
577581

578582
Take a look on samples in [./sample](https://github.com/pleerock/class-transformer/tree/master/sample) for more examples of

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "class-transformer",
3-
"version": "0.1.0-beta.8",
3+
"version": "0.1.0-beta.9",
44
"description": "Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors",
55
"license": "MIT",
66
"readmeFilename": "README.md",

src/TransformOperationExecutor.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ export class TransformOperationExecutor {
3030
targetType: Function,
3131
arrayType: Function,
3232
isMap: boolean,
33-
fromProperty: string,
3433
level: number = 0) {
3534

3635
if (value instanceof Array || value instanceof Set) {
3736
const newValue = arrayType && this.transformationType === "plainToClass" ? new (arrayType as any)() : [];
3837
(value as any[]).forEach((subValue, index) => {
3938
const subSource = source ? source[index] : undefined;
4039
if (!this.isCircular(subValue, level)) {
41-
const value = this.transform(subSource, subValue, targetType, undefined, subValue instanceof Map, fromProperty + "[" + String(index) + "]", level + 1);
40+
const value = this.transform(subSource, subValue, targetType, undefined, subValue instanceof Map, level + 1);
4241
if (newValue instanceof Set) {
4342
newValue.add(value);
4443
} else {
@@ -81,14 +80,12 @@ export class TransformOperationExecutor {
8180
const keys = this.getKeys(targetType, value);
8281
let newValue: any = source ? source : {};
8382
if (!source && (this.transformationType === "plainToClass" || this.transformationType === "classToClass")) {
84-
if (!targetType) {
85-
console.log(value);
86-
throw new Error(`Cannot determine type for ${fromProperty}, did you forget to specify a @Type?`);
87-
}
8883
if (isMap) {
8984
newValue = new Map();
90-
} else {
85+
} else if (targetType) {
9186
newValue = new (targetType as any)();
87+
} else {
88+
newValue = {};
9289
}
9390
}
9491

@@ -155,7 +152,7 @@ export class TransformOperationExecutor {
155152
}
156153

157154
if (!this.isCircular(subValue, level)) {
158-
let finalValue = this.transform(subSource, subValue, type, arrayType, isSubValueMap, (targetType as any).name + "." + propertyName, level + 1);
155+
let finalValue = this.transform(subSource, subValue, type, arrayType, isSubValueMap, level + 1);
159156
finalValue = this.applyCustomTransformations(finalValue, targetType, key);
160157
if (newValue instanceof Map) {
161158
newValue.set(newValueKey, finalValue);

0 commit comments

Comments
 (0)