Skip to content

Commit 6bd5e9e

Browse files
authored
Merge pull request #100 from tbreug/patch-1
Update MetadataStorage.ts
2 parents 7f2ad5b + 209c2bd commit 6bd5e9e

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/metadata/MetadataStorage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,19 @@ export class MetadataStorage {
146146

147147
private getMetadata<T extends { target: Function, propertyName: string }>(metadatas: T[], target: Function): T[] {
148148
const metadataFromTarget = metadatas.filter(meta => meta.target === target && meta.propertyName !== undefined);
149-
const metadataFromChildren = metadatas.filter(meta => target.prototype instanceof meta.target && meta.propertyName !== undefined);
149+
const metadataFromChildren = metadatas.filter(meta => target && target.prototype instanceof meta.target && meta.propertyName !== undefined);
150150
return metadataFromChildren.concat(metadataFromTarget);
151151
}
152152

153153
private findMetadata<T extends { target: Function, propertyName: string }>(metadatas: T[], target: Function, propertyName: string): T {
154154
const metadataFromTarget = metadatas.find(meta => meta.target === target && meta.propertyName === propertyName);
155-
const metadataFromChildren = metadatas.find(meta => target.prototype instanceof meta.target && meta.propertyName === propertyName);
155+
const metadataFromChildren = metadatas.find(meta => target && target.prototype instanceof meta.target && meta.propertyName === propertyName);
156156
return metadataFromTarget || metadataFromChildren;
157157
}
158158

159159
private findMetadatas<T extends { target: Function, propertyName: string }>(metadatas: T[], target: Function, propertyName: string): T[] {
160160
const metadataFromTarget = metadatas.filter(meta => meta.target === target && meta.propertyName === propertyName);
161-
const metadataFromChildren = metadatas.filter(meta => target.prototype instanceof meta.target && meta.propertyName === propertyName);
161+
const metadataFromChildren = metadatas.filter(meta => target && target.prototype instanceof meta.target && meta.propertyName === propertyName);
162162
return metadataFromChildren.reverse().concat(metadataFromTarget.reverse());
163163
}
164164

test/functional/custom-transform.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "reflect-metadata";
2+
import {expect} from "chai";
23
import {classToPlain, plainToClass} from "../../src/index";
34
import {defaultMetadataStorage} from "../../src/storage";
45
import {Expose, Transform, Type} from "../../src/decorators";
@@ -157,5 +158,63 @@ describe("custom transformation decorator", () => {
157158
objArg.should.be.equal(user);
158159
typeArg.should.be.equal(TransformationType.CLASS_TO_PLAIN);
159160
});
161+
162+
let model: any;
163+
it ("should serialize json into model instance of class Person", () => {
164+
expect(() => {
165+
const json = {
166+
name: "John Doe",
167+
address: {
168+
street: "Main Street 25",
169+
tel: "5454-534-645",
170+
zip: 10353,
171+
country: "West Samoa"
172+
},
173+
age: 25,
174+
hobbies: [
175+
{ type: "sport", name: "sailing" },
176+
{ type: "relax", name: "reading" },
177+
{ type: "sport", name: "jogging" },
178+
{ type: "relax", name: "movies" }
179+
]
180+
};
181+
class Hobby {
182+
public type: string;
183+
public name: string;
184+
}
185+
class Address {
186+
public street: string;
187+
188+
@Expose({ name: "tel" })
189+
public telephone: string;
190+
191+
public zip: number;
192+
193+
public country: string;
194+
}
195+
class Person {
196+
public name: string;
197+
198+
@Type(() => Address)
199+
public address: Address;
200+
201+
@Type(() => Hobby)
202+
@Transform(value => value.filter((hobby: any) => hobby.type === "sport"), { toClassOnly: true })
203+
public hobbies: Hobby[];
204+
205+
public age: number;
206+
}
207+
model = plainToClass(Person, json);
208+
expect(model instanceof Person);
209+
expect(model.address instanceof Address);
210+
model.hobbies.forEach((hobby: Hobby) => expect(hobby instanceof Hobby && hobby.type === "sport"));
211+
}).to.not.throw();
212+
});
213+
214+
it ("should serialize a model into json", () => {
215+
expect(() => {
216+
classToPlain(model);
217+
}).to.not.throw();
218+
});
160219

161220
});

0 commit comments

Comments
 (0)