Skip to content

Commit

Permalink
check for specific invalid BCP-47 format like en-USA or en_USA
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Feb 26, 2024
1 parent 86fd532 commit de4fb8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion api/src/main/java/org/openmrs/util/LocaleUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions api/src/test/java/org/openmrs/util/LocaleUtilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit de4fb8e

Please sign in to comment.