From 715154033600da9bb658239196325074389853d3 Mon Sep 17 00:00:00 2001 From: mandlil <138015259+mandlil@users.noreply.github.com> Date: Wed, 12 Nov 2025 11:07:08 +0000 Subject: [PATCH] 000 Short Code incorrectly formatting for E164 and National. --- .../i18n/phonenumbers/PhoneNumberUtil.java | 18 ++++++++++++--- .../phonenumbers/PhoneNumberUtilTest.java | 9 +++++++- .../i18n/phonenumbers/phonenumberutil.js | 22 ++++++++++++++----- .../i18n/phonenumbers/phonenumberutil_test.js | 7 ++++++ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index 6d8004f18..0622d3760 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -1289,10 +1289,22 @@ public class PhoneNumberUtil { public String format(PhoneNumber number, PhoneNumberFormat numberFormat) { if (number.getNationalNumber() == 0) { // Unparseable numbers that kept their raw input just use that. - // This is the only case where a number can be formatted as E164 without a - // leading '+' symbol (but the original number wasn't parseable anyway). String rawInput = number.getRawInput(); - if (rawInput.length() > 0 || !number.hasCountryCode()) { + if (rawInput.length() > 0 && number.hasCountryCode()) { + String rawInputwithoutCountryCode = rawInput; + // The country calling code must be stripped from the rawInput if it starts with '+'. + // This is necessary because the national formatting function will add the country calling + // code by default." + if (rawInput.startsWith("+")) { + int countryCallingCode = String.valueOf(number.getCountryCode()).length(); + rawInputwithoutCountryCode = rawInput.substring(countryCallingCode + 1); + } + if (numberFormat == PhoneNumberFormat.NATIONAL) { + return rawInputwithoutCountryCode; + } else { + return "+" + number.getCountryCode() + rawInputwithoutCountryCode; + } + } else if (rawInput.length() > 0 || !number.hasCountryCode()) { return rawInput; } } diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 2b9345c4c..18a3c81eb 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -108,7 +108,10 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { new PhoneNumber().setCountryCode(1).setNationalNumber(8002530000L); private static final PhoneNumber US_SPOOF = new PhoneNumber().setCountryCode(1).setNationalNumber(0L); - private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT = + private static final PhoneNumber AU_SHORT_CODE_WITH_RAW_INPUT = + new PhoneNumber().setCountryCode(61).setNationalNumber(0L) + .setRawInput("000"); + private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT = new PhoneNumber().setCountryCode(1).setNationalNumber(0L) .setRawInput("000-000-0000"); private static final PhoneNumber UZ_FIXED_LINE = @@ -507,6 +510,10 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals("000-000-0000", phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL)); assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL)); + assertEquals("+61000", phoneUtil.format(AU_SHORT_CODE_WITH_RAW_INPUT, PhoneNumberFormat.E164)); + PhoneNumber number = PhoneNumber.getDefaultInstance(); + assertThat(phoneUtil.format(number, PhoneNumberFormat.NATIONAL)).isEmpty(); + } public void testFormatBSNumber() { diff --git a/javascript/i18n/phonenumbers/phonenumberutil.js b/javascript/i18n/phonenumbers/phonenumberutil.js index 8812d3aa4..0df9b6117 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil.js +++ b/javascript/i18n/phonenumbers/phonenumberutil.js @@ -1781,15 +1781,25 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.hasValidCountryCallingCode_ = i18n.phonenumbers.PhoneNumberUtil.prototype.format = function(number, numberFormat) { - if (number.getNationalNumber() == 0 && number.hasRawInput()) { + if (number.getNationalNumber() == 0 ) { // Unparseable numbers that kept their raw input just use that. - // This is the only case where a number can be formatted as E164 without a - // leading '+' symbol (but the original number wasn't parseable anyway). - // TODO: Consider removing the 'if' above so that unparseable strings - // without raw input format to the empty string instead of "+00" /** @type {string} */ var rawInput = number.getRawInputOrDefault(); - if (rawInput.length > 0) { + if (rawInput.length > 0 && number.hasCountryCode()) { + /** @type {string} */ + let rawInputwithoutCountryCode = rawInput; + //The country calling code must be stripped from the rawInput if it starts with '+'. + //This is necessary because the national formatting function will add the country calling code by default." + if (rawInput.startsWith('+')) { + const countryCallingCode = String.valueOf(number.getCountryCode()).length(); + rawInputwithoutCountryCode = rawInput.substring(countryCallingCode + 1); + } + if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.NATIONAL) { + return rawInputwithoutCountryCode; + } else { + return '+' + number.getCountryCode() + rawInputwithoutCountryCode; + } + } else if (rawInput.length > 0 || !number.hasCountryCode()) { return rawInput; } } diff --git a/javascript/i18n/phonenumbers/phonenumberutil_test.js b/javascript/i18n/phonenumbers/phonenumberutil_test.js index 4070dd08a..a26bea618 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil_test.js +++ b/javascript/i18n/phonenumbers/phonenumberutil_test.js @@ -225,6 +225,11 @@ US_SPOOF_WITH_RAW_INPUT.setCountryCode(1); US_SPOOF_WITH_RAW_INPUT.setNationalNumber(0); US_SPOOF_WITH_RAW_INPUT.setRawInput('000-000-0000'); +/** @type {!i18n.phonenumbers.PhoneNumber} */ +var AU_SHORT_CODE_WITH_RAW_INPUT = new i18n.phonenumbers.PhoneNumber(); +AU_SHORT_CODE_WITH_RAW_INPUT.setCountryCode(61); +AU_SHORT_CODE_WITH_RAW_INPUT.setNationalNumber(0); +AU_SHORT_CODE_WITH_RAW_INPUT.setRawInput('000'); /** @type {!i18n.phonenumbers.PhoneNumber} */ var UZ_FIXED_LINE = new i18n.phonenumbers.PhoneNumber(); @@ -674,6 +679,8 @@ function testFormatUSNumber() { assertEquals( '000-000-0000', phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PNF.NATIONAL)); assertEquals('0', phoneUtil.format(US_SPOOF, PNF.NATIONAL)); + assertEquals('+61000', phoneUtil.format(AU_SHORT_CODE_WITH_RAW_INPUT, PNF.E164)); + assertEquals('', phoneUtil.format(new i18n.phonenumbers.PhoneNumber(), PNF.NATIONAL)); } function testFormatBSNumber() {