Skip to content

Commit

Permalink
Reference to Uri Converter (#4721)
Browse files Browse the repository at this point in the history
Reference to Uri Converter, enables custom search parameters with properties of type reference to be indexed as uri type.

Refs AB#131463
  • Loading branch information
brendankowitz authored Dec 3, 2024
1 parent bd101b1 commit ea75818
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Hl7.Fhir.ElementModel;
using Hl7.FhirPath;
using Microsoft.Health.Fhir.Core.Features.Search.SearchValues;

namespace Microsoft.Health.Fhir.Core.Features.Search.Converters
{
/// <summary>
/// A converter used to convert from <see cref="ReferenceSearchValue"/> to <see cref="Uri"/>.
/// </summary>
public class ReferenceToUriSearchValueConverter() :
FhirTypedElementToSearchValueConverter<UriSearchValue>("Reference")
{
protected override IEnumerable<ISearchValue> Convert(ITypedElement value)
{
if (value.Scalar("reference") is not string reference)
{
yield break;
}

// Contained resources will not be searchable.
if (reference.StartsWith('#')
|| reference.StartsWith("urn:", StringComparison.Ordinal))
{
yield break;
}

yield return new UriSearchValue(reference, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using Hl7.Fhir.Model;
using Microsoft.Health.Fhir.Core.Features.Search.Converters;
using Microsoft.Health.Fhir.Tests.Common;
using Microsoft.Health.Test.Utilities;
using Xunit;
using static Microsoft.Health.Fhir.Tests.Common.Search.SearchValueValidationHelper;
using Task = System.Threading.Tasks.Task;

namespace Microsoft.Health.Fhir.Core.UnitTests.Features.Search.Converters
{
[Trait(Traits.OwningTeam, OwningTeam.Fhir)]
[Trait(Traits.Category, Categories.Search)]
public class ReferenceToUriSearchValueConverterTests : FhirTypedElementToSearchValueConverterTests<ReferenceToUriSearchValueConverter, ResourceReference>
{
[Fact]
public async Task GivenAFhirReferenceWithNoValue_WhenConverted_ThenNoSearchValueShouldBeCreated()
{
await Test(uri => uri.Reference = null);
}

[Fact]
public async Task GivenAFhirReferenceWithBasicValue_WhenConverted_ThenAUriSearchValueShouldBeCreated()
{
const string value = "http://uri";

await Test(
uri => uri.Reference = value,
ValidateUri,
value);
}

[Fact]
public async Task GivenAFhirReferenceWithReferenceValue_WhenConverted_ThenAUriSearchValueShouldBeCreated()
{
const string value = "Patient/123ABC";

await Test(
uri => uri.Reference = value,
ValidateUri,
value);
}

[Fact]
public async Task GivenAFhirReferenceWithInvalidURL_WhenConverted_ThenSearchValueShouldBeCreated()
{
const string value = "this is not a valid url";

await Test(
uri => uri.Reference = value,
ValidateUri,
value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Features\Resources\ResourceHandlerTests_ConditionalDelete.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Features\Search\Converters\RangeToNumberSearchValueConverterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Features\Search\Converters\RangeToQuantitySearchValueConverterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Features\Search\Converters\ReferenceToUriSearchValueConverterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Features\Search\DataResourceFilterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Features\Search\ExtensionToNumberSearchValueConverterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Features\Search\ExtensionToDateTimeSearchValueConverterTests.cs" />
Expand Down

0 comments on commit ea75818

Please sign in to comment.