From de4fb8ebfe9ab56bab31245613f850593d0e838b Mon Sep 17 00:00:00 2001 From: mherman22 Date: Mon, 26 Feb 2024 13:11:05 +0300 Subject: [PATCH] check for specific invalid BCP-47 format like en-USA or en_USA --- api/src/main/java/org/openmrs/util/LocaleUtility.java | 6 +++++- .../test/java/org/openmrs/util/LocaleUtilityTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/openmrs/util/LocaleUtility.java b/api/src/main/java/org/openmrs/util/LocaleUtility.java index 54ba421e1a8c..547c1e0384d4 100644 --- a/api/src/main/java/org/openmrs/util/LocaleUtility.java +++ b/api/src/main/java/org/openmrs/util/LocaleUtility.java @@ -135,7 +135,11 @@ public static Locale fromSpecification(String localeSpecification) { try { createdLocale = LocaleUtils.toLocale(localeSpecification); } catch (IllegalArgumentException e) { - createdLocale = generateLocaleFromLegacyFormat(localeSpecification); + if (localeSpecification.matches("[a-zA-Z]{2}[-_][a-zA-Z]{2,}")) { + return null; + } else { + createdLocale = generateLocaleFromLegacyFormat(localeSpecification); + } } return createdLocale; diff --git a/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java b/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java index a7a06cfae37d..140b9f263a17 100644 --- a/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java +++ b/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java @@ -185,6 +185,17 @@ public void fromSpecification_shouldGetLocaleFromLanguageCodeAndCountryCode() { public void fromSpecification_shouldGetLocaleFromBCP47Format() { assertEquals(Locale.UK, LocaleUtility.fromSpecification("en-GB")); } + + /** + * @see LocaleUtility#fromSpecification(String) + */ + @Test + public void fromSpecification_shouldReturnNullWhenLocaleFormatIsIncorrectLocaleFromBCP47Format() { + LocaleUtility.fromSpecification("en-USA"); + LocaleUtility.fromSpecification("en_USA"); + assertEquals(LocaleUtility.fromSpecification("en-USA"), null); + assertEquals(LocaleUtility.fromSpecification("en_USA"), null); + } /** * @see LocaleUtility#fromSpecification(String)