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

add test case to ensure undefined fields are skipped while serializing. #1120

Merged
merged 4 commits into from
Apr 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { assert } from "chai";

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

describe("JsonParseNode", () => {
let backingStoreFactorySingleton: BackingStoreFactorySingleton;
const dummyBackingStore = {} as BackingStore;

beforeEach(() => {
backingStoreFactorySingleton = BackingStoreFactorySingleton.instance;
});

it("Test object serialization", async () => {
const testDate = new Date();

Expand Down Expand Up @@ -78,6 +87,70 @@ describe("JsonParseNode", () => {
const result = JSON.parse(contentAsStr);
assert.equal(result.testComplexString, "Błonie");
});
it("skip undefined objects from json body", async () => {
const inputObject: TestParser = {
testCollection: undefined,
testString: "test",
testObject: undefined,
};
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.isFalse("testCollection" in result);
assert.isTrue("testString" in result);
assert.isFalse("testObject" in result);
});
it("skip undefined objects from json body when Backing store enabled", async () => {
const jsonObject = {
"testCollection": ["2", "3"],
"foos": [
{
"id": "b089d1f1-e527-4b8a-ba96-094922af6e40",
"bars": [
{
"propA": "property A test value",
"propB": "property B test value"
}
]
}
]
};

const backedModel: TestBackedModel = new JsonParseNode(jsonObject).getObjectValue(createTestBackedModelFromDiscriminatorValue) as TestBackedModel;

const writer = new JsonSerializationWriter();
writer.writeObjectValue("", backedModel, serializeTestParser);
const serializedContent = writer.getSerializedContent();
const decoder = new TextDecoder();
const contentAsStr = decoder.decode(serializedContent);
const result = JSON.parse(contentAsStr);
assert.isTrue("testCollection" in result);
assert.isTrue("foos" in result);
assert.isFalse("testObject" in result);
assert.isFalse("testString" in result);

const handler = createBackedModelProxyHandler<TestBackedModel>();
const model = new Proxy<TestBackedModel>({backingStore: dummyBackingStore}, handler);
model.id = "abc";
model.testCollection = ["2", "3"];
model.testString = "test";

const modelWriter = new JsonSerializationWriter();
modelWriter.writeObjectValue("", model, serializeTestParser);
const serializedModelContent = modelWriter.getSerializedContent();
const modelContentAsString = decoder.decode(serializedModelContent);
const modelResult = JSON.parse(modelContentAsString);

assert.isTrue("id" in modelResult);
assert.isTrue("testCollection" in modelResult);
assert.isFalse("testObject" in modelResult);
assert.isTrue("testString" in modelResult);
// backingStore property shouldn't be part of the serialized content
assert.isFalse("backingStore:" in modelResult);
});
it("serializes untyped nodes as expected", async () => {
const inputObject: UntypedTestEntity = {
id: "1",
Expand Down
13 changes: 13 additions & 0 deletions packages/serialization/json/test/common/testEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export function serializeTestParser(
writer: SerializationWriter,
entity: TestParser | undefined = {}
): void {
writer.writeStringValue("id", entity.id);
writer.writeCollectionOfPrimitiveValues(
"testCollection",
entity.testCollection
Expand All @@ -151,9 +152,21 @@ export function serializeTestParser(

writer.writeDateValue("testDate", entity.testDate);
writer.writeObjectValue("testObject", entity.testObject, serializeTestObject);
writer.writeCollectionOfObjectValues("foos", entity.foos, serializeFoo);
writer.writeAdditionalData(entity.additionalData);
}

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

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

export function serializeTestBackModel(
writer: SerializationWriter,
entity: TestBackedModel | undefined = {},
Expand Down
Loading