Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/additional data #1639

Merged
merged 7 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/serialization/json/src/jsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* -------------------------------------------------------------------------------------------
*/

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";
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";
export class JsonParseNode implements ParseNode {
constructor(private readonly _jsonNode: unknown) {}
public onBeforeAssignFieldValues: ((value: Parsable) => void) | undefined;
Expand Down Expand Up @@ -108,8 +108,11 @@ export class JsonParseNode implements ParseNode {
if (deserializer) {
deserializer(new JsonParseNode(v));
} else {
// there is no real way to test if the model is actually a holder or not
// additional properties
(model as Record<string, unknown>)[k] = v;
const modelDataHolder = model as AdditionalDataHolder;
modelDataHolder.additionalData ??= {} as Record<string, unknown>;
modelDataHolder.additionalData[k] = v;
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/json/test/common/JsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { assert, describe, it } from "vitest";
import { JsonParseNode } from "../../src/index";
import { createTestParserFromDiscriminatorValue, type TestBackedModel, createTestBackedModelFromDiscriminatorValue, type TestParser, TestUnionObject, BarResponse } from "./testEntity";
import { UntypedTestEntity, createUntypedTestEntityFromDiscriminatorValue } from "./untypedTestEntiy";
import { UntypedTestEntity, createUntypedTestEntityFromDiscriminatorValue } from "./untypedTestEntity";
import { UntypedNode, UntypedObject, isUntypedArray, isUntypedBoolean, isUntypedNode, isUntypedNumber, isUntypedObject } from "@microsoft/kiota-abstractions";

describe("JsonParseNode", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assert, describe, it, beforeEach } from "vitest";

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

describe("JsonParseNode", () => {
Expand All @@ -36,7 +36,14 @@ describe("JsonParseNode", () => {
},
testDate,
};
const expectedObject: TestParser = {

const writer = new JsonSerializationWriter();
writer.writeObjectValue("", inputObject, serializeTestParser);
const serializedContent = writer.getSerializedContent();
const decoder = new TextDecoder();
const contentAsStr = decoder.decode(serializedContent);
const result = JSON.parse(contentAsStr);
assert.deepEqual(result, {
testCollection: ["2", "3"],
testString: "test",
testComplexString: "A more \"complex\" string with \r\nlinebreaks and 'weird' characters",
Expand All @@ -46,17 +53,23 @@ describe("JsonParseNode", () => {
someValue: 123,
},
},
testDate,
};

const writer = new JsonSerializationWriter();
writer.writeObjectValue("", inputObject, serializeTestParser);
const serializedContent = writer.getSerializedContent();
const decoder = new TextDecoder();
const contentAsStr = decoder.decode(serializedContent);
const result = JSON.parse(contentAsStr);
const stringValueResult = new JsonParseNode(result).getObjectValue(createTestParserFromDiscriminatorValue) as TestParser;
assert.deepEqual(stringValueResult, expectedObject);
testDate: testDate.toISOString(),
});
const parsedValueResult = new JsonParseNode(result).getObjectValue(createTestParserFromDiscriminatorValue);
assert.deepEqual(parsedValueResult as object, {
testCollection: ["2", "3"],
testString: "test",
testComplexString: "A more \"complex\" string with \r\nlinebreaks and 'weird' characters",
testObject: {
additionalData: {
testObjectName: "str",
testObjectProp: {
someValue: 123,
},
},
},
testDate: testDate,
});
});

it("Test enum serialization", async () => {
Expand Down
77 changes: 53 additions & 24 deletions packages/serialization/json/test/common/testEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,61 @@ export function createBarParserFromDiscriminatorValue(parseNode: ParseNode | und
return deserializeBarParser;
}

export function createTestObjectFromDiscriminatorValue(parseNode: ParseNode | undefined) {
if (!parseNode) throw new Error("parseNode cannot be undefined");
return deserializeTestObject;
}
export function deserializeTestObject(testObject: { additionalData?: Record<string, unknown> } | undefined = {}): Record<string, (node: ParseNode) => void> {
return {};
}

export function deserializeTestParser(testParser: TestParser | undefined = {}): Record<string, (node: ParseNode) => void> {
return {
testCollection: (n) => {
"testObject": (n) => {
testParser.testObject = n.getObjectValue<{ additionalData?: Record<string, unknown> }>(createTestObjectFromDiscriminatorValue);
},
"testCollection": (n) => {
testParser.testCollection = n.getCollectionOfPrimitiveValues();
},
testString: (n) => {
"testString": (n) => {
testParser.testString = n.getStringValue();
},
testBoolean: (n) => {
"testBoolean": (n) => {
testParser.testBoolean = n.getBooleanValue();
},
textComplexString: (n) => {
"testComplexString": (n) => {
testParser.testComplexString = n.getStringValue();
},
testDate: (n) => {
"testDate": (n) => {
testParser.testDate = n.getDateValue();
},
foos: (n) => {
"foos": (n) => {
testParser.foos = n.getCollectionOfObjectValues(createFooParserFromDiscriminatorValue);
},
id: (n) => {
"id": (n) => {
testParser.id = n.getStringValue();
},
testNumber: (n) => {
"testNumber": (n) => {
testParser.testNumber = n.getNumberValue();
},
testGuid: (n) => {
"testGuid": (n) => {
testParser.testGuid = n.getGuidValue();
},
testUnionObject: (n) => {
"testUnionObject": (n) => {
testParser.testUnionObject = n.getStringValue() ?? n.getNumberValue() ?? n.getObjectValue(createTestUnionObjectFromDiscriminatorValue);
},
status: (n) => {
"status": (n) => {
testParser.status = n.getEnumValue<LongRunningOperationStatus>(LongRunningOperationStatusObject);
},
nextStatuses: (n) => {
"nextStatuses": (n) => {
testParser.nextStatuses = n.getCollectionOfEnumValues<LongRunningOperationStatus>(LongRunningOperationStatusObject);
},
};
}

export function deserializeTestBackedModel(testParser: TestBackedModel | undefined = {}): Record<string, (node: ParseNode) => void> {
return {
backingStoreEnabled: (n) => {
"backingStoreEnabled": (n) => {
testParser.backingStoreEnabled = true;
},
...deserializeTestParser(testParser),
Expand All @@ -121,33 +132,39 @@ export function deserializeTestBackedModel(testParser: TestBackedModel | undefin

export function deserializeFooParser(fooResponse: FooResponse | undefined = {}): Record<string, (node: ParseNode) => void> {
return {
id: (n) => {
"id": (n) => {
fooResponse.id = n.getStringValue();
},
bars: (n) => {
"bars": (n) => {
fooResponse.bars = n.getCollectionOfObjectValues(createBarParserFromDiscriminatorValue);
},
};
}

export function deserializeBarParser(barResponse: BarResponse | undefined = {}): Record<string, (node: ParseNode) => void> {
return {
propA: (n) => {
"propA": (n) => {
barResponse.propA = n.getStringValue();
},
propB: (n) => {
"propB": (n) => {
barResponse.propB = n.getStringValue();
},
propC: (n) => {
"propC": (n) => {
barResponse.propC = n.getDateValue();
},
};
}

export function serializeTestObject(writer: SerializationWriter, entity: { additionalData?: Record<string, unknown> } | undefined = {}): void {
export function serializeTestObject(writer: SerializationWriter, entity: { additionalData?: Record<string, unknown> } | undefined | null = {}): void {
if (!entity) {
return;
}
writer.writeAdditionalData(entity.additionalData);
}
export function serializeTestParser(writer: SerializationWriter, entity: TestParser | undefined = {}): void {
export function serializeTestParser(writer: SerializationWriter, entity: TestParser | undefined | null = {}): void {
if (!entity) {
return;
}
writer.writeStringValue("id", entity.id);
writer.writeCollectionOfPrimitiveValues("testCollection", entity.testCollection);
writer.writeStringValue("testString", entity.testString);
Expand All @@ -170,18 +187,27 @@ export function serializeTestParser(writer: SerializationWriter, entity: TestPar
writer.writeCollectionOfEnumValues("nextStatuses", entity.nextStatuses);
}

export function serializeFoo(writer: SerializationWriter, entity: FooResponse | undefined = {}): void {
export function serializeFoo(writer: SerializationWriter, entity: FooResponse | undefined | null = {}): void {
if (!entity) {
return;
}
writer.writeStringValue("id", entity.id);
writer.writeCollectionOfObjectValues("bars", entity.bars, serializeBar);
}

export function serializeBar(writer: SerializationWriter, entity: BarResponse | undefined = {}): void {
export function serializeBar(writer: SerializationWriter, entity: BarResponse | undefined | null = {}): void {
if (!entity) {
return;
}
writer.writeStringValue("propA", entity.propA);
writer.writeStringValue("propB", entity.propB);
writer.writeDateValue("propC", entity.propC);
}

export function serializeTestBackModel(writer: SerializationWriter, entity: TestBackedModel | undefined = {}): void {
export function serializeTestBackModel(writer: SerializationWriter, entity: TestBackedModel | undefined | null = {}): void {
if (!entity) {
return;
}
serializeTestParser(writer, entity);
}

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

export function serializeTestUnionObject(writer: SerializationWriter, fooBar: Partial<TestUnionObject> | undefined = {}): void {
export function serializeTestUnionObject(writer: SerializationWriter, fooBar: Partial<TestUnionObject> | undefined | null = {}): void {
if (!fooBar) {
return;
}
serializeFoo(writer, fooBar as FooResponse);
serializeBar(writer, fooBar as BarResponse);
}
2 changes: 1 addition & 1 deletion packages/serialization/json/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig, configDefaults } from "vitest/config";

export default defineConfig({
test: {
exclude: [...configDefaults.exclude, "**/*/{testEntity,index,untypedTestEntiy,unionOfObjectsAndPrimitives,testUtils}.ts"],
exclude: [...configDefaults.exclude, "**/*/{testEntity,index,untypedTestEntity,unionOfObjectsAndPrimitives,testUtils}.ts"],
include: [...configDefaults.include, "test/**/*.ts"],
coverage: {
reporter: ["html", "cobertura"],
Expand Down
3 changes: 2 additions & 1 deletion prettier.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {
"singleQuote": false,
"tabWidth": 4,
"trailingComma": "all",
"useTabs": true
"useTabs": true,
"quoteProps": "preserve"
}
Loading