Skip to content

Commit f234258

Browse files
committed
Fix #62
1 parent 4462f0c commit f234258

File tree

6 files changed

+45
-18
lines changed

6 files changed

+45
-18
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.fasterxml.jackson</groupId>
55
<artifactId>jackson-base</artifactId>
6-
<version>2.9.3</version>
6+
<version>2.9.4-SNAPSHOT</version>
77
</parent>
88
<groupId>com.fasterxml.jackson.dataformat</groupId>
99
<artifactId>jackson-dataformats-text</artifactId>

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Modules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.9.4 (not yet released)
12+
13+
#62: SnakeYAML base64Coder is not OSGI exported
14+
(reported by mbechler@github)
15+
1116
2.9.3 (09-Dec-2017)
1217

1318
#39 (yaml): Binary data not recognized by YAML parser

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
1313
import org.yaml.snakeyaml.emitter.Emitter;
1414
import org.yaml.snakeyaml.events.*;
15-
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
1615
import org.yaml.snakeyaml.nodes.Tag;
1716

1817
import com.fasterxml.jackson.core.*;
@@ -623,7 +622,7 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l
623622
if (offset > 0 || (offset+len) != data.length) {
624623
data = Arrays.copyOfRange(data, offset, offset+len);
625624
}
626-
_writeScalarBinary(data);
625+
_writeScalarBinary(b64variant, data);
627626
}
628627

629628
/*
@@ -797,10 +796,15 @@ protected void _writeScalar(String value, String type, Character style) throws I
797796
_emitter.emit(_scalarEvent(value, style));
798797
}
799798

800-
private void _writeScalarBinary(byte[] data) throws IOException
799+
private void _writeScalarBinary(Base64Variant b64variant,
800+
byte[] data) throws IOException
801801
{
802-
// 29-Nov-2017, tatu: Use SnakeYAML encoder instead of Jackson's
803-
String encoded = Base64Coder.encodeLines(data);
802+
// 15-Dec-2017, tatu: as per [dataformats-text#62], can not use SnakeYAML's internal
803+
// codec. Also: force use of linefeed variant if using default
804+
if (b64variant == Base64Variants.getDefaultVariant()) {
805+
b64variant = Base64Variants.MIME;
806+
}
807+
String encoded = b64variant.encode(data);
804808
_emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
805809
null, null, STYLE_BASE64));
806810
}

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLParser.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import org.yaml.snakeyaml.error.Mark;
99
import org.yaml.snakeyaml.events.*;
10-
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
1110
import org.yaml.snakeyaml.nodes.NodeId;
1211
import org.yaml.snakeyaml.nodes.Tag;
1312
import org.yaml.snakeyaml.parser.ParserImpl;
@@ -480,10 +479,16 @@ protected JsonToken _decodeScalar(ScalarEvent scalar) throws IOException
480479
}
481480
// [dataformats-text#39]: support binary type
482481
if ("binary".equals(typeTag)) {
483-
// 29-Nov-2017, tatu: two choices for base64 codecs (or 3 with Java 8):
484-
// Jackson's own or SnakeYAML. Former is faster, but maybe makes sense
485-
// to use latter for sake of consistency.
486-
_binaryValue = Base64Coder.decodeLines(value);
482+
// 15-Dec-2017, tatu: 2.9.4 uses Jackson's codec because SnakeYAML does
483+
// not export its codec via OSGi (breaking 2.9.3). Note that trailing
484+
// whitespace is ok with core 2.9.4, but not earlier, so we'll trim
485+
// on purpose here
486+
value = value.trim();
487+
try {
488+
_binaryValue = Base64Variants.MIME.decode(value);
489+
} catch (IllegalArgumentException e) {
490+
_reportError(e.getMessage());
491+
}
487492
return JsonToken.VALUE_EMBEDDED_OBJECT;
488493
}
489494
// canonical values by YAML are actually 'y' and 'n'; but plenty more unofficial:

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/BinaryReadTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.yaml.deser;
22

3+
import java.io.IOException;
34
import java.util.Arrays;
45

56
import org.junit.Assert;
@@ -14,16 +15,21 @@ public class BinaryReadTest extends ModuleTestBase
1415

1516
public void testBinaryViaTree() throws Exception
1617
{
17-
final String BASE64 = " R0lGODlhDAAMAIQAAP//9/X\n"
18-
+" 17unp5WZmZgAAAOfn515eXv\n"
19-
+" Pz7Y6OjuDg4J+fn5OTk6enp\n"
20-
+" 56enmleECcgggoBADs=";
18+
final String BASE64 = " R0lGODlhDAAMAIQAAP//9/X1\n"
19+
+" 7unp5WZmZgAAAOfn515eXvPz\n"
20+
+" 7Y6OjuDg4J+fn5OTk6enp56e\n"
21+
+" nmleECcgggoBADs=";
2122
final String DOC = String.format(
2223
"---\n"
2324
+"picture: !!binary |\n"
2425
+"%s\n", BASE64);
25-
26-
final JsonNode bean = MAPPER.readTree(DOC);
26+
27+
JsonNode bean = null;
28+
try {
29+
bean = MAPPER.readTree(DOC);
30+
} catch (IOException e) {
31+
fail("Should have decoded properly, instead got "+e);
32+
}
2733
final JsonNode picture = bean.get("picture");
2834
assertNotNull(picture);
2935

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public void testBasicPOJO() throws Exception
3333
assertEquals("firstName: \"Bob\"", it.next());
3434
assertEquals("gender: \"MALE\"", it.next());
3535
assertEquals("lastName: \"Dabolito\"", it.next());
36-
assertEquals("userImage: !!binary |", it.next());
36+
// 15-Dec-2017, tatu: different linefeed modes, see f.ex:
37+
// https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines
38+
String line = it.next();
39+
String expLine = "userImage: !!binary |";
40+
if (line.endsWith("|-")) {
41+
expLine += "-";
42+
}
43+
assertEquals(expLine, line);
3744
assertEquals("verified: false", it.next());
3845
assertFalse(it.hasNext());
3946
}

0 commit comments

Comments
 (0)