diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_7_0/3170-allow-language-code-validation-to-be-case-insensitive.yml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_7_0/3170-allow-language-code-validation-to-be-case-insensitive.yml new file mode 100644 index 00000000000..a1caa2d72b6 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_7_0/3170-allow-language-code-validation-to-be-case-insensitive.yml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 3170 +title: "Fixed language code validation so that it is case insensitive (eg, en-US, en-us, EN-US, EN-us should all work)" diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyService.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyService.java index f4891ed7398..a196cf85863 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyService.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyService.java @@ -212,7 +212,6 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport { @Override public LookupCodeResult lookupCode(ValidationSupportContext theValidationSupportContext, String theSystem, String theCode, String theDisplayLanguage) { - Map map; switch (theSystem) { case LANGUAGES_CODESYSTEM_URL: @@ -254,9 +253,7 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport { } private LookupCodeResult lookupLanguageCode(String theCode) { - Map languagesMap = myLanguagesLanugageMap; - Map regionsMap = myLanguagesRegionMap; - if (languagesMap == null || regionsMap == null) { + if (myLanguagesLanugageMap == null || myLanguagesRegionMap == null) { initializeBcp47LanguageMap(); } @@ -266,8 +263,10 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport { String region; if (hasRegionAndCodeSegments) { - language = myLanguagesLanugageMap.get(theCode.substring(0, langRegionSeparatorIndex)); - region = myLanguagesRegionMap.get(theCode.substring(langRegionSeparatorIndex + 1)); + // we look for languages in lowercase only + // this will allow case insensitivity for language portion of code + language = myLanguagesLanugageMap.get(theCode.substring(0, langRegionSeparatorIndex).toLowerCase()); + region = myLanguagesRegionMap.get(theCode.substring(langRegionSeparatorIndex + 1).toUpperCase()); if (language == null || region == null) { //In case the user provides both a language and a region, they must both be valid for the lookup to succeed. @@ -278,7 +277,8 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport { } } else { //In case user has only provided a language, we build the lookup from only that. - language = myLanguagesLanugageMap.get(theCode); + //NB: we only use the lowercase version of the language + language = myLanguagesLanugageMap.get(theCode.toLowerCase()); if (language == null) { ourLog.warn("Couldn't find a valid bcp47 language from code: {}", theCode); return buildNotFoundLookupCodeResult(theCode); diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyServiceTest.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyServiceTest.java index a099a68ac93..d3d73fbdefe 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyServiceTest.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/CommonCodeSystemsTerminologyServiceTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -54,6 +55,26 @@ public class CommonCodeSystemsTerminologyServiceTest { assertNull(outcome); } + @Test + public void lookupCode_languageOnlyLookup_isCaseInsensitive() { + IValidationSupport.LookupCodeResult outcomeUpper = mySvc.lookupCode(newSupport(), "urn:ietf:bcp:47", "SGN", "Sign Languages"); + IValidationSupport.LookupCodeResult outcomeLower = mySvc.lookupCode(newSupport(), "urn:ietf:bcp:47", "sgn", "Sign Languages"); + assertNotNull(outcomeUpper); + assertNotNull(outcomeLower); + assertTrue(outcomeLower.isFound()); + assertTrue(outcomeUpper.isFound()); + } + + @Test + public void lookupCode_languageAndRegionLookup_isCaseInsensitive() { + IValidationSupport.LookupCodeResult outcomeUpper = mySvc.lookupCode(newSupport(), "urn:ietf:bcp:47", "EN-US", "English"); + IValidationSupport.LookupCodeResult outcomeLower = mySvc.lookupCode(newSupport(), "urn:ietf:bcp:47", "en-us", "English"); + assertNotNull(outcomeUpper); + assertNotNull(outcomeLower); + assertTrue(outcomeLower.isFound()); + assertTrue(outcomeUpper.isFound()); + } + @Test public void testUcum_ValidateCode_Good() { ValueSet vs = new ValueSet();