Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.C
if (inactiveProperty?.Value is FhirBoolean isInactive)
newContains.Inactive = isInactive.Value;

#if !STU3
#if R5 || R6
Comment thread
alexzautke marked this conversation as resolved.
Outdated
if (source.Property.Any())
{
newContains.Property = source.Property.Select(p => new ValueSet.ConceptPropertyComponent { Code = p.Code, Value = p.Value }).ToList();
Expand Down
36 changes: 36 additions & 0 deletions src/Hl7.Fhir.Specification.Shared.Tests/Source/TerminologyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Specification.Terminology;
using System;
Expand Down Expand Up @@ -757,6 +758,41 @@ void expandAction(string url)
}
}

/// <summary>
/// Test for issue with ValueSet expansion causing NullReferenceException during serialization
/// when the expansion contains property field that doesn't exist in R4/R4B.
/// The Property field in ValueSet.expansion.contains was introduced in R5.
/// </summary>
[Fact]
public async Tasks.Task ExpandedValueSetShouldSerializeSuccessfully()
{
var server = new LocalTerminologyService(_resolver);
var parameters = new Parameters()
{
Parameter = new List<Parameters.ParameterComponent>
{
new Parameters.ParameterComponent()
{
Name = "url",
Value = new FhirUri("http://hl7.org/fhir/ValueSet/administrative-gender"),
},
},
};

var resource = await server.Expand(parameters);

// This should not throw a NullReferenceException
var json = await resource.ToJsonAsync();

// Verify the expansion was successful
Assert.NotNull(json);
Assert.NotEmpty(json);
var valueSet = resource as ValueSet;
Assert.NotNull(valueSet);
Assert.True(valueSet.HasExpansion);
Assert.True(valueSet.Expansion.Contains.Any());
}

[Fact]
public void TestValidateCodeParametersCode()
{
Expand Down