liubiao4123 / servicestack

Automatically exported from code.google.com/p/servicestack
0 stars 0 forks source link

QueryStringSerializer backs out on null values #32

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Given the following class:

public class C
{
  public int? A { get; set; }
  public int? B { get; set; }
}

The following statements do not show correct output:

var c1 = new C { A = 1, B = 2 };
var c2 = new C { A = null, B = 2 };
var s1 = QueryStringSerializer.SerializeToString(c1); // = "A=1&B=2"
var s2 = QueryStringSerializer.SerializeToString(c2); // = ""

After some testing it appears all null properties will lead to following 
properties not being output. I think it's due to WriteQueryString in 
ServiceStack.Text.Common.WriteType
:

foreach (var propertyWriter in PropertyWriters)
{
  var propertyValue = propertyWriter.GetterFn((T)value);
  if (propertyValue == null) return;
  ...
}

Looks like that return should be a break.

Original issue reported on code.google.com by GrimaceO...@gmail.com on 25 Aug 2010 at 6:39

GoogleCodeExporter commented 8 years ago
er... I meant: continue, of course ;)

Original comment by GrimaceO...@gmail.com on 25 Aug 2010 at 6:41

GoogleCodeExporter commented 8 years ago
Yep that would do the trick.

Thanks for reporting and finding the bug - you're example is now apart of 
ServiceStack,  as a test case :)

Fix is attached.

Original comment by demis.be...@gmail.com on 25 Aug 2010 at 7:01

Attachments:

GoogleCodeExporter commented 8 years ago
I thought Ayende was a machine, but I'm beginning to suspect you were made in 
the same factory. Thanks again for the quick response!

Original comment by GrimaceO...@gmail.com on 25 Aug 2010 at 7:14

GoogleCodeExporter commented 8 years ago
lol - I just like fixing the easy ones :)

Original comment by demis.be...@gmail.com on 25 Aug 2010 at 7:19