Skip to content

Commit a91543c

Browse files
authored
Merge pull request #3030 from FirelyTeam/fix/canonical-cant-parse-fragment-only-urls
Canonical now parses fragment only URLs
2 parents 631f01e + 0d359c2 commit a91543c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/Hl7.Fhir.Base/Model/Canonical.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ public Canonical(Uri uri) : this(uri?.OriginalString)
5050
/// </summary>
5151
public Canonical(string? uri, string? version, string? fragment = null)
5252
{
53-
if (uri == null) throw Error.ArgumentNull(nameof(uri));
54-
if (uri.IndexOfAny(['|', '#']) != -1)
53+
if ((uri is not null) && uri.IndexOfAny(['|', '#']) != -1)
5554
throw Error.Argument(nameof(uri), "cannot contain version/fragment data");
5655

57-
if (version != null && version.IndexOfAny(['|', '#']) != -1)
56+
if ((version is not null) && version.IndexOfAny(['|', '#']) != -1)
5857
throw Error.Argument(nameof(version), "cannot contain version/fragment data");
5958

60-
if (fragment != null && fragment.IndexOfAny(['|', '#']) != -1)
59+
if ((fragment is not null) && fragment.IndexOfAny(['|', '#']) != -1)
6160
throw Error.Argument(nameof(fragment), "already contains version/fragment data");
6261

6362

src/Hl7.Fhir.Base/Specification/Terminology/ValidateCodeParameters.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public ValidateCodeParameters WithValueSet(string? url, string? context = null,
6262
return this;
6363
}
6464

65+
/// <summary>
66+
/// Takes a canonical and splits it into the correct "url", and "valueSetVersion" parameters.
67+
/// </summary>
68+
/// <param name="canonical">Canonical to be split up</param>
69+
/// <returns></returns>
70+
public ValidateCodeParameters WithValueSet(Canonical canonical)
71+
{
72+
var (uri, version, fragment) = canonical;
73+
Url = new FhirUri(new Canonical(uri, null, fragment));
74+
if (!string.IsNullOrWhiteSpace(version)) ValueSetVersion = new FhirString(version);
75+
return this;
76+
}
77+
6578
public ValidateCodeParameters WithCode(string? code = null, string? system = null,
6679
string? systemVersion = null, string? display = null, string? displayLanguage = null,
6780
string? context = null, bool? inferSystem = null)

src/Hl7.Fhir.Specification.Shared.Tests/Source/TerminologyTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,16 @@ public void TestValidateCodeParametersValueSet()
801801
paramResource.Parameter.Should().ContainSingle(p => p.Name == "context" && ((FhirUri)p.Value).Value == "Patient.gender");
802802
paramResource.Parameter.Should().ContainSingle(p => p.Name == "valueSet" && ((ValueSet)p.Resource) != null);
803803
paramResource.Parameter.Should().ContainSingle(p => p.Name == "valueSetVersion" && ((FhirString)p.Value).Value == "1.0.4");
804+
805+
806+
parameters = new ValidateCodeParameters()
807+
.WithValueSet(new Canonical("http://foo.bar/ValueSet/foo|1.0.4#fragment"));
808+
parameters.Url.Value.Should().Be("http://foo.bar/ValueSet/foo#fragment");
809+
parameters.ValueSetVersion.Value.Should().Be("1.0.4");
810+
811+
parameters = new ValidateCodeParameters()
812+
.WithValueSet(new Canonical("#fragment"));
813+
parameters.Url.Value.Should().Be("#fragment");
804814
}
805815

806816

0 commit comments

Comments
 (0)