Skip to content

Commit 34b31de

Browse files
authored
Merge pull request #166 from gregolsky/v4.0
fix @metadata serialization
2 parents 2e52e14 + 4607c31 commit 34b31de

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/Mapping/ObjectMapper.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getLogger } from "../Utility/LogUtil";
66
import { StringUtil } from "../Utility/StringUtil";
77
import { DocumentConventions } from "../Documents/Conventions/DocumentConventions";
88
import { ObjectUtil } from "../Utility/ObjectUtil";
9+
import { CONSTANTS } from "../Constants";
910

1011
const log = getLogger({ module: "ObjectMapper" });
1112

@@ -379,29 +380,38 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
379380
obj: object,
380381
objPathPrefix: string,
381382
typeInfoCallback: (types: NestedTypes) => void,
382-
knownTypes: ObjectTypeDescriptor[]): any {
383+
knownTypes: ObjectTypeDescriptor[],
384+
skipTypes: boolean = false): any {
383385

384386
if (TypeUtil.isDate(obj)) {
385-
typeInfoCallback({
386-
[objPathPrefix]: "date"
387-
});
387+
if (!skipTypes) {
388+
typeInfoCallback({
389+
[objPathPrefix]: "date"
390+
});
391+
}
388392

389393
return this._conventions.dateUtil.stringify(obj as Date);
390394
}
391395

392396
if (TypeUtil.isSet(obj)) {
393-
typeInfoCallback({
394-
[objPathPrefix]: "Set"
395-
});
397+
if (!skipTypes) {
398+
typeInfoCallback({
399+
[objPathPrefix]: "Set"
400+
});
401+
}
402+
396403
const newObjPathPrefix = `${objPathPrefix}$SET`;
397404
return Array.from((obj as Set<any>))
398405
.map(x => this._makeObjectLiteral(x, newObjPathPrefix, typeInfoCallback, knownTypes));
399406
}
400407

401408
if (TypeUtil.isMap(obj)) {
402-
typeInfoCallback({
403-
[objPathPrefix]: "Map"
404-
});
409+
if (!skipTypes) {
410+
typeInfoCallback({
411+
[objPathPrefix]: "Map"
412+
});
413+
}
414+
405415
const valuePathPrefix = `${objPathPrefix}$MAP`;
406416
const map = obj as Map<any, any>;
407417
return Array.from(map.entries()).reduce((result, [ name, value ]) => {
@@ -423,7 +433,8 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
423433
if (TypeUtil.isObject(obj)) {
424434
if (objPathPrefix) { // if it's non-root object
425435
const matchedType = TypeUtil.findType(obj, knownTypes);
426-
if (matchedType
436+
if (!skipTypes
437+
&& matchedType
427438
&& matchedType.name !== "Function") {
428439
typeInfoCallback({ [objPathPrefix]: matchedType.name });
429440
}
@@ -436,8 +447,12 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
436447
nestedTypeInfoKey = ObjectUtil[this._conventions.remoteEntityFieldNameConvention](key);
437448
}
438449

450+
if (!skipTypes) {
451+
skipTypes = key === CONSTANTS.Documents.Metadata.KEY;
452+
}
453+
439454
const fullPath = objPathPrefix ? `${objPathPrefix}.${nestedTypeInfoKey}` : nestedTypeInfoKey;
440-
result[key] = this._makeObjectLiteral(obj[key], fullPath, typeInfoCallback, knownTypes);
455+
result[key] = this._makeObjectLiteral(obj[key], fullPath, typeInfoCallback, knownTypes, skipTypes);
441456
return result;
442457
}, {});
443458
}

test/Mapping/ObjectMapperTests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,27 @@ describe("ObjectMapper", function () {
483483
typeInfoCallback = (_typeInfo) => typeInfo = _typeInfo;
484484
});
485485

486+
it("skips types from @metadata", () => {
487+
488+
class Empty {}
489+
490+
const testObject = {
491+
"test": new Date(),
492+
"@metadata": {
493+
ignoredType: new Date(),
494+
withClass: new Empty(),
495+
inner: {
496+
innerInner: new Empty()
497+
}
498+
}
499+
};
500+
501+
const result = mapper.toObjectLiteral(testObject, typeInfoCallback);
502+
const fields = Object.keys(typeInfo.nestedTypes);
503+
assert.strictEqual(fields.length, 1);
504+
assert.strictEqual(fields[0], "test");
505+
});
506+
486507
it("can handle Date type", () => {
487508

488509
const testObject = {

0 commit comments

Comments
 (0)