Skip to content

Commit e9998bf

Browse files
sleberknightcowtowncoder
authored andcommitted
Fix NPE in DataFormatMatcher#getMatchedFormatName when no match exists (#591)
Change DataFormatMatcher#getMatchedFormatName so it does what the Javadoc says to avoid NPE for non-match; add unit tests to verify.
1 parent dc9c40c commit e9998bf

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/com/fasterxml/jackson/core/format/DataFormatMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public MatchStrength getMatchStrength() {
9191
*</pre>
9292
*/
9393
public String getMatchedFormatName() {
94-
return _match.getFormatName();
94+
return hasMatch() ? getMatch().getFormatName() : null;
9595
}
9696

9797
/*

src/test/java/com/fasterxml/jackson/core/format/DataFormatMatcherTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,24 @@ public void testCreatesDataFormatMatcherTwo() throws IOException {
3535
verifyException(e, "Illegal start/length");
3636
}
3737
}
38+
39+
public void testGetMatchedFormatNameReturnsNameWhenMatches() {
40+
DataFormatMatcher dataFormatMatcher = new DataFormatMatcher(null,
41+
new byte[2],
42+
1,
43+
0,
44+
new JsonFactory(),
45+
MatchStrength.SOLID_MATCH);
46+
assertEquals(JsonFactory.FORMAT_NAME_JSON, dataFormatMatcher.getMatchedFormatName());
47+
}
48+
49+
public void testGetMatchedFormatNameReturnsNullWhenNoMatch() {
50+
DataFormatMatcher dataFormatMatcher = new DataFormatMatcher(null,
51+
new byte[2],
52+
1,
53+
0,
54+
null,
55+
MatchStrength.NO_MATCH);
56+
assertNull(dataFormatMatcher.getMatchedFormatName());
57+
}
3858
}

0 commit comments

Comments
 (0)