Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bug fix: cannot deserialize a complex type with a collection

  • Loading branch information...
commit af1029eea124c03a7436da3d6ccd2ebc766bd84f 1 parent 56ea0f8
Adrian Aisemberg authored
Showing with 86 additions and 0 deletions.
  1. +2 −0  CLAP/TypeValidator.cs
  2. +30 −0 Tests/Samples.cs
  3. +54 −0 Tests/Tests.cs
2  CLAP/TypeValidator.cs
View
@@ -31,6 +31,7 @@ public static void Validate(object obj)
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var propsAndValues = properties.
+ Where(p => p.GetIndexParameters().None()).
Select(p => new ValueInfo(p.Name, p.PropertyType, p.GetValue(obj, null))).
ToArray();
@@ -76,6 +77,7 @@ public static void Validate(object obj)
{
var propertyPropsAndValues = value.GetType().
GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
+ Where(p => p.GetIndexParameters().None()).
Select(p => new ValueInfo(p.Name, p.PropertyType, p.GetValue(value, null))).
ToArray();
30 Tests/Samples.cs
View
@@ -1339,4 +1339,34 @@ void Error(ExceptionContext c)
public class Sample_65
{
}
+
+
+
+ public class Sample_66
+ {
+ public Person Person { get; set; }
+
+ [Verb]
+ public void Foo(Person p)
+ {
+ Person = p;
+ }
+ }
+
+ public class Person
+ {
+ public int Age { get; set; }
+ public string Name { get; set; }
+ public string Email { get; set; }
+ public List<PhoneNumber> PhoneNumbers { get; set; }
+ }
+
+ public class PhoneNumber
+ {
+ public string Type { get; set; }
+ public string Number { get; set; }
+
+ [MoreThan(10)]
+ public int SomeNumber { get; set; }
+ }
}
54 Tests/Tests.cs
View
@@ -2396,5 +2396,59 @@ public void HandlePreMatureError_RegisteredHandler()
Assert.IsTrue(handled);
}
+
+ [Test]
+ [ExpectedException(typeof(ValidationException))]
+ public void DeserializeComplexTypeWithCollection_Json_ValidationError()
+ {
+ var s = new Sample_66();
+
+ var json = "{Age: 34,Name: 'Adrian',Email: 'adrianaisemberg@gmail.com',PhoneNumbers:[{Type:'Home',Number:'001-2232322'},{Type:'Mobile',Number:'002-7787787'},{Type: 'Office',Number: '003-4463565'}]}";
+
+ Parser.Run(new[] { "foo", "-p=" + json }, s);
+
+ Assert.AreEqual("Adrian", s.Person.Name);
+ Assert.AreEqual(3, s.Person.PhoneNumbers.Count);
+ }
+
+ [Test]
+ [ExpectedException(typeof(ValidationException))]
+ public void DeserializeComplexTypeWithCollection_Xml_ValidationError()
+ {
+ var s = new Sample_66();
+
+ var xml = "<Person><Age>34</Age><Name>Adrian</Name><Email>adrianaisemberg@gmail.com</Email><PhoneNumbers><PhoneNumber><Type>Home</Type><Number>001-2232322</Number></PhoneNumber><PhoneNumber><Type>Mobile</Type><Number>002-7787787</Number></PhoneNumber><PhoneNumber><Type>Office</Type><Number>003-4463565</Number></PhoneNumber></PhoneNumbers></Person>";
+
+ Parser.Run(new[] { "foo", "-p=" + xml }, s);
+
+ Assert.AreEqual("Adrian", s.Person.Name);
+ Assert.AreEqual(3, s.Person.PhoneNumbers.Count);
+ }
+
+ [Test]
+ public void DeserializeComplexTypeWithCollection_Json()
+ {
+ var s = new Sample_66();
+
+ var json = "{Age: 34,Name: 'Adrian',Email: 'adrianaisemberg@gmail.com',PhoneNumbers:[{Type:'Home',Number:'001-2232322',SomeNumber:20},{Type:'Mobile',Number:'002-7787787',SomeNumber:21},{Type: 'Office',Number: '003-4463565',SomeNumber:30}]}";
+
+ Parser.Run(new[] { "foo", "-p=" + json }, s);
+
+ Assert.AreEqual("Adrian", s.Person.Name);
+ Assert.AreEqual(3, s.Person.PhoneNumbers.Count);
+ }
+
+ [Test]
+ public void DeserializeComplexTypeWithCollection_Xml()
+ {
+ var s = new Sample_66();
+
+ var xml = "<Person><Age>34</Age><Name>Adrian</Name><Email>adrianaisemberg@gmail.com</Email><PhoneNumbers><PhoneNumber><Type>Home</Type><Number>001-2232322</Number><SomeNumber>20</SomeNumber></PhoneNumber><PhoneNumber><Type>Mobile</Type><Number>002-7787787</Number><SomeNumber>21</SomeNumber></PhoneNumber><PhoneNumber><Type>Office</Type><Number>003-4463565</Number><SomeNumber>30</SomeNumber></PhoneNumber></PhoneNumbers></Person>";
+
+ Parser.Run(new[] { "foo", "-p=" + xml }, s);
+
+ Assert.AreEqual("Adrian", s.Person.Name);
+ Assert.AreEqual(3, s.Person.PhoneNumbers.Count);
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.