Skip to content

Commit 6d46068

Browse files
authored
Merge pull request #359 from ml054/v5.4
merge v5.2 -> v5.4
2 parents 9505984 + 3d09248 commit 6d46068

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

src/Documents/Commands/Batches/SingleNodeBatchCommand.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export class SingleNodeBatchCommand extends RavenCommand<BatchCommandResult> imp
144144
.objectKeysTransform({
145145
defaultTransform: "camel",
146146
ignoreKeys: [/^@/],
147+
ignorePaths: [/results\.\[\]\.modifiedDocument\./i],
147148
})
148149
.process(bodyStream);
149150
return body;

src/Mapping/ObjectMapper.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectTypeDescriptor, ClassConstructor, ObjectLiteralDescriptor, EntityConstructor } from "../Types";
1+
import { ObjectTypeDescriptor, ObjectLiteralDescriptor, EntityConstructor } from "../Types";
22
import { throwError } from "../Exceptions";
33
import { TypeUtil } from "../Utility/TypeUtil";
44
import { getLogger } from "../Utility/LogUtil";
@@ -338,8 +338,7 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
338338
if (!ctorOrTypeDescriptor) {
339339
instance = Object.assign({}, rawValue);
340340
} else if (TypeUtil.isClass(ctorOrTypeDescriptor)) {
341-
instance = this._createEmptyObject(ctorOrTypeDescriptor as ClassConstructor);
342-
instance = Object.assign(instance, rawValue);
341+
instance = this.createEmptyObject(ctorOrTypeDescriptor, rawValue);
343342
} else if (TypeUtil.isObjectLiteralTypeDescriptor(ctorOrTypeDescriptor)) {
344343
instance = (ctorOrTypeDescriptor as ObjectLiteralDescriptor).construct(rawValue);
345344
} else {
@@ -367,12 +366,12 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
367366
return ctorOrTypeDescriptor;
368367
}
369368

370-
private _createEmptyObject<TResult extends object>(ctor: EntityConstructor<TResult>) {
369+
protected createEmptyObject<TResult extends object>(ctor: EntityConstructor<TResult>, rawValue: object) {
371370
if (!ctor) {
372371
throwError("InvalidArgumentException", "ctor argument must not be null or undefined.");
373372
}
374373

375-
return new ctor() as TResult;
374+
return Object.assign(new ctor(), rawValue) as TResult;
376375
}
377376

378377
private _makeObjectLiteral(
@@ -445,12 +444,13 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
445444
nestedTypeInfoKey = ObjectUtil[this._conventions.remoteEntityFieldNameConvention](key);
446445
}
447446

447+
let innerSkipTypes = skipTypes
448448
if (!skipTypes) {
449-
skipTypes = key === CONSTANTS.Documents.Metadata.KEY;
449+
innerSkipTypes = key === CONSTANTS.Documents.Metadata.KEY;
450450
}
451451

452452
const fullPath = objPathPrefix ? `${objPathPrefix}.${nestedTypeInfoKey}` : nestedTypeInfoKey;
453-
result[key] = this._makeObjectLiteral(obj[key], fullPath, typeInfoCallback, knownTypes, skipTypes);
453+
result[key] = this._makeObjectLiteral(obj[key], fullPath, typeInfoCallback, knownTypes, innerSkipTypes);
454454
return result;
455455
}, {});
456456
}

src/Utility/TypeUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class TypeUtil {
6363
return value === true || value === false;
6464
}
6565

66-
public static isClass(value: any): boolean {
66+
public static isClass(value: any): value is ClassConstructor {
6767
return this.isFunction(value) && ("name" in value) && value.name !== ""
6868
&& ("Object" !== value.name)
6969
&& (!!value.prototype && !!value.prototype.constructor.name);

test/Documents/CustomKeyCaseConventionsTests.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { testContext, disposeTestDocumentStore } from "../Utils/TestUtil";
22
import * as assert from "assert";
33
import DocumentStore, {
4-
IDocumentStore,
4+
IDocumentStore, PatchCommandData, PatchRequest,
55
} from "../../src";
66
import { assertThat } from "../Utils/AssertExtensions";
77

@@ -391,4 +391,33 @@ describe("With custom key case conventions set", function () {
391391
assert.strictEqual(loaded.Equipment[0].Name, stored.equipment[0].name);
392392
}
393393
});
394+
395+
it("returns correct modified document after patch", async () => {
396+
class PascalDoc {
397+
public SIPCall: string
398+
}
399+
400+
{
401+
const session = regularStore.openSession();
402+
const pascalDoc = new PascalDoc();
403+
pascalDoc.SIPCall = "RavenDB";
404+
405+
await session.store(pascalDoc, "pascal/1");
406+
await session.saveChanges();
407+
}
408+
let modifiedDocument
409+
regularStore.addSessionListener("afterSaveChanges", event => {
410+
modifiedDocument = event.entity
411+
});
412+
413+
const session = regularStore.openSession();
414+
await session.load("pascal/1");
415+
416+
const patchRequest = PatchRequest.forScript("this.SIPCall = \"Patched\"");
417+
session.advanced.defer(new PatchCommandData("pascal/1", null, patchRequest))
418+
await session.saveChanges();
419+
420+
assert.strictEqual(modifiedDocument.SIPCall, "Patched")
421+
assert.ok(!("sipCall" in modifiedDocument));
422+
});
394423
});

test/Issues/RDBC_681.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { IDocumentStore } from "../../src";
2+
import { disposeTestDocumentStore, testContext } from "../Utils/TestUtil";
3+
import * as assert from "assert";
4+
5+
6+
describe("[RDBC-681] @nested-object-types doesn’t store type information for newly added fields on update", function () {
7+
8+
let store: IDocumentStore;
9+
10+
beforeEach(async function () {
11+
store = await testContext.getDocumentStore();
12+
});
13+
14+
afterEach(async () =>
15+
await disposeTestDocumentStore(store));
16+
17+
it("sets correct nested-object-types", async () => {
18+
class DateDoc {
19+
public firstDate: Date
20+
public secondDate: Date
21+
}
22+
23+
{
24+
const session = store.openSession();
25+
const dateDoc = new DateDoc();
26+
dateDoc.firstDate = new Date();
27+
28+
await session.store(dateDoc, "date/1");
29+
await session.saveChanges();
30+
}
31+
const session = store.openSession()
32+
const doc = await session.load<DateDoc>("date/1")
33+
doc.secondDate = new Date()
34+
await session.saveChanges()
35+
const metadata = session.advanced.getMetadataFor(doc)
36+
assert.strictEqual(doc.firstDate instanceof Date, true)
37+
assert.strictEqual(doc.secondDate instanceof Date, true)
38+
assert.strictEqual(metadata["@nested-object-types"]["firstDate"], "date")
39+
assert.strictEqual(metadata["@nested-object-types"]["secondDate"], "date")
40+
41+
const session2 = store.openSession()
42+
const loaded = await session2.load<DateDoc>("date/1")
43+
const loadedMetadata = session2.advanced.getMetadataFor(loaded)
44+
assert.strictEqual(loaded.firstDate instanceof Date, true)
45+
assert.strictEqual(loaded.secondDate instanceof Date, true)
46+
assert.strictEqual(loadedMetadata["@nested-object-types"]["firstDate"], "date")
47+
assert.strictEqual(loadedMetadata["@nested-object-types"]["secondDate"], "date")
48+
})
49+
});
50+

0 commit comments

Comments
 (0)