Skip to content

Commit 908ed92

Browse files
committed
Test ThrowOnCircularReferences option.
1 parent 7c03f2e commit 908ed92

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Other changes:
88
- Added `[PhpClass()]` attribute.
99
- Added `StdClass` and `EnableTypeLookup` to deserialization options
1010
- Added options for `PhpSerialization.Serialize()`.
11-
- `ThrowOnCircularReferences` - whether or not to throw on ciruclar references, defaults to false (this might change in the future.)
11+
- `ThrowOnCircularReferences` - whether or not to throw on circular references, defaults to false (this might change in the future.)
1212
- Updated and adjusted some of the XML documentation.
1313

1414
# 0.4.0

PhpSerializerNET.Test/TestCircularReferences.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
**/
66

7+
using System;
78
using System.Collections.Generic;
89
using Microsoft.VisualStudio.TestTools.UnitTesting;
910

@@ -31,6 +32,25 @@ public void SerializeCircularObject() {
3132
);
3233
}
3334

35+
[TestMethod]
36+
public void ThrowOnCircularReferencesOption() {
37+
var testObject = new CircularClass() {
38+
Foo = "First"
39+
};
40+
testObject.Bar = new CircularClass() {
41+
Foo = "Second",
42+
Bar = testObject
43+
};
44+
45+
var ex = Assert.ThrowsException<ArgumentException>(
46+
() => PhpSerialization.Serialize(testObject, new PhpSerializiationOptions(){ ThrowOnCircularReferences = true})
47+
);
48+
Assert.AreEqual(
49+
"Input object has a circular reference.",
50+
ex.Message
51+
);
52+
}
53+
3454
[TestMethod]
3555
public void SerializeCircularList() {
3656
List<object> listA = new() { "A", "B" };

PhpSerializerNET/PhpSerialization.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public static T Deserialize<T>(
6969
/// Arrays, lists and dictionaries are serialized into arrays.
7070
/// Objects may also be serialized into arrays, if their respective struct or class does not have the <see cref="PhpClass"/> attribute.
7171
/// </returns>
72-
public static string Serialize(object input) {
73-
return new PhpSerializer().Serialize(input);
72+
public static string Serialize(object input, PhpSerializiationOptions options = null) {
73+
return new PhpSerializer(options).Serialize(input);
7474
}
7575
}
7676
}

0 commit comments

Comments
 (0)