Skip to content

Commit eab25cd

Browse files
authored
Merge pull request #151 from gregolsky/v4.0
RDBC-233 Do not revive types if there's a null midway when resolving …
2 parents 203478a + 14390d9 commit eab25cd

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

src/Mapping/MetadataAsDictionary.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class MetadataInternal {
4343
}
4444

4545
private _metadataConvertValue(key, val) {
46+
if (!val) {
47+
return null;
48+
}
49+
4650
if (typeof val !== "object") {
4751
return val;
4852
}

src/Mapping/ObjectMapper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
133133

134134
private _getFieldContext(parent: object, objPath: string[])
135135
: ObjectPropertyContext | ObjectPropertyContext[] {
136+
137+
if (!parent) {
138+
return null;
139+
}
140+
136141
// tslint:disable-next-line:prefer-const
137142
let [field, ...fieldsPathTail] = objPath;
138143

test/Documents/SessionApiTests.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,29 @@ describe("Session API dev experience tests", function () {
258258
await session.saveChanges();
259259
}
260260
});
261+
262+
it("loads @metadata.Raven-Node-Type as null", async () => {
263+
store.conventions.findCollectionNameForObjectLiteral =
264+
(e: any) => e["collection"];
265+
266+
{
267+
const user: any = {
268+
collection: "Users",
269+
name: "John"
270+
};
271+
const session = store.openSession();
272+
await session.store(user, "users/1");
273+
await session.saveChanges();
274+
}
275+
276+
{
277+
const session = store.openSession();
278+
const loaded = await session.load("users/1");
279+
assert.strictEqual(loaded["@metadata"]["Raven-Node-Type"], null);
280+
assert.strictEqual(loaded["@metadata"]["@collection"], "Users");
281+
}
282+
283+
});
261284
});
262285

263286
describe("using classes", async () => {

test/Issues/RDBC_233.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import * as mocha from "mocha";
2+
import * as BluebirdPromise from "bluebird";
3+
import * as assert from "assert";
4+
import { testContext, disposeTestDocumentStore } from "../Utils/TestUtil";
5+
6+
import DocumentStore, {
7+
RavenErrorType,
8+
GetNextOperationIdCommand,
9+
IDocumentStore,
10+
TypesAwareObjectMapper,
11+
DocumentConventions,
12+
PatchOperation,
13+
} from "../../src";
14+
import { DateUtil } from "../../src/Utility/DateUtil";
15+
import { PatchRequest } from "../../src/Documents/Operations/PatchRequest";
16+
17+
describe("RDBC-233", function () {
18+
19+
let mapper: TypesAwareObjectMapper;
20+
21+
beforeEach(() => {
22+
mapper = new TypesAwareObjectMapper({
23+
documentConventions: DocumentConventions.defaultConventions
24+
});
25+
});
26+
27+
it("object mapper does not revive types if there's null midway", async () => {
28+
const obj = {
29+
date: null,
30+
anotherOne: {
31+
start: "2018-01-02",
32+
end: "2018-01-03"
33+
}
34+
};
35+
36+
const result: any = mapper.fromObjectLiteral(obj, {
37+
nestedTypes: {
38+
"date.start": "date",
39+
"date.end": "date",
40+
"anotherOne.start": "date",
41+
"anotherOne.end": "date",
42+
}
43+
});
44+
45+
assert.ok(result);
46+
assert.strictEqual(result.date, null);
47+
assert.ok(result.anotherOne.start instanceof Date);
48+
assert.strictEqual(result.anotherOne.start.toString(), DateUtil.parse(obj.anotherOne.start).toString());
49+
assert.strictEqual(result.anotherOne.end.toString(), DateUtil.parse(obj.anotherOne.end).toString());
50+
});
51+
52+
describe("entire flow - store, patch, load", async () => {
53+
54+
let store: IDocumentStore;
55+
56+
beforeEach(async function () {
57+
store = await testContext.getDocumentStore();
58+
});
59+
60+
afterEach(async () => disposeTestDocumentStore(store));
61+
62+
it("does not revive types if there's null midway", async () => {
63+
const docStore = new DocumentStore(store.urls, store.database);
64+
docStore.conventions.findCollectionNameForObjectLiteral = () => "test";
65+
docStore.initialize();
66+
67+
try {
68+
{
69+
const doc = {
70+
date: {
71+
start: new Date(),
72+
end: new Date()
73+
}
74+
};
75+
76+
const session = docStore.openSession();
77+
await session.store(doc, "test/1");
78+
await session.saveChanges();
79+
}
80+
81+
{
82+
const patchOp = new PatchOperation(
83+
"test/1", null, PatchRequest.forScript("this.date = null;"));
84+
await docStore.operations.send(patchOp);
85+
}
86+
87+
{
88+
const session = docStore.openSession();
89+
const doc: any = await session.load("test/1");
90+
assert.ok(doc);
91+
assert.ok("date" in doc);
92+
assert.strictEqual(doc["@metadata"]["@nested-object-types"]["date.start"], "date");
93+
assert.strictEqual(doc.date, null);
94+
}
95+
} finally {
96+
docStore.dispose();
97+
}
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)