Skip to content

Commit e01fbec

Browse files
authored
Fix terminology cache hashing (hapifhir#2006)
* Fix tx-cache by normalizing end-of-line * Simple tests to make sure we don't regress
1 parent 7ac1df7 commit e01fbec

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/utilities/TerminologyCache.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,10 @@ private String loadJS(JsonElement e) {
959959
}
960960

961961
public String hashJson(String s) {
962-
return String.valueOf(s.trim().hashCode());
962+
return String.valueOf(s
963+
.trim()
964+
.replaceAll("\\r\\n?", "\n")
965+
.hashCode());
963966
}
964967

965968
// management

org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/TerminologyCacheTests.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.hl7.fhir.r5.terminologies.expansion.ValueSetExpansionOutcome;
2727
import org.hl7.fhir.r5.terminologies.utilities.TerminologyCache;
2828
import org.hl7.fhir.r5.terminologies.utilities.ValidationResult;
29-
import org.hl7.fhir.utilities.Utilities;
3029
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
3130
import org.hl7.fhir.utilities.tests.ResourceLoaderTests;
3231
import org.hl7.fhir.utilities.validation.ValidationMessage;
@@ -550,4 +549,38 @@ public void testCacheTokenGeneration(String system, String expectedName) throws
550549
assertEquals(expectedName + "_dummyVersion", cacheToken.getName());
551550
}
552551
}
552+
553+
@Test
554+
void testHashJsonHandlesDifferentEndOfLine() throws IOException {
555+
TerminologyCache terminologyCache = createTerminologyCache();
556+
String newLineJson = "{\n" +
557+
" \"resourceType\": \"ValueSet\",\n" +
558+
" \"id\": \"dummyId\"\n" +
559+
"}";
560+
String newLineAndCarriageReturnJson = "{\r\n" +
561+
" \"resourceType\": \"ValueSet\",\r\n" +
562+
" \"id\": \"dummyId\"\r\n" +
563+
"}";
564+
String newLineToken = terminologyCache.hashJson(newLineJson);
565+
String newLineAndCarriageReturnToken = terminologyCache.hashJson(newLineAndCarriageReturnJson);
566+
567+
assertEquals(newLineToken, newLineAndCarriageReturnToken);
568+
}
569+
570+
@Test
571+
void testHashJsonHandlesLeadingAndTrailingWhitespace() throws IOException {
572+
TerminologyCache terminologyCache = createTerminologyCache();
573+
String paddedJson = " {\n" +
574+
" \"resourceType\": \"ValueSet\",\n" +
575+
" \"id\": \"dummyId\"\n" +
576+
"}\n ";
577+
String json = "{\n" +
578+
" \"resourceType\": \"ValueSet\",\n" +
579+
" \"id\": \"dummyId\"\n" +
580+
"}";
581+
String paddedJsonToken = terminologyCache.hashJson(paddedJson);
582+
String jsonToken = terminologyCache.hashJson(json);
583+
584+
assertEquals(paddedJsonToken, jsonToken);
585+
}
553586
}

org.hl7.fhir.validation/src/test/resources/txCache/org.hl7.fhir.validation/4.0.1/fhir-types.cache

-24
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
-------------------------------------------------------------------------------------
2-
{"code" : {
3-
"system" : "http://hl7.org/fhir/fhir-types",
4-
"code" : "OperationOutcome"
5-
}, "valueSet" :null, "langs":"en-CA", "useServer":"true", "useClient":"true", "guessSystem":"false", "activeOnly":"false", "membershipOnly":"false", "displayWarningMode":"false", "versionFlexible":"true", "profile": {
6-
"resourceType" : "Parameters",
7-
"parameter" : [{
8-
"name" : "profile-url",
9-
"valueString" : "http://hl7.org/fhir/ExpansionProfile/dc8fd4bc-091a-424a-8a3b-6198ef146891"
10-
},
11-
{
12-
"name" : "displayLanguage",
13-
"valueCode" : "en-US"
14-
}]
15-
}}####
16-
v: {
17-
"severity" : "error",
18-
"error" : "A definition for CodeSystem 'http://hl7.org/fhir/fhir-types' could not be found, so the code cannot be validated",
19-
"class" : "CODESYSTEM_UNSUPPORTED",
20-
"issues" : {
21-
"resourceType" : "OperationOutcome"
22-
}
23-
24-
}
25-
-------------------------------------------------------------------------------------
262
{"code" : {
273
"system" : "http://hl7.org/fhir/fhir-types",
284
"code" : "OperationOutcome"

0 commit comments

Comments
 (0)