Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Model/IDeepCopyable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static class ListCopyExtensions
{
public static IEnumerable<T> DeepCopy<T>(this IEnumerable<T> source) where T : IDeepCopyable
{
return source.Select(item => item != null ? (T)item.DeepCopy() : default(T)).ToList();
return source.Where(item => item != null).Select(item => (T)item.DeepCopy()).ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ private void serializeInternal(
writer.WriteStartArray();

foreach (var value in coll)
serializeMemberValue(value, writer, filter, requiredType);
{
// Skip null values in collections
if (value is not null)
serializeMemberValue(value, writer, filter, requiredType);
}

writer.WriteEndArray();
}
Expand Down
6 changes: 5 additions & 1 deletion src/Hl7.Fhir.Base/Serialization/BaseFhirXmlPocoSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ private void serializeElement(IReadOnlyDictionary<string, object> members, XmlWr
if (mValue is ICollection coll and not byte[])
{
foreach (var value in coll)
serializeMemberValue(elementName, value, writer, filter);
{
// Skip null values in collections
if (value is not null)
serializeMemberValue(elementName, value, writer, filter);
}
}
else
serializeMemberValue(elementName, mValue, writer, filter);
Expand Down
73 changes: 73 additions & 0 deletions src/Hl7.Fhir.Serialization.R4.Tests/NullInListSerializationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace Hl7.Fhir.Serialization.Tests
{
[TestClass]
public class NullInListSerializationTest
{
[TestMethod]
public void TestSerializeWithNullInExtensionList()
{
var patient = new Patient { Id = "example" };
patient.Extension = new List<Extension> { null, new Extension("http://test", new FhirString("abcd")) };

// This should not throw a NullReferenceException
var serializer = new FhirJsonPocoSerializer();
var json = serializer.SerializeToString(patient);

// The null should be filtered out
Assert.IsFalse(json.Contains("null"));
Assert.IsTrue(json.Contains("http://test"));
Assert.IsTrue(json.Contains("abcd"));
}

[TestMethod]
public void TestSerializeXmlWithNullInExtensionList()
{
var patient = new Patient { Id = "example" };
patient.Extension = new List<Extension> { null, new Extension("http://test", new FhirString("abcd")) };

// This should not throw a NullReferenceException
var serializer = new FhirXmlPocoSerializer();
var xml = serializer.SerializeToString(patient);

// The null should be filtered out
Assert.IsTrue(xml.Contains("http://test"));
Assert.IsTrue(xml.Contains("abcd"));
}

[TestMethod]
public void TestSerializeWithNullInIdentifierList()
{
var patient = new Patient { Id = "example" };
patient.Identifier = new List<Identifier> { null, new Identifier { System = "http://example.org", Value = "12345" } };

// This should not throw a NullReferenceException
var serializer = new FhirJsonPocoSerializer();
var json = serializer.SerializeToString(patient);

// The null should be filtered out
Assert.IsFalse(json.Contains("null"));
Assert.IsTrue(json.Contains("http://example.org"));
Assert.IsTrue(json.Contains("12345"));
}

[TestMethod]
public void TestDeepCopyWithNullInExtensionList()
{
var patient = new Patient { Id = "example" };
patient.Extension = new List<Extension> { null, new Extension("http://test", new FhirString("abcd")) };

// This should not throw a NullReferenceException
var copy = (Patient)patient.DeepCopy();

// The null should be filtered out in the copy
Assert.IsNotNull(copy);
Assert.AreEqual(1, copy.Extension.Count);
Assert.AreEqual("http://test", copy.Extension[0].Url);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"resourceType": "Patient",
"id": "pat1-with-errors",
"meta": { "lastUpdated": "not a valid instant" },
"meta": {
"lastUpdated": "not a valid instant"
},
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Patient Donald DUCK @ Acme Healthcare, Inc. MR = 654321</p></div>"
},
"contained": [
null,
null,
null,
null,
{
"resourceType": "Patient",
"id": "1",
Expand All @@ -26,15 +24,22 @@
"id": "firstname",
"use": "official",
"family": "Everywoman",
"given": [ "This should be an array" ]
"given": [
"This should be an array"
]
},
{
"id": "anothername",
"use": "usual",
"given": [ "Eveline", null ],
"given": [
"Eveline",
null
],
"_given": [
null,
{ "id": "a1" }
{
"id": "a1"
}
]
}
],
Expand All @@ -49,14 +54,21 @@
"birthDate": "1973-05-31",
"address": [
{
"line": [ "2222 Home Street", null ],
"line": [
"2222 Home Street",
null
],
"_line": [
{ "id": "bla" },
{
"id": "bla"
},
null
]
}
],
"managingOrganization": { "reference": "Organization/hl7" }
"managingOrganization": {
"reference": "Organization/hl7"
}
},
{
"resourceType": "Questionnaire",
Expand All @@ -67,19 +79,25 @@
},
"status": "draft",
"date": "2012-01",
"subjectType": [ "Patient" ],
"subjectType": [
"Patient"
],
"item": [
{
"linkId": "1",
"text": "Cancer Quality Forum Questionnaire 2012",
"type": "display",
"option": [
{ "valueBoolean": true }
{
"valueBoolean": true
}
],
"item": [
{
"linkId": "1.1",
"code": [ {} ],
"code": [
{}
],
"type": "display",
"item": [
{
Expand All @@ -91,7 +109,9 @@
}
],
"type": "choice",
"options": { "reference": "#yesno" },
"options": {
"reference": "#yesno"
},
"item": [
{
"linkId": "1.1.1.1",
Expand All @@ -112,7 +132,9 @@
}
],
"type": "choice",
"options": { "reference": "#yesno" }
"options": {
"reference": "#yesno"
}
},
{
"linkId": "1.1.1.1.2",
Expand All @@ -129,7 +151,9 @@
}
],
"type": "choice",
"options": { "reference": "#yesno" }
"options": {
"reference": "#yesno"
}
},
{
"linkId": "1.1.1.1.3",
Expand All @@ -141,7 +165,9 @@
}
],
"type": "choice",
"options": { "reference": "#yesno" }
"options": {
"reference": "#yesno"
}
}
]
},
Expand Down Expand Up @@ -214,7 +240,10 @@
{
"use": "official",
"family": "Donald",
"given": [ "Duck", null ]
"given": [
"Duck",
null
]
}
],
"telecom": [
Expand All @@ -236,8 +265,12 @@
"url": "http://example.org/StructureDefinition/extension-with-number-as-code",
"valueCode": "28"
},
{ "url": "http://example.org/StructureDefinition/extension-without-type" },
{ "url": "http://example.org/StructureDefinition/extension-with-unknown-type" }
{
"url": "http://example.org/StructureDefinition/extension-without-type"
},
{
"url": "http://example.org/StructureDefinition/extension-with-unknown-type"
}
]
},
"birthDate": "1974-12-25",
Expand Down Expand Up @@ -277,7 +310,13 @@
],
"communication": [
{
"language": { "coding": [ { "code": "nl-nl" } ] },
"language": {
"coding": [
{
"code": "nl-nl"
}
]
},
"preferred": true,
"_preferred": {
"extension": [
Expand Down Expand Up @@ -305,5 +344,7 @@
"reference": "Organization/1",
"display": ""
},
"link": [ {} ]
}
"link": [
{}
]
}