From 11281beb848f9dcf74976e25deb68999e8c5dda8 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Mon, 6 Nov 2023 22:58:13 +0800 Subject: [PATCH 1/5] issue 2472 throw exception if it is an invalid date format and since NumberFormatException extends IllegalArgumentException, it is only necessary to write IllegalArgumentException. --- gson/src/main/java/com/google/gson/GsonBuilder.java | 8 +++++++- .../com/google/gson/internal/bind/util/ISO8601Utils.java | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 15e14cb7b6..448477b86a 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -44,6 +44,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; @@ -595,7 +596,12 @@ public GsonBuilder disableHtmlEscaping() { */ @CanIgnoreReturnValue public GsonBuilder setDateFormat(String pattern) { - // TODO(Joel): Make this fail fast if it is an invalid date format + try { + new SimpleDateFormat(pattern); + } catch (IllegalArgumentException e) { + // throw exception if it is an invalid date format + throw new IllegalArgumentException("The date pattern '" + pattern + "' is not valid", e); + } this.datePattern = pattern; return this; } diff --git a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java index 3fd22d7407..2586674868 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java +++ b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java @@ -280,11 +280,7 @@ public static Date parse(String date, ParsePosition pos) throws ParseException { return calendar.getTime(); // If we get a ParseException it'll already have the right message/offset. // Other exception types can convert here. - } catch (IndexOutOfBoundsException e) { - fail = e; - } catch (NumberFormatException e) { - fail = e; - } catch (IllegalArgumentException e) { + } catch (IndexOutOfBoundsException | IllegalArgumentException e) { fail = e; } String input = (date == null) ? null : ('"' + date + '"'); From 04c04d286544b9d4ce0712a43f7752ad92ddf94b Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Mon, 6 Nov 2023 23:51:53 +0800 Subject: [PATCH 2/5] add test --- .../gson/functional/DefaultTypeAdaptersTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index e858062dd1..7b8a391e2b 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -16,6 +16,7 @@ package com.google.gson.functional; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import com.google.gson.Gson; @@ -710,6 +711,20 @@ public void testStringBufferDeserialization() { StringBuffer sb = gson.fromJson("'abc'", StringBuffer.class); assertThat(sb.toString()).isEqualTo("abc"); } + @Test + public void testSetDateFormatWithInvalidPattern() { + GsonBuilder builder = new GsonBuilder(); + String invalidPattern = "This is a invalid Pattern"; + assertThrows(IllegalArgumentException.class, () -> { + builder.setDateFormat(invalidPattern); + }); + } + @Test + void testSetDateFormatWithValidPattern() { + GsonBuilder builder = new GsonBuilder(); + String validPattern = "yyyy-MM-dd"; + builder.setDateFormat(validPattern); + } private static class MyClassTypeAdapter extends TypeAdapter> { @Override From 3bd2e14b14c86960d1ebd58dbbba602085efdfbe Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Tue, 7 Nov 2023 00:05:00 +0800 Subject: [PATCH 3/5] add test --- .../com/google/gson/functional/DefaultTypeAdaptersTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index 7b8a391e2b..f7982c951b 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -720,7 +720,7 @@ public void testSetDateFormatWithInvalidPattern() { }); } @Test - void testSetDateFormatWithValidPattern() { + public void testSetDateFormatWithValidPattern() { GsonBuilder builder = new GsonBuilder(); String validPattern = "yyyy-MM-dd"; builder.setDateFormat(validPattern); From 26e04d01e94558c88cf9749be51cab3d7de88303 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Tue, 7 Nov 2023 00:18:47 +0800 Subject: [PATCH 4/5] add a blank line before each of test methods --- .../java/com/google/gson/functional/DefaultTypeAdaptersTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index f7982c951b..009832f291 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -719,6 +719,7 @@ public void testSetDateFormatWithInvalidPattern() { builder.setDateFormat(invalidPattern); }); } + @Test public void testSetDateFormatWithValidPattern() { GsonBuilder builder = new GsonBuilder(); From 8ac0ffb04a68750f48eee1dc7c00c552aeddc6e5 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Wed, 8 Nov 2023 00:41:25 +0800 Subject: [PATCH 5/5] add if pattern != null --- gson/src/main/java/com/google/gson/GsonBuilder.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 448477b86a..cf5a4c5ca2 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -596,11 +596,13 @@ public GsonBuilder disableHtmlEscaping() { */ @CanIgnoreReturnValue public GsonBuilder setDateFormat(String pattern) { - try { - new SimpleDateFormat(pattern); - } catch (IllegalArgumentException e) { - // throw exception if it is an invalid date format - throw new IllegalArgumentException("The date pattern '" + pattern + "' is not valid", e); + if (pattern != null) { + try { + new SimpleDateFormat(pattern); + } catch (IllegalArgumentException e) { + // Throw exception if it is an invalid date format + throw new IllegalArgumentException("The date pattern '" + pattern + "' is not valid", e); + } } this.datePattern = pattern; return this;