Skip to content

Commit b2aeeae

Browse files
committed
Deserializer: Ignore redundant property/field name information.
1 parent 19a68cc commit b2aeeae

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-14
lines changed

PhpSerializerNET.Test/DataTypes/AStructWithRename.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ public struct AStructWithRename {
1111
[PhpProperty("foobar")]
1212
public string bar;
1313
}
14+
public struct RedundantStructName {
15+
[PhpProperty("foo")]
16+
public string foo;
17+
}
1418
}

PhpSerializerNET.Test/DataTypes/MappedClass.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ public class MappedClass {
2020

2121
public Guid Guid { get; set; }
2222
}
23+
24+
public class BadMappedClass {
25+
[PhpProperty("A")]
26+
public int A { get; set; }
27+
28+
[PhpProperty("B")]
29+
public int B { get; set; }
30+
}
2331
}

PhpSerializerNET.Test/Deserialize/ArrayDeserialization.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ public void ExplicitToClass_MappingInfo() {
130130
Assert.Null(deserializedObject.It);
131131
}
132132

133+
[Fact]
134+
public void RedundantMappingInfo() {
135+
var deserializedObject = PhpSerialization.Deserialize<BadMappedClass>(
136+
"""a:2:{s:1:"A";i:1;s:1:"B";i:2;}"""
137+
);
138+
Assert.Equal(1, deserializedObject.A);
139+
Assert.Equal(2, deserializedObject.B);
140+
}
141+
133142
[Fact]
134143
public void ExplicitToStruct() {
135144
var value = PhpSerialization.Deserialize<AStruct>(

PhpSerializerNET.Test/Deserialize/DeserializeStructs.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public void DeserializePropertyName() {
4646
Assert.Equal("Foo", value.foo);
4747
Assert.Equal("Bar", value.bar);
4848
}
49+
[Fact]
50+
public void DeserializeDuplicateFieldName() {
51+
var value = PhpSerialization.Deserialize<RedundantStructName>(
52+
"a:1:{s:3:\"foo\";s:3:\"Foo\";}"
53+
);
54+
Assert.Equal("Foo", value.foo);
55+
}
4956

5057
[Fact]
5158
public void DeserializeBoolToStruct() {

PhpSerializerNET.Test/Deserialize/Validation/TestOtherErrors.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@ public void ErrorOnEmptyInput() {
3939
);
4040

4141
const string expected = "PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty. (Parameter 'input')";
42+
const string expectedUtf8 = "PhpSerialization.DeserializeUtf8(): Parameter 'input' must not be empty. (Parameter 'input')";
4243
Assert.Equal(expected, ex.Message);
4344

4445
ex = Assert.Throws<ArgumentOutOfRangeException>(
4546
() => PhpSerialization.Deserialize<string>("")
4647
);
47-
4848
Assert.Equal(expected, ex.Message);
4949

5050
ex = Assert.Throws<ArgumentOutOfRangeException>(
5151
() => PhpSerialization.Deserialize("", typeof(string))
5252
);
53-
5453
Assert.Equal(expected, ex.Message);
54+
55+
ex = Assert.Throws<ArgumentOutOfRangeException>(
56+
() => PhpSerialization.DeserializeUtf8(""u8, typeof(string))
57+
);
58+
Assert.Equal(expectedUtf8, ex.Message);
59+
ex = Assert.Throws<ArgumentOutOfRangeException>(
60+
() => PhpSerialization.DeserializeUtf8(""u8)
61+
);
62+
Assert.Equal(expectedUtf8, ex.Message);
5563
}
5664

5765

PhpSerializerNET/Deserialization/PhpDeserializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ private object MakeObject(Type targetType, in PhpToken token) {
360360
result, this.DeserializeToken(property.PropertyType)
361361
);
362362
} catch (Exception exception) {
363-
var valueToken = this._tokens[this._currentToken - 1];
363+
var valueToken = this._tokens[this._currentToken-1];
364364
throw new DeserializationException(
365365
$"Exception encountered while trying to assign '{this.GetString(valueToken)}' to {targetType.Name}.{property.Name}. See inner exception for details.",
366366
exception
@@ -408,7 +408,7 @@ private object MakeList(Type targetType, in PhpToken token) {
408408
}
409409
index++;
410410
if (valueToken.Type == PhpDataType.Array || valueToken.Type == PhpDataType.Object) {
411-
itemPosition = valueToken.ValueEnd +1;
411+
itemPosition = valueToken.LastValuePosition +1;
412412
} else {
413413
itemPosition += 2;
414414
}
@@ -493,7 +493,7 @@ private object MakeCollection(in PhpToken token) {
493493
}
494494
index++;
495495
if (this._tokens[itemPosition+1].Type == PhpDataType.Array || this._tokens[itemPosition+1].Type == PhpDataType.Object) {
496-
itemPosition = this._tokens[itemPosition+1].ValueEnd +1;
496+
itemPosition = this._tokens[itemPosition+1].LastValuePosition +1;
497497
} else {
498498
itemPosition += 2;
499499
}

PhpSerializerNET/Extensions/ArrayExtensions.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ internal static Dictionary<object, PropertyInfo> GetAllProperties(this PropertyI
2626
phpPropertyAttribute = foundAttribute;
2727
}
2828
}
29+
var propertyName = options.CaseSensitiveProperties
30+
? property.Name
31+
: property.Name.ToLower();
2932
if (phpPropertyAttribute != null) {
3033
if (phpPropertyAttribute.IsInteger) {
3134
result.Add(phpPropertyAttribute.Key, isIgnored ? null : property);
3235
} else {
3336
var attributeName = options.CaseSensitiveProperties
3437
? phpPropertyAttribute.Name
3538
: phpPropertyAttribute.Name.ToLower();
36-
result.Add(attributeName, isIgnored ? null : property);
39+
if (attributeName != propertyName) {
40+
result.Add(attributeName, isIgnored ? null : property);
41+
}
3742
}
3843
}
39-
var propertyName = options.CaseSensitiveProperties
40-
? property.Name
41-
: property.Name.ToLower();
4244
result.Add(propertyName, isIgnored ? null : property);
4345
}
4446
return result;
@@ -59,16 +61,17 @@ internal static Dictionary<string, FieldInfo> GetAllFields(this FieldInfo[] fiel
5961
phpPropertyAttribute = foundAttribute;
6062
}
6163
}
64+
var fieldName = options.CaseSensitiveProperties
65+
? field.Name
66+
: field.Name.ToLower();
6267
if (phpPropertyAttribute != null) {
6368
var attributeName = options.CaseSensitiveProperties
6469
? phpPropertyAttribute.Name
6570
: phpPropertyAttribute.Name.ToLower();
66-
result.Add(attributeName, isIgnored ? null : field);
71+
if (attributeName != fieldName) {
72+
result.Add(attributeName, isIgnored ? null : field);
73+
}
6774
}
68-
69-
var fieldName = options.CaseSensitiveProperties
70-
? field.Name
71-
: field.Name.ToLower();
7275
result.Add(fieldName, isIgnored ? null : field);
7376
}
7477
return result;

0 commit comments

Comments
 (0)