Skip to content

Commit

Permalink
Merge pull request #1737 from IBM/issue-1629
Browse files Browse the repository at this point in the history
Issue #1629 - return error if bad resource type on revinclude
  • Loading branch information
michaelwschroeder authored Nov 20, 2020
2 parents f87cfba + 55eb9e2 commit 48c43d8
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 66 deletions.
103 changes: 62 additions & 41 deletions fhir-search/src/main/java/com/ibm/fhir/search/util/SearchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,8 @@ public static FHIRSearchContext parseQueryParameters(Class<?> resourceType,
if (ModelSupport.isResourceType(resType)) {
resourceTypes.add(resType);
} else {
String msg = "_type search parameter has invalid resource type:" + resType;
if (lenient) {
// TODO add this to the list of supplemental warnings?
log.log(Level.FINE, msg);
continue;
} else {
throw SearchExceptionUtil.buildNewInvalidSearchException(msg);
}
manageException("_type search parameter has invalid resource type:" + resType, lenient);
continue;
}
}
}
Expand Down Expand Up @@ -1867,7 +1861,8 @@ private static void parseInclusionParameter(Class<?> resourceType, FHIRSearchCon
// Parse value into 3 parts: joinResourceType, searchParameterName, searchParameterTargetType
inclusionValueParts = inclusionValue.split(":");
if (inclusionValueParts.length < 2) {
throw SearchExceptionUtil.buildNewInvalidSearchException("A value for _include or _revinclude must have at least 2 parts separated by a colon.");
manageException("A value for _include or _revinclude must have at least 2 parts separated by a colon.", lenient);
continue;
}
joinResourceType = inclusionValueParts[0];
searchParameterName = inclusionValueParts[1];
Expand All @@ -1877,28 +1872,37 @@ private static void parseInclusionParameter(Class<?> resourceType, FHIRSearchCon

// For _include parameter, join resource type must match resource type being searched
if (!joinResourceType.equals(resourceType.getSimpleName())) {
throw SearchExceptionUtil.buildNewInvalidSearchException(
"The join resource type must match the resource type being searched.");
manageException("The join resource type must match the resource type being searched.", lenient);
continue;
}

// Check allowed _include values
if (allowedIncludes != null && !allowedIncludes.contains(inclusionValue)) {
throw SearchExceptionUtil.buildNewInvalidSearchException("'" + inclusionValue
+ "' is not a valid _include parameter value for resource type '" + resourceType.getSimpleName() + "'");
manageException("'" + inclusionValue + "' is not a valid _include parameter value for resource type '"
+ resourceType.getSimpleName() + "'", lenient);
continue;
}
}

if (SearchConstants.REVINCLUDE.equals(inclusionKeyword)) {

// For _revinclude parameter, join resource type must be valid resource type
if (!ModelSupport.isResourceType(joinResourceType)) {
manageException("'" + joinResourceType + "' is not a valid resource type.", lenient);
continue;
}

// For _revinclude parameter, target resource type, if specified, must match resource type being searched
if (searchParameterTargetType != null && !searchParameterTargetType.equals(resourceType.getSimpleName())) {
throw SearchExceptionUtil.buildNewInvalidSearchException("The search parameter target type must match the resource type being searched.");
manageException("The search parameter target type must match the resource type being searched.", lenient);
continue;
}

// Check allowed _revinclude values
if (allowedRevIncludes != null && !allowedRevIncludes.contains(inclusionValue)) {
throw SearchExceptionUtil.buildNewInvalidSearchException("'" + inclusionValue
+ "' is not a valid _revinclude parameter value for resource type '" + resourceType.getSimpleName() + "'");
manageException("'" + inclusionValue + "' is not a valid _revinclude parameter value for resource type '"
+ resourceType.getSimpleName() + "'", lenient);
continue;
}
}

Expand All @@ -1912,29 +1916,34 @@ private static void parseInclusionParameter(Class<?> resourceType, FHIRSearchCon
} else {
searchParm = getSearchParameter(joinResourceType, searchParameterName);
if (searchParm == null) {
String msg = "Undefined Inclusion Parameter: " + inclusionValue;
if (lenient) {
log.fine(msg);
continue;
} else {
throw SearchExceptionUtil.buildNewInvalidSearchException(msg);
}
manageException("Undefined Inclusion Parameter: " + inclusionValue, lenient);
continue;
}
if (!SearchParamType.REFERENCE.equals(searchParm.getType())) {
throw SearchExceptionUtil.buildNewInvalidSearchException("Inclusion Parameter must be of type 'reference'. "
+ "The passed Inclusion Parameter is of type: '" + searchParm.getType().getValue() + "'");
manageException("Inclusion Parameter must be of type 'reference'. The passed Inclusion Parameter is of type: '"
+ searchParm.getType().getValue() + "'" + inclusionValue, lenient);
continue;
}
searchParametersMap = Collections.singletonMap(searchParameterName, searchParm);
}
for (Map.Entry<String, SearchParameter> entry : searchParametersMap.entrySet()) {
if (inclusionKeyword.equals(SearchConstants.INCLUDE)) {
newInclusionParms =
buildIncludeParameter(resourceType, joinResourceType, entry.getValue(), entry.getKey(), searchParameterTargetType);
context.getIncludeParameters().addAll(newInclusionParms);
try {
for (Map.Entry<String, SearchParameter> entry : searchParametersMap.entrySet()) {
if (inclusionKeyword.equals(SearchConstants.INCLUDE)) {
newInclusionParms =
buildIncludeParameter(resourceType, joinResourceType, entry.getValue(), entry.getKey(), searchParameterTargetType);
context.getIncludeParameters().addAll(newInclusionParms);
} else {
newInclusionParm =
buildRevIncludeParameter(resourceType, joinResourceType, entry.getValue(), entry.getKey(), searchParameterTargetType);
context.getRevIncludeParameters().add(newInclusionParm);
}
}
} catch (FHIRSearchException e) {
if (lenient) {
log.fine(e.getMessage());
continue;
} else {
newInclusionParm =
buildRevIncludeParameter(resourceType, joinResourceType, entry.getValue(), entry.getKey(), searchParameterTargetType);
context.getRevIncludeParameters().add(newInclusionParm);
throw e;
}
}
}
Expand Down Expand Up @@ -2090,14 +2099,8 @@ private static void parseElementsParameter(Class<?> resourceType, FHIRSearchCont
throw SearchExceptionUtil.buildNewInvalidSearchException("Invalid element name: " + elementName);
}
if (!resourceFieldNames.contains(elementName)) {
if (lenient) {
// TODO add this to the list of supplemental warnings?
log.fine("Skipping unknown element name: " + elementName);
continue;
} else {
throw SearchExceptionUtil
.buildNewInvalidSearchException("Unknown element name: " + elementName);
}
manageException("Unknown element name: " + elementName, lenient);
continue;
}
context.addElementsParameter(elementName);
}
Expand Down Expand Up @@ -2134,4 +2137,22 @@ public static Set<String> getSummaryTextElementNames(Class<?> resourceType) {
summaryTextList.add("text");
return Collections.unmodifiableSet(summaryTextList);
}

/**
* Either throw a FHIRSearchException or log the error message, depending on if
* we are in strict or lenient mode.
*
* @param message
* The error message.
* @param lenient
* A flag indicating lenient or strict mode.
* @throws FHIRSearchException
*/
private static void manageException(String message, boolean lenient) throws FHIRSearchException {
if (lenient) {
log.fine(message);
} else {
throw SearchExceptionUtil.buildNewInvalidSearchException(message);
}
}
}
Loading

0 comments on commit 48c43d8

Please sign in to comment.