Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation added for Target resource type for include and revinclude search options #2776

Merged
merged 9 commits into from
Sep 1, 2022
Merged
9 changes: 9 additions & 0 deletions src/Microsoft.Health.Fhir.Core/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Microsoft.Health.Fhir.Core/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@
<value>The '_revinclude:iterate={0}' search parameter has multiple target types. Please specify a target type.</value>
<comment>_revinclude:iterate is a query parameter.</comment>
</data>
<data name="IncludeRevIncludeInvalidTargetResourceType" xml:space="preserve">
<value>Invalid TargetResourceType : value cannot be empty.</value>
rajithaalurims marked this conversation as resolved.
Show resolved Hide resolved
<comment>TargetResourceType is an optional parameter</comment>
</data>
<data name="InvalidBooleanConfigSetting" xml:space="preserve">
<value>Field '{0}' with value '{1}' is not supported.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using EnsureThat;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Utility;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Health.Core.Features.Context;
Expand Down Expand Up @@ -421,6 +422,18 @@ public SearchOptions Create(string compartmentType, string compartmentId, string
string.Format(Core.Resources.RevIncludeIterateTargetTypeNotSpecified, p.query));
}

if (expression.TargetResourceType != null &&
rajithaalurims marked this conversation as resolved.
Show resolved Hide resolved
string.IsNullOrWhiteSpace(expression.TargetResourceType))
{
throw new BadRequestException(
string.Format(Core.Resources.IncludeRevIncludeInvalidTargetResourceType, expression.TargetResourceType));
}

if (expression.TargetResourceType != null && !ModelInfoProvider.IsKnownResource(expression.TargetResourceType))
rbans96 marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ResourceNotSupportedException(expression.TargetResourceType);
}

// For circular include iterate expressions, add an informational issue indicating that a single iteration is supported.
// See https://www.hl7.org/fhir/search.html#revinclude.
if (expression.Iterate && expression.CircularReference)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,42 @@ public async Task GivenARevIncludeIterateSearchExpressionWithCircularReference_W
};
}

[Theory]
[InlineData("_include")]
[InlineData("_revinclude")]
[HttpIntegrationFixtureArgumentSets(DataStore.SqlServer)]
public async Task GivenAIncludeOrRevIncludeIterateSearchExpressionWithInvalidTargetResourceType_WhenSearched_ShouldThrowResourceNotSupportedException(string include)
{
string query = $"{include}=Observation:subject:NotAResourceType";

using var fhirException = await Assert.ThrowsAsync<FhirException>(async () => await Client.SearchAsync(ResourceType.Patient, query));
Assert.Equal(HttpStatusCode.BadRequest, fhirException.StatusCode);

string[] expectedDiagnostics = { string.Format(Core.Resources.ResourceNotSupported, "NotAResourceType") };
IssueSeverity[] expectedIssueSeverities = { IssueSeverity.Error };
IssueType[] expectedCodeTypes = { IssueType.NotSupported };
ValidateOperationOutcome(expectedDiagnostics, expectedIssueSeverities, expectedCodeTypes, fhirException.OperationOutcome);
}

[Theory]
[InlineData("_include", "")]
[InlineData("_include", " ")]
[InlineData("_revinclude", "")]
[InlineData("_revinclude", " ")]
[HttpIntegrationFixtureArgumentSets(DataStore.SqlServer)]
public async Task GivenAIncludeOrRevIncludeIterateSearchExpressionWithEmptyOrWhiteSpaceTargetResourceType_WhenSearched_ShouldThrowBadRequestException(string include, string target)
{
string query = $"{include}=Observation:subject:{target}";

using var fhirException = await Assert.ThrowsAsync<FhirException>(async () => await Client.SearchAsync(ResourceType.Patient, query));
Assert.Equal(HttpStatusCode.BadRequest, fhirException.StatusCode);

string[] expectedDiagnostics = { string.Format(Core.Resources.IncludeRevIncludeInvalidTargetResourceType) };
IssueSeverity[] expectedIssueSeverities = { IssueSeverity.Error };
IssueType[] expectedCodeTypes = { IssueType.Invalid };
ValidateOperationOutcome(expectedDiagnostics, expectedIssueSeverities, expectedCodeTypes, fhirException.OperationOutcome);
}

// This will not work for circular reference
private static void ValidateSearchEntryMode(Bundle bundle, ResourceType matchResourceType)
{
Expand Down