Skip to content

Commit 786480e

Browse files
authored
Fixed an NPE when parsing malformed subjects (#1291)
1 parent c2356c8 commit 786480e

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

pipelines/batch/src/main/java/com/google/fhir/analytics/FetchResources.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 Google LLC
2+
* Copyright 2020-2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,20 +68,16 @@ static String getSubjectPatientIdOrNull(Resource resource) {
6868
Property subject = resource.getNamedProperty("subject");
6969
if (subject != null) {
7070
List<Base> values = subject.getValues();
71-
if (values.size() == 1) {
71+
if (values.size() == 1 && values.get(0) instanceof Reference) {
7272
Reference reference = (Reference) values.get(0);
7373
// TODO: Find a more generic way to check if this is a reference to a Patient. With the
7474
// current OpenMRS setup, reference.getType() is null so we cannot rely on that.
7575
String refStr = reference.getReference();
76-
Matcher matcher = PATIENT_REFERENCE.matcher(refStr);
77-
if (matcher.matches()) {
78-
patientId = matcher.group(1);
79-
}
80-
if (patientId == null) {
81-
log.warn(
82-
String.format(
83-
"Ignoring subject of %s with id %s because it is not a Patient reference: %s",
84-
resource.getResourceType(), resource.getId(), refStr));
76+
if (refStr != null) {
77+
Matcher matcher = PATIENT_REFERENCE.matcher(refStr);
78+
if (matcher.matches()) {
79+
patientId = matcher.group(1);
80+
}
8581
}
8682
}
8783
if (values.size() > 1) {
@@ -91,6 +87,9 @@ static String getSubjectPatientIdOrNull(Resource resource) {
9187
resource.getResourceType(), resource.getId()));
9288
}
9389
}
90+
if (patientId == null) {
91+
log.warn("Ignoring subject of {} with id {}", resource.getResourceType(), resource.getId());
92+
}
9493
return patientId;
9594
}
9695

pipelines/batch/src/test/java/com/google/fhir/analytics/FetchResourcesTest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 Google LLC
2+
* Copyright 2020-2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.apache.beam.sdk.transforms.Create;
4040
import org.apache.beam.sdk.values.KV;
4141
import org.apache.beam.sdk.values.PCollection;
42+
import org.hamcrest.core.IsNull;
4243
import org.hl7.fhir.r4.model.Bundle;
4344
import org.hl7.fhir.r4.model.Observation;
4445
import org.junit.Before;
@@ -86,6 +87,15 @@ public void testGetPatientId() throws IOException {
8687
assertThat(patientId, equalTo(expectedId));
8788
}
8889

90+
@Test
91+
public void testGetPatientIdBadSubject() throws IOException {
92+
URL url = Resources.getResource("observation_bad_subject.json");
93+
String obsStr = Resources.toString(url, StandardCharsets.UTF_8);
94+
Observation observation = parser.parseResource(Observation.class, obsStr);
95+
String patientId = FetchResources.getSubjectPatientIdOrNull(observation);
96+
assertThat(patientId, IsNull.nullValue());
97+
}
98+
8999
@Test
90100
public void testPatientIdsFromBundle() {
91101
List<SearchSegmentDescriptor> searchSegments =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"resourceType": "Observation",
3+
"id": "0adb6cfb-0ae3-42df-9d39-fc68ec4334cd",
4+
"meta": {
5+
"tag": [
6+
{
7+
"system": "http://hl7.org/fhir/v3/ObservationValue",
8+
"code": "SUBSETTED",
9+
"display": "Resource encoded in summary mode"
10+
}
11+
]
12+
},
13+
"status": "final",
14+
"code": {
15+
"coding": [
16+
{
17+
"code": "162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
18+
"display": "Text of encounter note"
19+
}
20+
]
21+
},
22+
"subject": {
23+
"identifier": {
24+
"type": {
25+
"coding": [ {
26+
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
27+
"code": "NI",
28+
"display": "National unique individual identifier"
29+
} ]
30+
},
31+
"system": "http://some.system.com",
32+
"value": "somevalue"
33+
}
34+
},
35+
"context": {
36+
"reference": "Encounter/2df7151b-fdde-4d1b-8dfa-aeaae4bb0430"
37+
},
38+
"effectiveDateTime": "2020-09-22T13:54:41-04:00",
39+
"issued": "2020-09-22T13:54:41.000-04:00",
40+
"valueString": "Adding a test diagnosis"
41+
}
42+

0 commit comments

Comments
 (0)