Skip to content

Commit 145fc50

Browse files
committed
fix: a bug where additional data would not go to the dedicated property
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 20004e7 commit 145fc50

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

packages/serialization/json/src/jsonParseNode.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { DateOnly, Duration, TimeOnly, UntypedNode, createBackedModelProxyHandler, createUntypedArray, createUntypedBoolean, createUntypedNodeFromDiscriminatorValue, createUntypedNull, createUntypedNumber, createUntypedObject, createUntypedString, inNodeEnv, isBackingStoreEnabled, isUntypedNode, parseGuidString, getEnumValueFromStringValue, type Parsable, type ParsableFactory, type ParseNode } from "@microsoft/kiota-abstractions";
8+
import { DateOnly, Duration, TimeOnly, UntypedNode, createBackedModelProxyHandler, createUntypedArray, createUntypedBoolean, createUntypedNodeFromDiscriminatorValue, createUntypedNull, createUntypedNumber, createUntypedObject, createUntypedString, inNodeEnv, isBackingStoreEnabled, isUntypedNode, parseGuidString, getEnumValueFromStringValue, type Parsable, type ParsableFactory, type ParseNode, AdditionalDataHolder } from "@microsoft/kiota-abstractions";
99
export class JsonParseNode implements ParseNode {
1010
constructor(private readonly _jsonNode: unknown) {}
1111
public onBeforeAssignFieldValues: ((value: Parsable) => void) | undefined;
@@ -108,8 +108,11 @@ export class JsonParseNode implements ParseNode {
108108
if (deserializer) {
109109
deserializer(new JsonParseNode(v));
110110
} else {
111+
// there is no real way to test if the model is actually a holder or not
111112
// additional properties
112-
(model as Record<string, unknown>)[k] = v;
113+
const modelDataHolder = model as AdditionalDataHolder;
114+
modelDataHolder.additionalData ??= {} as Record<string, unknown>;
115+
modelDataHolder.additionalData[k] = v;
113116
}
114117
});
115118
};

packages/serialization/json/test/common/jsonSerializationWriter.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ describe("JsonParseNode", () => {
3636
},
3737
testDate,
3838
};
39-
const expectedObject: TestParser = {
39+
40+
const writer = new JsonSerializationWriter();
41+
writer.writeObjectValue("", inputObject, serializeTestParser);
42+
const serializedContent = writer.getSerializedContent();
43+
const decoder = new TextDecoder();
44+
const contentAsStr = decoder.decode(serializedContent);
45+
const result = JSON.parse(contentAsStr);
46+
assert.deepEqual(result, {
4047
testCollection: ["2", "3"],
4148
testString: "test",
4249
testComplexString: "A more \"complex\" string with \r\nlinebreaks and 'weird' characters",
@@ -46,17 +53,23 @@ describe("JsonParseNode", () => {
4653
someValue: 123,
4754
},
4855
},
49-
testDate,
50-
};
51-
52-
const writer = new JsonSerializationWriter();
53-
writer.writeObjectValue("", inputObject, serializeTestParser);
54-
const serializedContent = writer.getSerializedContent();
55-
const decoder = new TextDecoder();
56-
const contentAsStr = decoder.decode(serializedContent);
57-
const result = JSON.parse(contentAsStr);
58-
const stringValueResult = new JsonParseNode(result).getObjectValue(createTestParserFromDiscriminatorValue) as TestParser;
59-
assert.deepEqual(stringValueResult, expectedObject);
56+
testDate: testDate.toISOString(),
57+
});
58+
const parsedValueResult = new JsonParseNode(result).getObjectValue(createTestParserFromDiscriminatorValue);
59+
assert.deepEqual(parsedValueResult as object, {
60+
testCollection: ["2", "3"],
61+
testString: "test",
62+
testComplexString: "A more \"complex\" string with \r\nlinebreaks and 'weird' characters",
63+
testObject: {
64+
additionalData: {
65+
testObjectName: "str",
66+
testObjectProp: {
67+
someValue: 123,
68+
},
69+
},
70+
},
71+
testDate: testDate.toISOString(),
72+
});
6073
});
6174

6275
it("Test enum serialization", async () => {

0 commit comments

Comments
 (0)