Skip to content

Commit 4b8d91e

Browse files
committed
Bugfix: continue instead of break on excess key in struct / object.
Fixes #27.
1 parent e02a24d commit 4b8d91e

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# 1.0
1+
# 1.0 (Future)
22

33
**Deserialization:**
44
- Added `string Serialize(object? input, PhpSerializiationOptions? options = null)` to `PhpSerialization` so the target type can be specified at run time.
55
- `PhpSerialization` (entry point of the library) is now null reference aware, aiding library consumers in caching `NullReferenceException`.
66
- `PhpSerialization` throws `ArgumentOutOfRangeException` instead of the more generalised `ArgumentException`
77
- Bugfix: "INF" and "-INF" would not be handled correctly when using explicit typing (`Deserialize<T>`) for some target types.
88
- Bugfix: Properly set classname when deserializing with explicit types that implement IPhpObject.
9+
- Bugfix: With the AllowExcessKeys, the deserialization of the given struct or object would abort when an excess key was encountered, leaving the properties after the excess key unassigned. See issue [#27](https://github.com/StringEpsilon/PhpSerializerNET/issues/27).
910
- Performance tweaks:
1011
- Minor improvements on memory use during deserialization.
11-
- Improved performance for deserializing Double and Integer values with explicit types.
12+
- Improved performance for deserializing Double and Integer values with explicit types.
1213

1314
**General:**
1415
* Bugfix: `PhpSerialization.ClearTypeCache()` was not exposed.

PhpSerializerNET.Test/Deserialize/Options/AllowExcessKeys.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,16 @@ public void Object_ThrowsWithOptionDisabled() {
5656
));
5757
Assert.AreEqual("Could not bind the key \"BString\" to object of type SimpleClass: No such property.", ex.Message);
5858
}
59+
60+
[TestMethod]
61+
public void Enabled_ProperlyAssignsAllKeys() {
62+
// Explicit test for issue #27.
63+
var result = PhpSerialization.Deserialize<SimpleClass>(
64+
"O:11:\"SimpleClass\":3:{s:1:\"_\";b:0;s:4:\"True\";b:1;s:5:\"False\";b:0;}",
65+
new PhpDeserializationOptions() { AllowExcessKeys = true }
66+
);
67+
Assert.AreEqual(true, result.True);
68+
Assert.AreEqual(false, result.False);
69+
}
5970
}
6071
}

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public object Deserialize(Type targetType) {
4040
}
4141

4242
public T Deserialize<T>() {
43-
return (T) this.Deserialize(typeof(T));
43+
return (T)this.Deserialize(typeof(T));
4444
}
4545

4646
/// <summary>
@@ -323,7 +323,7 @@ private object MakeStruct(Type targetType, PhpSerializeToken token) {
323323
$"Could not bind the key \"{token.Children[i].Value}\" to struct of type {targetType.Name}: No such field."
324324
);
325325
}
326-
break;
326+
continue;
327327
}
328328
if (fields[fieldName] != null) {
329329
var field = fields[fieldName];
@@ -363,7 +363,7 @@ private object MakeObject(Type targetType, PhpSerializeToken token) {
363363
$"Could not bind the key \"{token.Children[i].Value}\" to object of type {targetType.Name}: No such property."
364364
);
365365
}
366-
break;
366+
continue;
367367
}
368368
var property = properties[propertyName];
369369
if (property != null) { // null if PhpIgnore'd

0 commit comments

Comments
 (0)