Skip to content

Commit 44d8088

Browse files
committed
Test AllowExcessKeys and tweak error messages.
1 parent e632c06 commit 44d8088

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Support for structs.
44
- Fixed performance drop due to over-checking the input string.
55
- Refactored deserializer to work in less steps and with cleaner code.
6+
- Slight tweaks of some error messages.
67

78
# 0.3.0
89

PhpSerializerNET.Test/DeserializeObjects.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ public void DeserializeObjectCaseInsenstiveProps() {
5353
Assert.AreEqual("Hallo Welt!", deserializedObject.German);
5454
}
5555

56+
[TestMethod]
57+
public void DeserializeObjectWithExcessKeys() {
58+
var deserializedObject = PhpSerializer.Deserialize<MappedClass>(
59+
"a:3:{s:2:\"en\";s:12:\"Hello World!\";s:2:\"de\";s:11:\"Hallo Welt!\";s:2:\"es\";s:11:\"Hola Mundo!\";}",
60+
new PhpDeserializationOptions() { AllowExcessKeys = true }
61+
);
62+
Assert.IsNotNull(deserializedObject);
63+
}
64+
65+
[TestMethod]
66+
public void ThrowsOnExcessKeys() {
67+
var ex = Assert.ThrowsException<DeserializationException>(() => PhpSerializer.Deserialize<MappedClass>(
68+
"a:3:{s:2:\"en\";s:12:\"Hello World!\";s:2:\"de\";s:11:\"Hallo Welt!\";s:2:\"es\";s:11:\"Hola Mundo!\";}",
69+
new PhpDeserializationOptions() { AllowExcessKeys = false }
70+
));
71+
Assert.AreEqual("Could not bind the key \"es\" to object of type MappedClass: No such property.", ex.Message);
72+
}
73+
5674

5775
[TestMethod]
5876
public void DeserializeList() {

PhpSerializerNET.Test/TestStructs.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ public void DeserializeArrayToStruct() {
5656
);
5757
}
5858

59+
[TestMethod]
60+
public void DeserializeStructWithExcessKeys() {
61+
var value = PhpSerializer.Deserialize<MyStruct>(
62+
"a:3:{s:3:\"foo\";s:3:\"Foo\";s:3:\"bar\";s:3:\"Bar\";s:6:\"foobar\";s:6:\"FooBar\";}",
63+
new PhpDeserializationOptions() { AllowExcessKeys = true }
64+
);
65+
66+
Assert.AreEqual(
67+
"Foo",
68+
value.foo
69+
);
70+
Assert.AreEqual(
71+
"Bar",
72+
value.bar
73+
);
74+
}
75+
76+
[TestMethod]
77+
public void ThrowsOnStructWithExcessKeys() {
78+
var ex = Assert.ThrowsException<DeserializationException>(() => PhpSerializer.Deserialize<MyStruct>(
79+
"a:3:{s:3:\"foo\";s:3:\"Foo\";s:3:\"bar\";s:3:\"Bar\";s:6:\"foobar\";s:6:\"FooBar\";}",
80+
new PhpDeserializationOptions() { AllowExcessKeys = false }
81+
));
82+
Assert.AreEqual("Could not bind the key \"foobar\" to struct of type MyStruct: No such field.", ex.Message);
83+
}
84+
5985
[TestMethod]
6086
public void DeserializeStructCaseInsensitive() {
6187
var value = PhpSerializer.Deserialize<MyStruct>(

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private object MakeStruct(Type targetType, PhpSerializeToken token) {
111111
if (field == null) {
112112
if (!_options.AllowExcessKeys) {
113113
throw new DeserializationException(
114-
$"Error: Could not bind the key {fieldName} to struct of type {targetType.Name}: No such property."
114+
$"Could not bind the key \"{fieldName}\" to struct of type {targetType.Name}: No such field."
115115
);
116116
}
117117
break;
@@ -138,7 +138,7 @@ private object MakeObject(Type targetType, PhpSerializeToken token) {
138138
if (property == null) {
139139
if (!_options.AllowExcessKeys) {
140140
throw new DeserializationException(
141-
$"Error: Could not bind the key {propertyName} to object of type {targetType.Name}: No such property."
141+
$"Could not bind the key \"{propertyName}\" to object of type {targetType.Name}: No such property."
142142
);
143143
}
144144
break;

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ string serializedData = PhpSerializer.Serialize(myObject);
2323
## TODOs
2424

2525
* [ ] Documentation.
26-
* [ ] Write better exceptions.
27-
- Partially done.
2826
* [ ] General polish.
29-
* [ ] Cover all features and most error handlinng with unit tests.
30-
- AllowExcessKeys needs testing (both ways) (and on structs too.)
3127
* [ ] Reorganize unit tests.
3228

3329
## Non-TODO:

0 commit comments

Comments
 (0)