Skip to content

Commit 554fff7

Browse files
committed
Partial fix for #609 for 2.10(.4)
1 parent e24cb74 commit 554fff7

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,8 @@ Scott Leberknight (sleberknight@github)
211211
* Reported, contributed fix for #592: DataFormatMatcher#getMatchedFormatName throws NPE
212212
when no match exists
213213
(2.10.3)
214+
215+
Volkan Yazıcı (vy@github)
216+
* Reported #609: (partial fix) `FilteringGeneratorDelegate` does not handle
217+
`writeString(Reader, int)`
218+
(2.10.4 [partial], 2.11.0 [full fix])

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ JSON library.
1818

1919
#605: Handle case when system property access is restricted
2020
(reported by rhernandez35@github)
21+
#609: (partial fix) `FilteringGeneratorDelegate` does not handle `writeString(Reader, int)`
22+
(reported by Volkan Y)
2123

2224
2.10.3 (03-Mar-2020)
2325

src/main/java/com/fasterxml/jackson/core/filter/FilteringGeneratorDelegate.java

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.io.Reader;
56
import java.math.BigDecimal;
67
import java.math.BigInteger;
78

@@ -486,6 +487,30 @@ public void writeString(SerializableString value) throws IOException
486487
delegate.writeString(value);
487488
}
488489

490+
@Override
491+
public void writeString(Reader reader, int len) throws IOException {
492+
if (_itemFilter == null) {
493+
return;
494+
}
495+
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
496+
TokenFilter state = _filterContext.checkValue(_itemFilter);
497+
if (state == null) {
498+
return;
499+
}
500+
if (state != TokenFilter.INCLUDE_ALL) {
501+
// [core#609]: do need to implement, but with 2.10.x TokenFilter no
502+
// useful method to call so will be mostly unfiltered
503+
/*
504+
if (!state.includeString("")) {
505+
return;
506+
}
507+
*/
508+
}
509+
_checkParentPath();
510+
}
511+
delegate.writeString(reader, len);
512+
}
513+
489514
@Override
490515
public void writeRawUTF8String(byte[] text, int offset, int length) throws IOException
491516
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.io.*;
4+
5+
import com.fasterxml.jackson.core.*;
6+
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
7+
import com.fasterxml.jackson.core.filter.TokenFilter;
8+
import com.fasterxml.jackson.core.util.JsonGeneratorDelegate;
9+
10+
// for [core#609]
11+
public class TokenFilter609Test
12+
extends com.fasterxml.jackson.core.BaseTest
13+
{
14+
static class NullExcludingTokenFilter extends TokenFilter {
15+
private static final NullExcludingTokenFilter INSTANCE =
16+
new NullExcludingTokenFilter();
17+
18+
@Override
19+
public boolean includeNull() {
20+
return false;
21+
}
22+
23+
}
24+
25+
static class StringTruncatingGeneratorDelegate
26+
extends JsonGeneratorDelegate
27+
{
28+
private final int maxStringLength;
29+
30+
private StringTruncatingGeneratorDelegate(
31+
JsonGenerator jsonGenerator,
32+
int maxStringLength) {
33+
super(jsonGenerator);
34+
this.maxStringLength = maxStringLength;
35+
}
36+
37+
@Override
38+
public void writeString(String text) throws IOException {
39+
if (text == null) {
40+
writeNull();
41+
} else if (maxStringLength <= 0 || maxStringLength >= text.length()) {
42+
super.writeString(text);
43+
} else {
44+
// super.writeString(text.substring(0, maxStringLength));
45+
StringReader textReader = new StringReader(text);
46+
super.writeString(textReader, maxStringLength);
47+
}
48+
}
49+
50+
@Override
51+
public void writeFieldName(String name) throws IOException {
52+
if (maxStringLength <= 0 || maxStringLength >= name.length()) {
53+
super.writeFieldName(name);
54+
} else {
55+
String truncatedName = name.substring(0, maxStringLength);
56+
super.writeFieldName(truncatedName);
57+
}
58+
}
59+
60+
}
61+
62+
// for [core#609]: will pass in 2.10 for some cases
63+
@SuppressWarnings("resource")
64+
public void testIssue609() throws Exception
65+
{
66+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
67+
JsonGenerator g = createGenerator(outputStream);
68+
g = new FilteringGeneratorDelegate(
69+
g, NullExcludingTokenFilter.INSTANCE, true, true);
70+
int maxStringLength = 10;
71+
g = new StringTruncatingGeneratorDelegate(
72+
g, maxStringLength);
73+
g.writeStartObject();
74+
g.writeFieldName("message");
75+
g.writeString("1234567890!");
76+
g.writeEndObject();
77+
g.close();
78+
79+
String json = outputStream.toString("US-ASCII");
80+
assertEquals("{\"message\":\"1234567890\"}", json);
81+
}
82+
}

0 commit comments

Comments
 (0)