Skip to content

Commit

Permalink
Added handling for null class objects
Browse files Browse the repository at this point in the history
  • Loading branch information
WillForrestMindBridge committed Feb 26, 2025
1 parent bb637be commit c8573a8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions strategy/to_json/from_serializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function fromSerializable(): ToJSONStrategy {
if (Array.isArray(value)) {
return value.map((item) => item.tsSerialize());
}
else if (value === null) {
// Objects can be null, return them without further serialization.
return value;
}
return value.tsSerialize();
};
}
54 changes: 54 additions & 0 deletions strategy/to_json/from_serializable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ test({
},
});

test({
name: "fromSerializable - arrays of nested objects with nested object set to null",
fn() {
class Test1 extends Serializable {
@SerializeProperty("nested_property")
serializeMe = 999;
}
class Test2 extends Serializable {
@FromSerializable("outer_property")
nested: Test1[] = [new Test1()];
}

class Test3 extends Serializable {
@FromSerializable("outer_outer_property")
nested2: Test2[] = [new Test2()];
}
const testObj = new Test3();
testObj.nested = null;

assertEquals(
testObj.toJSON(),
`{"outer_outer_property":null}]}`,
);
},
});

test({
name: "fromSerializable - single serializable objects",
fn() {
Expand All @@ -69,3 +95,31 @@ test({
);
},
});


test({
name: "fromSerializable - single serializable objects with a nested object set to null",
fn() {
class Test1 extends Serializable {
@SerializeProperty("nested_property")
serializeMe = 999;
}
class Test2 extends Serializable {
@FromSerializable("outer_property")
nested: Test1 = new Test1();
}

class Test3 extends Serializable {
@FromSerializable("outer_outer_property")
nested2: Test2 = new Test2();
}

const testObj = new Test3();
testObj.nested2 = null;

assertEquals(
testObj.toJSON(),
`{"outer_outer_property":null}`,
);
},
});

0 comments on commit c8573a8

Please sign in to comment.