Skip to content

Commit

Permalink
fix JSON.stringify for backed models
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Oct 16, 2023
1 parent 809a1a5 commit 66e0709
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/abstractions/src/store/backedModelProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function createBackedModelProxyHandler<T extends {}>(): ProxyHandler<T> {
console.warn(`BackingStore - Ignoring attempt to set 'backingStore' property`);
return true;
}
// set the value on the target object as well to allow target object to have keys needed for serialization
Reflect.set(target, prop, value, receiver);
console.debug(`BackingStore - Setting property '${prop.toString()}'`);
backingStore.set(prop.toString(), value);
return true;
Expand Down
38 changes: 38 additions & 0 deletions packages/serialization/json/test/common/JsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,42 @@ describe("JsonParseNode", () => {
assert.equal(backingStore.get("testString"), "test");
});

it("backing store shouldn't interfere with JSON.stringify", async () => {

const jsonObject = {
"foos": [
{
"id": "b089d1f1-e527-4b8a-ba96-094922af6e40",
"bars": [
{
"propA": "property A test value",
"propB": "property B test value"
}
]
}
]
};

const result = new JsonParseNode(jsonObject).getObjectValue(createTestBackedModelFromDiscriminatorValue) as TestBackedModel;
assert.equal(result.foos![0].bars![0].propA, "property A test value");
let jsonObjectStr = JSON.stringify(jsonObject);
let resultStr = JSON.stringify(result);
assert.equal(jsonObjectStr, resultStr);

// update the object then check stringify again
result.testString = "testStringValue";
jsonObjectStr = JSON.stringify(jsonObject);
resultStr = JSON.stringify(result);
assert.notEqual(jsonObjectStr, resultStr);

// update the backing store and check stringify again
const updateTestStrValue = "test string value";
const backingStore = result.backingStore;
backingStore.set("testString", updateTestStrValue);
const updatedJsonObject = {...jsonObject, testString: updateTestStrValue};
jsonObjectStr = JSON.stringify(updatedJsonObject);
resultStr = JSON.stringify(result);
assert.equal(jsonObjectStr, resultStr);
});

});

0 comments on commit 66e0709

Please sign in to comment.