From 3f54b7733fa092447d4d2ac7c74eb7332dc22670 Mon Sep 17 00:00:00 2001 From: Samuel Raphael Date: Wed, 11 Sep 2024 10:33:43 -0700 Subject: [PATCH] Raise descriptive IllegalArgumentException when zipcode is - (#468) Summary: Fixes this issue - https://github.com/facebook/facebook-java-business-sdk/issues/460 > When executing a postal code validation on a String containing only "-" an ArrayIndexOutOfBoundsException is thrown. Pull Request resolved: https://github.com/facebook/facebook-java-business-sdk/pull/468 Reviewed By: yoongyj Differential Revision: D62392830 Pulled By: stcheng fbshipit-source-id: ba84bf0dd10eb9006dbde0da966f09542857e2c1 --- .../com/facebook/ads/utils/ServerSideApiUtil.java | 12 +++++++++--- .../facebook/ads/utils/ServerSideApiUtilTest.java | 9 ++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/facebook/ads/utils/ServerSideApiUtil.java b/src/main/java/com/facebook/ads/utils/ServerSideApiUtil.java index 6fed62a2..e4d294d2 100644 --- a/src/main/java/com/facebook/ads/utils/ServerSideApiUtil.java +++ b/src/main/java/com/facebook/ads/utils/ServerSideApiUtil.java @@ -49,7 +49,7 @@ public static String hash(String input) { * Checks whether the input is already hashed with SHA256 or MD5. * * @param input value that need to be validated. - * @return bool representing the whether the input is hashed + * @return bool representing whether the input is hashed */ public static boolean isAlreadyHashed(String input) { @@ -170,9 +170,15 @@ private static String normalizePostalCode(String postalCode) { // Remove space characters in the Zip Code postalCode = postalCode.replaceAll("[\\s]+", ""); - // If the code has more than one part, retain the first part. - postalCode = postalCode.split("-")[0]; + // If the code is just a hyphen throw a descriptive IllegalArgumentException instead of + // ArrayIndexOutOfBoundsException below. + String[] postalCodeArray = postalCode.split("-"); + if (postalCodeArray.length == 0) { + throw new IllegalArgumentException("Invalid postalcode for the passed postalCode: '" + postalCode + "'."); + } + // If the code has more than one part, retain the first part. + postalCode = postalCodeArray[0]; if (postalCode.length() < 2) { throw new IllegalArgumentException("Invalid postalcode format for the passed postalCode:" + postalCode + ". Please check the passed postalcode."); } diff --git a/src/test/java/com/facebook/ads/utils/ServerSideApiUtilTest.java b/src/test/java/com/facebook/ads/utils/ServerSideApiUtilTest.java index abc9a16c..5872e6d0 100644 --- a/src/test/java/com/facebook/ads/utils/ServerSideApiUtilTest.java +++ b/src/test/java/com/facebook/ads/utils/ServerSideApiUtilTest.java @@ -216,4 +216,11 @@ public void NormalizeSanitizesEmptyFieldsToNull() { assertNull(normalize(" \t ", param)); } } -} \ No newline at end of file + + @Test + public void NormalizeHyphenPostalCodeThrowsIllegalArgumentException() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Invalid postalcode for the passed postalCode: '-'."); + normalize("-", ZIP_CODE); + } +}