From b399146e3f2cfaa65df473711f43219d12e551f1 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Wed, 8 Nov 2023 01:17:44 +0800 Subject: [PATCH 1/2] validating the date format, add test case, since NumberFormatException extends IllegalArgumentException, it is only necessary to write IllegalArgumentException. --- .../main/java/com/google/gson/GsonBuilder.java | 10 +++++++++- .../gson/internal/bind/util/ISO8601Utils.java | 6 +----- .../functional/DefaultTypeAdaptersTest.java | 17 +++++++++++++++++ 3 files changed, 27 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 536207362f..7ebdad4e30 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -43,6 +43,7 @@ import com.google.gson.stream.JsonWriter; 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; @@ -598,7 +599,14 @@ public GsonBuilder disableHtmlEscaping() { */ @CanIgnoreReturnValue public GsonBuilder setDateFormat(String pattern) { - // TODO(Joel): Make this fail fast if it is an invalid date format + 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; } 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 4f08b039b3..bf7b7c19b2 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 @@ -287,11 +287,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 + '"'); 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 770996f92f..96a30a9455 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; @@ -737,6 +738,22 @@ public void testStringBufferDeserialization() { 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 + public void testSetDateFormatWithValidPattern() { + GsonBuilder builder = new GsonBuilder(); + String validPattern = "yyyy-MM-dd"; + builder.setDateFormat(validPattern); + } + private static class MyClassTypeAdapter extends TypeAdapter> { @Override public void write(JsonWriter out, Class value) throws IOException { From bb8c45dab0d4a5da06d9eaf0f72e78da27857ab1 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Sun, 19 Nov 2023 01:38:38 +0800 Subject: [PATCH 2/2] Apply code formatting with Spotless --- .../google/gson/functional/DefaultTypeAdaptersTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 7475cae440..ccef12229c 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -742,9 +742,11 @@ public void testStringBufferDeserialization() { public void testSetDateFormatWithInvalidPattern() { GsonBuilder builder = new GsonBuilder(); String invalidPattern = "This is a invalid Pattern"; - assertThrows(IllegalArgumentException.class, () -> { - builder.setDateFormat(invalidPattern); - }); + assertThrows( + IllegalArgumentException.class, + () -> { + builder.setDateFormat(invalidPattern); + }); } @Test