From 15fa6ec14608790664f214ab53688b68aad23dbd Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 14 Dec 2023 20:51:51 -0800 Subject: [PATCH] Improve handling of failure to deserialize `java.util.regex.Pattern` (#4266) --- .../deser/std/FromStringDeserializer.java | 17 ++++++++++++++--- .../deser/jdk/JDKStringLikeTypeDeserTest.java | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java index 63ad531123..2fccdd0c1e 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java @@ -12,6 +12,7 @@ import java.util.Locale; import java.util.TimeZone; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.util.VersionUtil; @@ -329,10 +330,20 @@ protected Object _deserialize(String value, DeserializationContext ctxt) throws return ctxt.getTypeFactory().constructFromCanonical(value); case STD_CURRENCY: // will throw IAE if unknown: - return Currency.getInstance(value); + try { + return Currency.getInstance(value); + } catch (IllegalArgumentException e) { + // Looks like there is no more information + return ctxt.handleWeirdStringValue(_valueClass, value, + "Unrecognized currency"); + } case STD_PATTERN: - // will throw IAE (or its subclass) if malformed - return Pattern.compile(value); + try { + return Pattern.compile(value); + } catch (PatternSyntaxException e) { + return ctxt.handleWeirdStringValue(_valueClass, value, + "Invalid Pattern, problem: "+e.getDescription()); + } case STD_LOCALE: return _deserializeLocale(value, ctxt); case STD_CHARSET: diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java index 275729663e..f94edb6f86 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java @@ -114,7 +114,15 @@ public void testClassWithParams() throws IOException public void testCurrency() throws IOException { Currency usd = Currency.getInstance("USD"); - assertEquals(usd, new ObjectMapper().readValue(q("USD"), Currency.class)); + assertEquals(usd, MAPPER.readValue(q("USD"), Currency.class)); + + try { + MAPPER.readValue(q("poobah"), Currency.class); + fail("Should not pass!"); + } catch (InvalidFormatException e) { + verifyException(e, "Cannot deserialize value of type `java.util.Currency` from String \"Poobah\""); + verifyException(e, "Unrecognized currency"); + } } public void testFile() throws Exception @@ -199,12 +207,13 @@ public void testPattern() throws IOException assertEquals(exp.pattern(), result.pattern()); // [databind#3598]: should also handle invalid pattern serialization - // somehwat gracefully + // somewhat gracefully try { MAPPER.readValue(q("[abc"), Pattern.class); fail("Should not pass"); } catch (InvalidFormatException e) { - verifyException(e, "not a valid textual representation, problem: Unclosed character class"); + verifyException(e, "Cannot deserialize value of type `java.util.regex.Pattern` from String \"[abc\""); + verifyException(e, "Invalid pattern, problem"); } }