Skip to content

Commit 632427c

Browse files
authored
Merge pull request #1639 from microsoft/fix/additional-data
fix/additional data
2 parents 749226e + ebe0270 commit 632427c

7 files changed

+88
-42
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/JsonParseNode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { assert, describe, it } from "vitest";
99
import { JsonParseNode } from "../../src/index";
1010
import { createTestParserFromDiscriminatorValue, type TestBackedModel, createTestBackedModelFromDiscriminatorValue, type TestParser, TestUnionObject, BarResponse } from "./testEntity";
11-
import { UntypedTestEntity, createUntypedTestEntityFromDiscriminatorValue } from "./untypedTestEntiy";
11+
import { UntypedTestEntity, createUntypedTestEntityFromDiscriminatorValue } from "./untypedTestEntity";
1212
import { UntypedNode, UntypedObject, isUntypedArray, isUntypedBoolean, isUntypedNode, isUntypedNumber, isUntypedObject } from "@microsoft/kiota-abstractions";
1313

1414
describe("JsonParseNode", () => {

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

+26-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { assert, describe, it, beforeEach } from "vitest";
99

1010
import { JsonParseNode, JsonSerializationWriter } from "../../src/index";
1111
import { createTestBackedModelFromDiscriminatorValue, createTestParserFromDiscriminatorValue, LongRunningOperationStatusObject, serializeTestParser, TestBackedModel, type TestParser } from "./testEntity";
12-
import { UntypedTestEntity, serializeUntypedTestEntity } from "./untypedTestEntiy";
12+
import { UntypedTestEntity, serializeUntypedTestEntity } from "./untypedTestEntity";
1313
import { BackingStore, BackingStoreFactorySingleton, createBackedModelProxyHandler, createUntypedArray, createUntypedBoolean, createUntypedNull, createUntypedNumber, createUntypedObject, createUntypedString } from "@microsoft/kiota-abstractions";
1414

1515
describe("JsonParseNode", () => {
@@ -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,
72+
});
6073
});
6174

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

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

+53-24
Original file line numberDiff line numberDiff line change
@@ -69,50 +69,61 @@ export function createBarParserFromDiscriminatorValue(parseNode: ParseNode | und
6969
return deserializeBarParser;
7070
}
7171

72+
export function createTestObjectFromDiscriminatorValue(parseNode: ParseNode | undefined) {
73+
if (!parseNode) throw new Error("parseNode cannot be undefined");
74+
return deserializeTestObject;
75+
}
76+
export function deserializeTestObject(testObject: { additionalData?: Record<string, unknown> } | undefined = {}): Record<string, (node: ParseNode) => void> {
77+
return {};
78+
}
79+
7280
export function deserializeTestParser(testParser: TestParser | undefined = {}): Record<string, (node: ParseNode) => void> {
7381
return {
74-
testCollection: (n) => {
82+
"testObject": (n) => {
83+
testParser.testObject = n.getObjectValue<{ additionalData?: Record<string, unknown> }>(createTestObjectFromDiscriminatorValue);
84+
},
85+
"testCollection": (n) => {
7586
testParser.testCollection = n.getCollectionOfPrimitiveValues();
7687
},
77-
testString: (n) => {
88+
"testString": (n) => {
7889
testParser.testString = n.getStringValue();
7990
},
80-
testBoolean: (n) => {
91+
"testBoolean": (n) => {
8192
testParser.testBoolean = n.getBooleanValue();
8293
},
83-
textComplexString: (n) => {
94+
"testComplexString": (n) => {
8495
testParser.testComplexString = n.getStringValue();
8596
},
86-
testDate: (n) => {
97+
"testDate": (n) => {
8798
testParser.testDate = n.getDateValue();
8899
},
89-
foos: (n) => {
100+
"foos": (n) => {
90101
testParser.foos = n.getCollectionOfObjectValues(createFooParserFromDiscriminatorValue);
91102
},
92-
id: (n) => {
103+
"id": (n) => {
93104
testParser.id = n.getStringValue();
94105
},
95-
testNumber: (n) => {
106+
"testNumber": (n) => {
96107
testParser.testNumber = n.getNumberValue();
97108
},
98-
testGuid: (n) => {
109+
"testGuid": (n) => {
99110
testParser.testGuid = n.getGuidValue();
100111
},
101-
testUnionObject: (n) => {
112+
"testUnionObject": (n) => {
102113
testParser.testUnionObject = n.getStringValue() ?? n.getNumberValue() ?? n.getObjectValue(createTestUnionObjectFromDiscriminatorValue);
103114
},
104-
status: (n) => {
115+
"status": (n) => {
105116
testParser.status = n.getEnumValue<LongRunningOperationStatus>(LongRunningOperationStatusObject);
106117
},
107-
nextStatuses: (n) => {
118+
"nextStatuses": (n) => {
108119
testParser.nextStatuses = n.getCollectionOfEnumValues<LongRunningOperationStatus>(LongRunningOperationStatusObject);
109120
},
110121
};
111122
}
112123

113124
export function deserializeTestBackedModel(testParser: TestBackedModel | undefined = {}): Record<string, (node: ParseNode) => void> {
114125
return {
115-
backingStoreEnabled: (n) => {
126+
"backingStoreEnabled": (n) => {
116127
testParser.backingStoreEnabled = true;
117128
},
118129
...deserializeTestParser(testParser),
@@ -121,33 +132,39 @@ export function deserializeTestBackedModel(testParser: TestBackedModel | undefin
121132

122133
export function deserializeFooParser(fooResponse: FooResponse | undefined = {}): Record<string, (node: ParseNode) => void> {
123134
return {
124-
id: (n) => {
135+
"id": (n) => {
125136
fooResponse.id = n.getStringValue();
126137
},
127-
bars: (n) => {
138+
"bars": (n) => {
128139
fooResponse.bars = n.getCollectionOfObjectValues(createBarParserFromDiscriminatorValue);
129140
},
130141
};
131142
}
132143

133144
export function deserializeBarParser(barResponse: BarResponse | undefined = {}): Record<string, (node: ParseNode) => void> {
134145
return {
135-
propA: (n) => {
146+
"propA": (n) => {
136147
barResponse.propA = n.getStringValue();
137148
},
138-
propB: (n) => {
149+
"propB": (n) => {
139150
barResponse.propB = n.getStringValue();
140151
},
141-
propC: (n) => {
152+
"propC": (n) => {
142153
barResponse.propC = n.getDateValue();
143154
},
144155
};
145156
}
146157

147-
export function serializeTestObject(writer: SerializationWriter, entity: { additionalData?: Record<string, unknown> } | undefined = {}): void {
158+
export function serializeTestObject(writer: SerializationWriter, entity: { additionalData?: Record<string, unknown> } | undefined | null = {}): void {
159+
if (!entity) {
160+
return;
161+
}
148162
writer.writeAdditionalData(entity.additionalData);
149163
}
150-
export function serializeTestParser(writer: SerializationWriter, entity: TestParser | undefined = {}): void {
164+
export function serializeTestParser(writer: SerializationWriter, entity: TestParser | undefined | null = {}): void {
165+
if (!entity) {
166+
return;
167+
}
151168
writer.writeStringValue("id", entity.id);
152169
writer.writeCollectionOfPrimitiveValues("testCollection", entity.testCollection);
153170
writer.writeStringValue("testString", entity.testString);
@@ -170,18 +187,27 @@ export function serializeTestParser(writer: SerializationWriter, entity: TestPar
170187
writer.writeCollectionOfEnumValues("nextStatuses", entity.nextStatuses);
171188
}
172189

173-
export function serializeFoo(writer: SerializationWriter, entity: FooResponse | undefined = {}): void {
190+
export function serializeFoo(writer: SerializationWriter, entity: FooResponse | undefined | null = {}): void {
191+
if (!entity) {
192+
return;
193+
}
174194
writer.writeStringValue("id", entity.id);
175195
writer.writeCollectionOfObjectValues("bars", entity.bars, serializeBar);
176196
}
177197

178-
export function serializeBar(writer: SerializationWriter, entity: BarResponse | undefined = {}): void {
198+
export function serializeBar(writer: SerializationWriter, entity: BarResponse | undefined | null = {}): void {
199+
if (!entity) {
200+
return;
201+
}
179202
writer.writeStringValue("propA", entity.propA);
180203
writer.writeStringValue("propB", entity.propB);
181204
writer.writeDateValue("propC", entity.propC);
182205
}
183206

184-
export function serializeTestBackModel(writer: SerializationWriter, entity: TestBackedModel | undefined = {}): void {
207+
export function serializeTestBackModel(writer: SerializationWriter, entity: TestBackedModel | undefined | null = {}): void {
208+
if (!entity) {
209+
return;
210+
}
185211
serializeTestParser(writer, entity);
186212
}
187213

@@ -198,7 +224,10 @@ export function deserializeIntoTestUnionObject(fooBar: Partial<TestUnionObject>
198224
};
199225
}
200226

201-
export function serializeTestUnionObject(writer: SerializationWriter, fooBar: Partial<TestUnionObject> | undefined = {}): void {
227+
export function serializeTestUnionObject(writer: SerializationWriter, fooBar: Partial<TestUnionObject> | undefined | null = {}): void {
228+
if (!fooBar) {
229+
return;
230+
}
202231
serializeFoo(writer, fooBar as FooResponse);
203232
serializeBar(writer, fooBar as BarResponse);
204233
}

packages/serialization/json/vitest.config.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig, configDefaults } from "vitest/config";
22

33
export default defineConfig({
44
test: {
5-
exclude: [...configDefaults.exclude, "**/*/{testEntity,index,untypedTestEntiy,unionOfObjectsAndPrimitives,testUtils}.ts"],
5+
exclude: [...configDefaults.exclude, "**/*/{testEntity,index,untypedTestEntity,unionOfObjectsAndPrimitives,testUtils}.ts"],
66
include: [...configDefaults.include, "test/**/*.ts"],
77
coverage: {
88
reporter: ["html", "cobertura"],

prettier.config.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010
"singleQuote": false,
1111
"tabWidth": 4,
1212
"trailingComma": "all",
13-
"useTabs": true
13+
"useTabs": true,
14+
"quoteProps": "preserve"
1415
}

0 commit comments

Comments
 (0)