Skip to content

Commit a092470

Browse files
committed
Fixed all test failures related to #2177
1 parent 9e02859 commit a092470

File tree

7 files changed

+59
-42
lines changed

7 files changed

+59
-42
lines changed

src/main/java/com/fasterxml/jackson/databind/ext/jdk8/DoubleStreamSerializer.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private DoubleStreamSerializer() {
2626
}
2727

2828
@Override
29-
public void serialize(DoubleStream stream, JsonGenerator g, SerializerProvider provider)
29+
public void serialize(DoubleStream stream, JsonGenerator g, SerializerProvider ctxt)
3030
throws JacksonException
3131
{
3232
try (final DoubleStream ds = stream) {
@@ -35,6 +35,10 @@ public void serialize(DoubleStream stream, JsonGenerator g, SerializerProvider p
3535
g.writeNumber(value);
3636
});
3737
g.writeEndArray();
38-
}
38+
} catch (Exception e) {
39+
// For most regular serializers we won't both handling but streams are typically
40+
// root values so
41+
wrapAndThrow(ctxt, e, stream, g.getOutputContext().getCurrentIndex());
42+
}
3943
}
4044
}

src/main/java/com/fasterxml/jackson/databind/ext/jdk8/IntStreamSerializer.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ private IntStreamSerializer() {
2828
}
2929

3030
@Override
31-
public void serialize(IntStream stream, JsonGenerator g, SerializerProvider provider)
31+
public void serialize(IntStream stream, JsonGenerator g, SerializerProvider ctxt)
3232
throws JacksonException
3333
{
3434
try (IntStream is = stream) {
3535
g.writeStartArray(is);
3636
is.forEach(value -> {
3737
g.writeNumber(value);
3838
});
39-
4039
g.writeEndArray();
40+
} catch (Exception e) {
41+
// For most regular serializers we won't both handling but streams are typically
42+
// root values so
43+
wrapAndThrow(ctxt, e, stream, g.getOutputContext().getCurrentIndex());
4144
}
4245
}
4346
}

src/main/java/com/fasterxml/jackson/databind/ext/jdk8/Jdk8StreamSerializer.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,24 @@ public JsonSerializer<?> createContextual(SerializerProvider provider, BeanPrope
6060
}
6161

6262
@Override
63-
public void serialize(Stream<?> stream, JsonGenerator g, SerializerProvider provider)
64-
throws JacksonException
63+
public void serialize(Stream<?> stream, JsonGenerator g, SerializerProvider ctxt)
64+
throws JacksonException
6565
{
6666
try (Stream<?> s = stream) {
6767
g.writeStartArray(s);
6868

6969
s.forEach(elem -> {
7070
if (elemSerializer == null) {
71-
provider.writeValue(g, elem);
71+
ctxt.writeValue(g, elem);
7272
} else {
73-
elemSerializer.serialize(elem, g, provider);
73+
elemSerializer.serialize(elem, g, ctxt);
7474
}
7575
});
7676
g.writeEndArray();
77+
} catch (Exception e) {
78+
// For most regular serializers we won't both handling but streams are typically
79+
// root values so
80+
wrapAndThrow(ctxt, e, stream, g.getOutputContext().getCurrentIndex());
7781
}
7882
}
7983
}

src/main/java/com/fasterxml/jackson/databind/ext/jdk8/LongStreamSerializer.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private LongStreamSerializer() {
2525
}
2626

2727
@Override
28-
public void serialize(LongStream stream, JsonGenerator g, SerializerProvider provider)
28+
public void serialize(LongStream stream, JsonGenerator g, SerializerProvider ctxt)
2929
throws JacksonException
3030
{
3131
try (LongStream ls = stream) {
@@ -34,6 +34,10 @@ public void serialize(LongStream stream, JsonGenerator g, SerializerProvider pro
3434
g.writeNumber(value);
3535
});
3636
g.writeEndArray();
37+
} catch (Exception e) {
38+
// For most regular serializers we won't both handling but streams are typically
39+
// root values so
40+
wrapAndThrow(ctxt, e, stream, g.getOutputContext().getCurrentIndex());
3741
}
3842
}
3943
}

src/main/java/com/fasterxml/jackson/databind/ser/std/StdSerializer.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.fasterxml.jackson.annotation.JsonInclude;
1010
import com.fasterxml.jackson.core.*;
1111
import com.fasterxml.jackson.core.JsonParser.NumberType;
12+
import com.fasterxml.jackson.core.exc.WrappedIOException;
1213
import com.fasterxml.jackson.databind.*;
1314
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
1415
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
@@ -234,29 +235,30 @@ protected void visitArrayFormat(JsonFormatVisitorWrapper visitor, JavaType typeH
234235
* Rules for wrapping and unwrapping are bit complicated; essentially:
235236
*<ul>
236237
* <li>Errors are to be passed as is (if uncovered via unwrapping)
237-
* <li>"Plain" IOExceptions (ones that are not of type
238-
* {@link JsonMappingException} are to be passed as is
238+
* <li>Wrapped {@code IOException}s are unpeeled
239239
*</ul>
240240
*/
241241
public void wrapAndThrow(SerializerProvider provider,
242242
Throwable t, Object bean, String fieldName)
243243
throws JacksonException
244244
{
245-
/* 05-Mar-2009, tatu: But one nasty edge is when we get
246-
* StackOverflow: usually due to infinite loop. But that
247-
* usually gets hidden within an InvocationTargetException...
248-
*/
245+
// 05-Mar-2009, tatu: But one nasty edge is when we get
246+
// StackOverflow: usually due to infinite loop. But that
247+
// usually gets hidden within an InvocationTargetException...
249248
while (t instanceof InvocationTargetException && t.getCause() != null) {
250249
t = t.getCause();
251250
}
252-
// Errors and "plain" to be passed as is
251+
// 16-Jan-2021, tatu: Let's peel off useless wrapper as well
252+
while (t instanceof WrappedIOException && t.getCause() != null) {
253+
t = t.getCause();
254+
}
255+
// Errors to be passed as is, most others not
253256
ClassUtil.throwIfError(t);
254-
// Ditto for IOExceptions... except for mapping exceptions!
255-
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
256-
if (t instanceof JacksonException) {
257-
throw (JacksonException) t;
258-
} else if (!wrap) {
259-
ClassUtil.throwIfRTE(t);
257+
if (!(t instanceof JacksonException)) {
258+
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
259+
if (!wrap) {
260+
ClassUtil.throwIfRTE(t);
261+
}
260262
}
261263
// Need to add reference information
262264
throw JsonMappingException.wrapWithPath(t, bean, fieldName);
@@ -269,14 +271,16 @@ public void wrapAndThrow(SerializerProvider provider,
269271
while (t instanceof InvocationTargetException && t.getCause() != null) {
270272
t = t.getCause();
271273
}
272-
// Errors are to be passed as is
274+
while (t instanceof WrappedIOException && t.getCause() != null) {
275+
t = t.getCause();
276+
}
277+
// Errors to be passed as is, most others not
273278
ClassUtil.throwIfError(t);
274-
// Ditto for IOExceptions... except for mapping exceptions!
275-
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
276-
if (t instanceof JacksonException) {
277-
throw (JacksonException) t;
278-
} else if (!wrap) {
279-
ClassUtil.throwIfRTE(t);
279+
if (!(t instanceof JacksonException)) {
280+
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
281+
if (!wrap) {
282+
ClassUtil.throwIfRTE(t);
283+
}
280284
}
281285
// Need to add reference information
282286
throw JsonMappingException.wrapWithPath(t, bean, index);

src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionsDuringWriting.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.*;
55

66
import com.fasterxml.jackson.core.*;
7-
import com.fasterxml.jackson.core.exc.WrappedIOException;
7+
88
import com.fasterxml.jackson.databind.*;
99
import com.fasterxml.jackson.databind.module.SimpleModule;
1010
import com.fasterxml.jackson.databind.testutil.BrokenStringWriter;
@@ -88,8 +88,8 @@ public void testExceptionWithSimpleMapper()
8888
BrokenStringWriter sw = new BrokenStringWriter("TEST");
8989
mapper.writeValue(sw, createLongObject());
9090
fail("Should have gotten an exception");
91-
} catch (WrappedIOException e) {
92-
verifyException(e, WrappedIOException.class, "TEST");
91+
} catch (JsonMappingException e) {
92+
verifyException(e, JsonMappingException.class, "TEST");
9393
Throwable root = e.getCause();
9494
assertNotNull(root);
9595

src/test/java/com/fasterxml/jackson/databind/ext/jdk8/StreamTestBase.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fasterxml.jackson.databind.ext.jdk8;
22

3-
import java.io.IOException;
43
import java.util.concurrent.atomic.AtomicBoolean;
54
import java.util.function.Consumer;
65
import java.util.stream.BaseStream;
@@ -12,6 +11,7 @@
1211
import org.junit.Rule;
1312
import org.junit.rules.ExpectedException;
1413

14+
import com.fasterxml.jackson.core.exc.WrappedIOException;
1515
import com.fasterxml.jackson.databind.JsonMappingException;
1616
import com.fasterxml.jackson.databind.ObjectMapper;
1717

@@ -61,22 +61,20 @@ <T, S extends BaseStream<T, S>> void assertClosesOnRuntimeException(String excep
6161
<T, S extends BaseStream<T, S>> void assertClosesOnIoException(String exceptionMessage, Consumer<S> roundTrip,
6262
S baseStream) {
6363
AtomicBoolean closed = new AtomicBoolean();
64-
initExpectedExceptionIoException(exceptionMessage,closed);
64+
this.expectedException.expect(new IsClosedMatcher(closed));
65+
this.expectedException.expect(Is.isA(JsonMappingException.class));
66+
this.expectedException.expectMessage(exceptionMessage);
6567
roundTrip.accept(baseStream.onClose(() -> closed.set(true)));
6668
}
6769

6870
<T, S extends BaseStream<T, S>> void assertClosesOnWrappedIoException(String exceptionMessage,
6971
Consumer<S> roundTrip, S baseStream) {
7072
AtomicBoolean closed = new AtomicBoolean();
71-
final String actualMessage = "Unexpected IOException (of type java.io.IOException): " + exceptionMessage;
72-
initExpectedExceptionIoException(actualMessage,closed);
73-
roundTrip.accept(baseStream.onClose(() -> closed.set(true)));
74-
}
75-
76-
void initExpectedExceptionIoException(final String exceptionMessage, AtomicBoolean closed) {
73+
final String actualMessage = exceptionMessage;
7774
this.expectedException.expect(new IsClosedMatcher(closed));
78-
this.expectedException.expect(Is.isA(IOException.class));
79-
this.expectedException.expectMessage(exceptionMessage);
75+
this.expectedException.expect(Is.isA(JsonMappingException.class));
76+
this.expectedException.expectMessage(actualMessage);
77+
roundTrip.accept(baseStream.onClose(() -> closed.set(true)));
8078
}
8179

8280
void initExpectedException(Class<? extends Throwable> cause, final String exceptionMessage, AtomicBoolean closed) {

0 commit comments

Comments
 (0)