Skip to content

Commit

Permalink
Merge pull request #2554 from FirelyTeam/bugfix/142-fix-valueset-expa…
Browse files Browse the repository at this point in the history
…nder-error-message

Change error message when the ValueSetExpander can't find a specific CodeSystem
  • Loading branch information
mmsmits authored Jul 31, 2023
2 parents 04bd24b + f0f5268 commit 7fdb121
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using T=System.Threading.Tasks;
using T = System.Threading.Tasks;

namespace Hl7.Fhir.Specification.Terminology
{
public class ValueSetExpander
{

//ValueSetExpander keeps throwing TerminologyService Exceptions to not change the public interface.
//ValueSetExpander keeps throwing TerminologyService Exceptions to not change the public interface.
#pragma warning disable 0618

public ValueSetExpanderSettings Settings { get; }
Expand Down Expand Up @@ -55,19 +55,19 @@ public async T.Task ExpandAsync(ValueSet source)
source.Expansion = null;
throw;
}

}

private void setExpansionParameters(ValueSet vs)
{
vs.Expansion.Parameter = new List<ValueSet.ParameterComponent>();
if(Settings.IncludeDesignations)
if (Settings.IncludeDesignations)
{
vs.Expansion.Parameter.Add(new ValueSet.ParameterComponent
{
Name = "includeDesignations",
Value = new FhirBoolean(true)
});
});
}
//TODO add more parameters to the valuset here when we implement them.
}
Expand Down Expand Up @@ -122,15 +122,15 @@ private async T.Task handleCompose(ValueSet source)
{
// We'd probably really have to look this code up in the original ValueSet (by system) to know something about 'abstract'
// and what would we do with a hierarchy if we encountered that in the include?
if(Settings.IncludeDesignations)
if (Settings.IncludeDesignations)
{
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display, concept.Designation);
}
else
{
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display);
}

}
}
else
Expand All @@ -152,7 +152,7 @@ private async T.Task handleCompose(ValueSet source)
var concepts = await getExpansionForValueSet(importedVs).ConfigureAwait(false);
import(result, concepts, importedVs);
}

return result;

void import(List<ValueSet.ContainsComponent> dest, List<ValueSet.ContainsComponent> source, string importeeUrl)
Expand Down Expand Up @@ -209,7 +209,7 @@ private async T.Task handleExclude(ValueSet source)
"set ValueSetExpander.Settings.ValueSetSource to fix.");

var importedVs = await Settings.ValueSetSource.AsAsync().FindValueSetAsync(uri).ConfigureAwait(false);
if (importedVs == null) throw new ValueSetUnknownException($"Cannot resolve canonical reference '{uri}' to ValueSet");
if (importedVs == null) throw new ValueSetUnknownException($"The ValueSet expander cannot find system '{uri}', so the expansion cannot be completed.");

if (!importedVs.HasExpansion) await ExpandAsync(importedVs).ConfigureAwait(false);

Expand All @@ -226,7 +226,7 @@ private async T.Task handleExclude(ValueSet source)
"set ValueSetExpander.Settings.ValueSetSource to fix.");

var importedCs = await Settings.ValueSetSource.AsAsync().FindCodeSystemAsync(uri).ConfigureAwait(false);
if (importedCs == null) throw new ValueSetUnknownException($"Cannot resolve canonical reference '{uri}' to CodeSystem");
if (importedCs == null) throw new ValueSetUnknownException($"The ValueSet expander cannot find system '{uri}', so the expansion cannot be completed.");

var result = new List<ValueSet.ContainsComponent>();
result.AddRange(importedCs.Concept.Select(c => c.ToContainsComponent(importedCs, Settings)));
Expand Down Expand Up @@ -294,9 +294,9 @@ internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.C
newContains.System = system.Url;
newContains.Version = system.Version;
newContains.Code = source.Code;
newContains.Display = source.Display;
if(settings.IncludeDesignations)
newContains.Designation = source.Designation.ToValueSetDesignations();
newContains.Display = source.Display;
if (settings.IncludeDesignations)
newContains.Designation = source.Designation.ToValueSetDesignations();

var abstractProperty = source.ListConceptProperties(system, CodeSystem.CONCEPTPROPERTY_NOT_SELECTABLE).SingleOrDefault();
if (abstractProperty?.Value is FhirBoolean isAbstract)
Expand All @@ -316,7 +316,7 @@ internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.C
private static List<ValueSet.DesignationComponent> ToValueSetDesignations(this List<CodeSystem.DesignationComponent> csDesignations)
{
var vsDesignations = new List<ValueSet.DesignationComponent>();
csDesignations.ForEach(d => vsDesignations.Add(d.ToValueSetDesignation()));
csDesignations.ForEach(d => vsDesignations.Add(d.ToValueSetDesignation()));
return vsDesignations;
}

Expand All @@ -331,5 +331,5 @@ private static ValueSet.DesignationComponent ToValueSetDesignation(this CodeSyst
}

}
#pragma warning restore
#pragma warning restore
}
28 changes: 14 additions & 14 deletions src/Hl7.Fhir.STU3/Specification/Terminology/ValueSetExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using T=System.Threading.Tasks;
using T = System.Threading.Tasks;

namespace Hl7.Fhir.Specification.Terminology
{
public class ValueSetExpander
{

//ValueSetExpander keeps throwing TerminologyService Exceptions to not change the public interface.
//ValueSetExpander keeps throwing TerminologyService Exceptions to not change the public interface.
#pragma warning disable 0618

public ValueSetExpanderSettings Settings { get; }
Expand Down Expand Up @@ -55,19 +55,19 @@ public async T.Task ExpandAsync(ValueSet source)
source.Expansion = null;
throw;
}

}

private void setExpansionParameters(ValueSet vs)
{
vs.Expansion.Parameter = new List<ValueSet.ParameterComponent>();
if(Settings.IncludeDesignations)
if (Settings.IncludeDesignations)
{
vs.Expansion.Parameter.Add(new ValueSet.ParameterComponent
{
Name = "includeDesignations",
Value = new FhirBoolean(true)
});
});
}
//TODO add more parameters to the valuset here when we implement them.
}
Expand Down Expand Up @@ -122,15 +122,15 @@ private async T.Task handleCompose(ValueSet source)
{
// We'd probably really have to look this code up in the original ValueSet (by system) to know something about 'abstract'
// and what would we do with a hierarchy if we encountered that in the include?
if(Settings.IncludeDesignations)
if (Settings.IncludeDesignations)
{
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display, concept.Designation);
}
else
{
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display);
}

}
}
else
Expand All @@ -152,7 +152,7 @@ private async T.Task handleCompose(ValueSet source)
var concepts = await getExpansionForValueSet(importedVs).ConfigureAwait(false);
import(result, concepts, importedVs);
}

return result;

void import(List<ValueSet.ContainsComponent> dest, List<ValueSet.ContainsComponent> source, string importeeUrl)
Expand Down Expand Up @@ -226,7 +226,7 @@ private async T.Task handleExclude(ValueSet source)
"set ValueSetExpander.Settings.ValueSetSource to fix.");

var importedCs = await Settings.ValueSetSource.AsAsync().FindCodeSystemAsync(uri).ConfigureAwait(false);
if (importedCs == null) throw new ValueSetUnknownException($"Cannot resolve canonical reference '{uri}' to CodeSystem");
if (importedCs == null) throw new ValueSetUnknownException($"The ValueSet expander cannot find system '{uri}', so the expansion cannot be completed.");

var result = new List<ValueSet.ContainsComponent>();
result.AddRange(importedCs.Concept.Select(c => c.ToContainsComponent(importedCs, Settings)));
Expand Down Expand Up @@ -294,9 +294,9 @@ internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.C
newContains.System = system.Url;
newContains.Version = system.Version;
newContains.Code = source.Code;
newContains.Display = source.Display;
if(settings.IncludeDesignations)
newContains.Designation = source.Designation.ToValueSetDesignations();
newContains.Display = source.Display;
if (settings.IncludeDesignations)
newContains.Designation = source.Designation.ToValueSetDesignations();

var abstractProperty = source.ListConceptProperties(system, CodeSystem.CONCEPTPROPERTY_NOT_SELECTABLE).SingleOrDefault();
if (abstractProperty?.Value is FhirBoolean isAbstract)
Expand All @@ -316,7 +316,7 @@ internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.C
private static List<ValueSet.DesignationComponent> ToValueSetDesignations(this List<CodeSystem.DesignationComponent> csDesignations)
{
var vsDesignations = new List<ValueSet.DesignationComponent>();
csDesignations.ForEach(d => vsDesignations.Add(d.ToValueSetDesignation()));
csDesignations.ForEach(d => vsDesignations.Add(d.ToValueSetDesignation()));
return vsDesignations;
}

Expand All @@ -331,5 +331,5 @@ private static ValueSet.DesignationComponent ToValueSetDesignation(this CodeSyst
}

}
#pragma warning restore
#pragma warning restore
}
23 changes: 23 additions & 0 deletions src/Hl7.Fhir.Specification.STU3.Tests/Source/TerminologyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ public async T.Task ExpansionOfWholeSystem()
//Assert.Equal("http://hl7.org/fhir/ValueSet/issue-type?version=3.14", ((FhirUri)versionParam.Value).Value);
}

[Fact]
public async T.Task TestExpandingVsWithUnknownSystem()
{

var expander = new ValueSetExpander(new ValueSetExpanderSettings { ValueSetSource = new InMemoryResourceResolver() });
var vs = new ValueSet
{
Compose = new()
{
Include = new List<ValueSet.ConceptSetComponent>
{
new()
{
System = "http://www.unknown.org/"
}
}
}
};

var job = async () => await expander.ExpandAsync(vs);
await job.Should().ThrowAsync<ValueSetUnknownException>().WithMessage("The ValueSet expander cannot find system 'http://www.unknown.org/', so the expansion cannot be completed.");
}


[Fact]
public async T.Task ExpansionOfComposeInclude()
Expand Down
23 changes: 23 additions & 0 deletions src/Hl7.Fhir.Specification.Shared.Tests/Source/TerminologyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ public async T.Task ExpansionOfWholeSystem()
//Assert.Equal("http://hl7.org/fhir/ValueSet/issue-type?version=3.14", ((FhirUri)versionParam.Value).Value);
}

[Fact]
public async T.Task TestExpandingVsWithUnknownSystem()
{

var expander = new ValueSetExpander(new ValueSetExpanderSettings { ValueSetSource = new InMemoryResourceResolver() });
var vs = new ValueSet
{
Compose = new()
{
Include = new List<ValueSet.ConceptSetComponent>
{
new()
{
System = "http://www.unknown.org/"
}
}
}
};

var job = async () => await expander.ExpandAsync(vs);
await job.Should().ThrowAsync<ValueSetUnknownException>().WithMessage("The ValueSet expander cannot find system 'http://www.unknown.org/', so the expansion cannot be completed.");
}


[Fact]
public async T.Task ExpansionOfComposeInclude()
Expand Down

0 comments on commit 7fdb121

Please sign in to comment.