Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

checked for null elements when serializing a collection

  • Loading branch information...
commit 167f41b6ad193a2f2ca8be51234cfa80c3b91d00 1 parent cd60727
@mausch authored
View
40 SolrNet.Tests/CollectionFieldSerializerTests.cs
@@ -0,0 +1,40 @@
+#region license
+// Copyright (c) 2007-2010 Mauricio Scheffer
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#endregion
+
+using System.Collections.Generic;
+using System.Linq;
+using MbUnit.Framework;
+using SolrNet.Impl.FieldSerializers;
+
+namespace SolrNet.Tests {
+ [TestFixture]
+ public class CollectionFieldSerializerTests {
+ [Test]
+ public void Serialize_null_returns_empty() {
+ var s = new CollectionFieldSerializer(new DefaultFieldSerializer());
+ var p = s.Serialize(null).ToList();
+ Assert.AreEqual(0, p.Count);
+ }
+
+ [Test]
+ public void Serialize_collection_with_null_element() {
+ var s = new CollectionFieldSerializer(new DefaultFieldSerializer());
+ var c = new List<string> {"a", null };
+ var p = s.Serialize(c).ToList();
+ Assert.AreEqual(2, p.Count);
+ }
+ }
+}
View
1  SolrNet.Tests/SolrNet.Tests.csproj
@@ -84,6 +84,7 @@
<Compile Include="AllPropertiesMappingManagerTests.cs" />
<Compile Include="AttributesMappingManagerTests.cs" />
<Compile Include="CollapseResponseParserTests.cs" />
+ <Compile Include="CollectionFieldSerializerTests.cs" />
<Compile Include="CommitCommandTests.cs" />
<Compile Include="CommonServiceLocatorTests.cs" />
<Compile Include="DateTimeFieldParserTests.cs" />
View
23 SolrNet/Impl/FieldSerializers/CollectionFieldSerializer.cs
@@ -1,4 +1,5 @@
#region license
+
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
+
#endregion
using System;
@@ -21,7 +23,7 @@
namespace SolrNet.Impl.FieldSerializers {
/// <summary>
- /// Serializes 1-dimensional collections
+ /// Serializes 1-dimensional collections
/// </summary>
public class CollectionFieldSerializer : ISolrFieldSerializer {
private readonly ISolrFieldSerializer valueSerializer;
@@ -31,18 +33,23 @@ public class CollectionFieldSerializer : ISolrFieldSerializer {
}
public bool CanHandleType(Type t) {
- return t != typeof(string) &&
- typeof(IEnumerable).IsAssignableFrom(t) &&
- !typeof(IDictionary).IsAssignableFrom(t) &&
- !TypeHelper.IsGenericAssignableFrom(typeof(IDictionary<,>), t);
+ return t != typeof (string) &&
+ typeof (IEnumerable).IsAssignableFrom(t) &&
+ !typeof (IDictionary).IsAssignableFrom(t) &&
+ !TypeHelper.IsGenericAssignableFrom(typeof (IDictionary<,>), t);
}
public IEnumerable<PropertyNode> Serialize(object obj) {
if (obj == null)
yield break;
- foreach (var o in (IEnumerable)obj)
- foreach (var n in valueSerializer.Serialize(o))
- yield return n;
+ foreach (var o in (IEnumerable) obj) {
+ var e = valueSerializer.Serialize(o);
+ if (e == null)
+ yield return new PropertyNode();
+ else
+ foreach (var n in e)
+ yield return n;
+ }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.