Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b8dabb9

Browse files
chriswebbmythz
authored andcommitted
Fixed NRE caused by inherited non-public data members. (#506)
1 parent 4f3c14d commit b8dabb9

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ internal static ParseStringSegmentDelegate GetPropertyMethod(ITypeSerializer ser
285285
private static SetMemberDelegate GetSetPropertyMethod(TypeConfig typeConfig, PropertyInfo propertyInfo)
286286
{
287287
if (typeConfig.Type != propertyInfo.DeclaringType)
288-
propertyInfo = propertyInfo.DeclaringType.GetProperty(propertyInfo.Name);
288+
propertyInfo = propertyInfo.DeclaringType.GetProperty(propertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
289289

290290
if (!propertyInfo.CanWrite && !typeConfig.EnableAnonymousFieldSetters) return null;
291291

@@ -326,7 +326,7 @@ public static TypeAccessor Create(ITypeSerializer serializer, TypeConfig typeCon
326326
private static SetMemberDelegate GetSetFieldMethod(TypeConfig typeConfig, FieldInfo fieldInfo)
327327
{
328328
if (typeConfig.Type != fieldInfo.DeclaringType)
329-
fieldInfo = fieldInfo.DeclaringType.GetField(fieldInfo.Name);
329+
fieldInfo = fieldInfo.DeclaringType.GetField(fieldInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
330330

331331
return PclExport.Instance.CreateSetter(fieldInfo);
332332
}

tests/ServiceStack.Text.Tests/ReportedIssues.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,68 @@ public void Deserialize_Correctly_When_Last_Item_Is_Null_in_Int_array_prop()
330330
var deserialized = TypeSerializer.DeserializeFromString<TestMappedList>(serialized);
331331
Assert.That(deserialized.IntArrayProp, Is.EqualTo(type.IntArrayProp));
332332
}
333+
334+
335+
[Test]
336+
public void Null_Reference_Exception_On_Inherited_Field_With_No_Setter()
337+
{
338+
string testString = null;
339+
InheritedFieldErrorTest parentClass = new InheritedFieldErrorTest(), parentClassResult = null;
340+
InheritedFieldErrorTestChild childClass = new InheritedFieldErrorTestChild(), childClassResult = null;
341+
Assert.DoesNotThrow(() => { testString = JsonSerializer.SerializeToString(parentClass); });
342+
Assert.IsNotNull(testString);
343+
Assert.IsNotEmpty(testString);
344+
Assert.DoesNotThrow(() => { parentClassResult = JsonSerializer.DeserializeFromString<InheritedFieldErrorTest>(testString); });
345+
Assert.IsNotNull(parentClassResult);
346+
Assert.DoesNotThrow(() => { testString = JsonSerializer.SerializeToString(childClass); });
347+
Assert.IsNotNull(testString);
348+
Assert.IsNotEmpty(testString);
349+
Assert.DoesNotThrow(() => { childClassResult = JsonSerializer.DeserializeFromString<InheritedFieldErrorTestChild>(testString); });
350+
Assert.IsNotNull(childClassResult);
351+
}
352+
353+
[System.Runtime.Serialization.DataContract]
354+
public class InheritedFieldErrorTest
355+
{
356+
[System.Runtime.Serialization.DataMember]
357+
protected bool test = false;
358+
}
359+
360+
[System.Runtime.Serialization.DataContract]
361+
public class InheritedFieldErrorTestChild : InheritedFieldErrorTest
362+
{
363+
}
364+
365+
[Test]
366+
public void Null_Reference_Exception_On_Inherited_Property_With_No_Setter()
367+
{
368+
string testString = null;
369+
InheritedPropertyErrorTest parentClass = new InheritedPropertyErrorTest(), parentClassResult = null;
370+
InheritedPropertyErrorTestChild childClass = new InheritedPropertyErrorTestChild(), childClassResult = null;
371+
Assert.DoesNotThrow(() => { testString = JsonSerializer.SerializeToString(parentClass); });
372+
Assert.IsNotNull(testString);
373+
Assert.IsNotEmpty(testString);
374+
Assert.DoesNotThrow(() => { parentClassResult = JsonSerializer.DeserializeFromString<InheritedPropertyErrorTest>(testString); });
375+
Assert.IsNotNull(parentClassResult);
376+
Assert.DoesNotThrow(() => { testString = JsonSerializer.SerializeToString(childClass); });
377+
Assert.IsNotNull(testString);
378+
Assert.IsNotEmpty(testString);
379+
Assert.DoesNotThrow(() => { childClassResult = JsonSerializer.DeserializeFromString<InheritedPropertyErrorTestChild>(testString); });
380+
Assert.IsNotNull(childClassResult);
381+
}
382+
383+
384+
[System.Runtime.Serialization.DataContract]
385+
public class InheritedPropertyErrorTest
386+
{
387+
[System.Runtime.Serialization.DataMember]
388+
protected bool Test { get; }
389+
}
390+
391+
[System.Runtime.Serialization.DataContract]
392+
public class InheritedPropertyErrorTestChild : InheritedPropertyErrorTest
393+
{
394+
}
333395
}
334396

335397
public class TestMappedList

0 commit comments

Comments
 (0)