diff --git a/src/main/java/com/fasterxml/jackson/core/format/DataFormatMatcher.java b/src/main/java/com/fasterxml/jackson/core/format/DataFormatMatcher.java index 73586440f9..38a699b85c 100644 --- a/src/main/java/com/fasterxml/jackson/core/format/DataFormatMatcher.java +++ b/src/main/java/com/fasterxml/jackson/core/format/DataFormatMatcher.java @@ -91,7 +91,7 @@ public MatchStrength getMatchStrength() { * */ public String getMatchedFormatName() { - return _match.getFormatName(); + return hasMatch() ? getMatch().getFormatName() : null; } /* diff --git a/src/test/java/com/fasterxml/jackson/core/format/DataFormatMatcherTest.java b/src/test/java/com/fasterxml/jackson/core/format/DataFormatMatcherTest.java index 948d7803fa..479fd83f24 100644 --- a/src/test/java/com/fasterxml/jackson/core/format/DataFormatMatcherTest.java +++ b/src/test/java/com/fasterxml/jackson/core/format/DataFormatMatcherTest.java @@ -35,4 +35,24 @@ public void testCreatesDataFormatMatcherTwo() throws IOException { verifyException(e, "Illegal start/length"); } } + + public void testGetMatchedFormatNameReturnsNameWhenMatches() { + DataFormatMatcher dataFormatMatcher = new DataFormatMatcher(null, + new byte[2], + 1, + 0, + new JsonFactory(), + MatchStrength.SOLID_MATCH); + assertEquals(JsonFactory.FORMAT_NAME_JSON, dataFormatMatcher.getMatchedFormatName()); + } + + public void testGetMatchedFormatNameReturnsNullWhenNoMatch() { + DataFormatMatcher dataFormatMatcher = new DataFormatMatcher(null, + new byte[2], + 1, + 0, + null, + MatchStrength.NO_MATCH); + assertNull(dataFormatMatcher.getMatchedFormatName()); + } }