Skip to content

Commit

Permalink
issue #3805 - fix FHIRPath evaluator string function error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Prasanna Hegde <prasanna.hegde1@ibm.com>
  • Loading branch information
PrasannaHegde1 committed Jul 27, 2022
1 parent f1ce72a commit e2d2eec
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 44 deletions.
105 changes: 105 additions & 0 deletions fhir-examples/src/main/resources/json/spec/appointment-example3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"resourceType": "Appointment",
"id": "examplereq",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Brian MRI results discussion</div>"
},
"identifier": [
{
"system": "http://example.org/sampleappointment-identifier",
"value": "123"
}
],
"status": "proposed",
"serviceCategory": [
{
"coding": [
{
"system": "http://example.org/service-category",
"code": "gp",
"display": "General Practice"
}
]
}
],
"specialty": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "394814009",
"display": "General practice"
}
]
}
],
"appointmentType": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0276",
"code": "WALKIN",
"display": "A previously unscheduled walk-in visit"
}
]
},
"reasonCode": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "413095006"
}
],
"text": "Clinical Review"
}
],
"priority": 5,
"description": "Discussion on the results of your recent MRI",
"minutesDuration": 15,
"slot": [
{
"reference": "Slot/example"
}
],
"created": "2015-12-02",
"comment": "Further expand on the results of the MRI and determine the next actions that may be appropriate.",
"participant": [
{
"actor": {
"reference": "Patient/example",
"display": "Peter James Chalmers"
},
"required": "required",
"status": "needs-action"
},
{
"type": [
{
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v3-ParticipationType",
"code": "ATND"
}
]
}
],
"required": "required",
"status": "needs-action"
},
{
"actor": {
"reference": "Location/1",
"display": "South Wing, second floor"
},
"required": "required",
"status": "accepted"
}
],
"requestedPeriod": [
{
"start": "2016-06-02",
"end": "2016-06-09"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_FALSE;
import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_TRUE;
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;

import java.util.Collection;
import java.util.List;
Expand All @@ -36,9 +35,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);
return getStringValue(context).contains(getStringValue(arguments.get(0))) ? SINGLETON_TRUE : SINGLETON_FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_FALSE;
import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_TRUE;
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;

import java.util.Collection;
import java.util.List;
Expand All @@ -36,9 +35,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);
return getStringValue(context).endsWith(getStringValue(arguments.get(0))) ? SINGLETON_TRUE : SINGLETON_FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package com.ibm.fhir.path.function;

import static com.ibm.fhir.path.FHIRPathIntegerValue.integerValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.singleton;

import java.util.Collection;
Expand All @@ -36,9 +35,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);
return singleton(integerValue(getStringValue(context).length()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_FALSE;
import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_TRUE;
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;

import java.util.Collection;
import java.util.List;
Expand All @@ -37,9 +36,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);

FHIRPathStringValue string = getStringValue(context);
FHIRPathStringValue regex = getStringValue(arguments.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

package com.ibm.fhir.path.function;

import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.singleton;

import java.util.Collection;
Expand All @@ -35,9 +34,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);
return singleton(getStringValue(context).replace(getStringValue(arguments.get(0)), getStringValue(arguments.get(1))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

package com.ibm.fhir.path.function;

import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.singleton;

import java.util.Collection;
Expand All @@ -35,9 +34,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);
return singleton(getStringValue(context).replaceMatches(getStringValue(arguments.get(0)), getStringValue(arguments.get(1))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_FALSE;
import static com.ibm.fhir.path.evaluator.FHIRPathEvaluator.SINGLETON_TRUE;
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;

import java.util.Collection;
import java.util.List;
Expand All @@ -36,9 +35,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}
checkStringValue(context);
return getStringValue(context).startsWith(getStringValue(arguments.get(0))) ? SINGLETON_TRUE : SINGLETON_FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.getInteger;
import static com.ibm.fhir.path.util.FHIRPathUtil.getStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.singleton;

import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import java.util.Collection;
import java.util.List;

Expand All @@ -37,9 +36,8 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!hasStringValue(context)) {
return empty();
}

checkStringValue(context);

FHIRPathStringValue value = getStringValue(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
package com.ibm.fhir.path.function;

import static com.ibm.fhir.path.FHIRPathStringValue.stringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.checkStringValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.empty;
import static com.ibm.fhir.path.util.FHIRPathUtil.getSystemValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.hasSystemValue;
import static com.ibm.fhir.path.util.FHIRPathUtil.isSingleton;
import static com.ibm.fhir.path.util.FHIRPathUtil.singleton;

import java.util.Collection;
Expand Down Expand Up @@ -38,9 +38,7 @@ public int getMaxArity() {

@Override
public Collection<FHIRPathNode> apply(EvaluationContext evaluationContext, Collection<FHIRPathNode> context, List<Collection<FHIRPathNode>> arguments) {
if (!isSingleton(context)) {
return empty();
}
checkStringValue(context);
if (hasSystemValue(context)) {
FHIRPathSystemValue value = getSystemValue(context);
return singleton(stringValue(value.toString()));
Expand Down
18 changes: 18 additions & 0 deletions fhir-path/src/main/java/com/ibm/fhir/path/util/FHIRPathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1238,4 +1238,22 @@ public static com.ibm.fhir.model.type.String getDisplay(FHIRPathTree tree, FHIRP
}
return null;
}

/**
* Check if the given collection item is of type String.
* @param nodes
* @return
* @throws IllegalArgumentException if the given collection item has more than one item or if the given collection item is not of type String
*/
public static boolean checkStringValue(Collection<FHIRPathNode> nodes) {
if(!isSingleton(nodes)) {
throw new IllegalArgumentException("Input collection must not contain more than one item, but found " + nodes.size());

} else if(!hasStringValue(nodes)) {
FHIRPathNode node = getSingleton(nodes);
throw new IllegalArgumentException("Input collection item must be of type String, but found '"+node.type().getName()+ "'");

}
return true;
}
}
Loading

0 comments on commit e2d2eec

Please sign in to comment.