Skip to content

Commit

Permalink
Fix issue FasterXML#54
Browse files Browse the repository at this point in the history
  • Loading branch information
niksu7 committed Mar 7, 2017
1 parent 22c1d34 commit 219d597
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ public class ByteAccumulator
protected Segment _firstSegment, _lastSegment;

protected int _segmentBytes;

// used to cache start pointer for nested message's parent
protected int _parentStart = 0;

public ByteAccumulator(ByteAccumulator p, int typedTag,
byte[] prefixBuffer, int prefixOffset)
byte[] prefixBuffer, int prefixOffset, int parentStart)
{
_parent = p;
_typedTag = typedTag;
_prefixBuffer = prefixBuffer;
_prefixOffset = prefixOffset;
this(p, typedTag, prefixBuffer, prefixOffset);
this._parentStart = parentStart;
}

public ByteAccumulator(ByteAccumulator p,
byte[] prefixBuffer, int prefixOffset) {
public ByteAccumulator(ByteAccumulator p, int typedTag,
byte[] prefixBuffer, int prefixOffset)
{
_parent = p;
_typedTag = -1;
_typedTag = typedTag;
_prefixBuffer = prefixBuffer;
_prefixOffset = prefixOffset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1709,8 +1709,8 @@ private final void _startBuffering(int typedTag) throws IOException
_output.write(_currBuffer, start, len);
}
}
_buffered = new ByteAccumulator(_buffered, typedTag, _currBuffer, ptr, _currStart);
_currStart = _currPtr = ptr + 10;
_buffered = new ByteAccumulator(_buffered, typedTag, _currBuffer, ptr);
}

/**
Expand All @@ -1735,8 +1735,8 @@ private final void _startBuffering() throws IOException
}
}

_buffered = new ByteAccumulator(_buffered, -1, _currBuffer, ptr, _currStart);
_currStart = _currPtr = ptr + 5;
_buffered = new ByteAccumulator(_buffered, _currBuffer, ptr);
}

private final void _finishBuffering() throws IOException
Expand All @@ -1745,13 +1745,17 @@ private final void _finishBuffering() throws IOException
final int currLen = _currPtr - start;

ByteAccumulator acc = _buffered;
final ByteAccumulator child = _buffered;
acc = acc.finish(_output, _currBuffer, start, currLen);
_buffered = acc;
if (acc == null) {
_currStart = 0;
_currPtr = 0;
} else {
_currStart = _currPtr;
_currStart = child._parentStart;
_currPtr = child._prefixOffset;
// need to reallocate buffer, otherwise will overwrite
_ensureMore();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.fasterxml.jackson.dataformat.protobuf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;

public class NestedMessages {

private static final String PROTO = //
"message TestObject {\n" + //
"optional string a = 1;\n" + //
"optional TestSub b = 2;\n" + //
"}\n" + //
"message TestSub {;\n" + //
"optional string c = 2;\n" + //
"optional string b = 3;\n" + //
"optional TestSubSub d = 4;\n" + //
"}\n" + //
"message TestSubSub {;\n" + //
"optional string a = 1;\n" + //
"}\n"; //

public static class TestObject {
String a;
TestSub b;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public TestSub getB() {
return b;
}

public void setB(TestSub b) {
this.b = b;
}

// The following annotation to force "d" as first field is needed before
// the fix
// @JsonPropertyOrder(value={"d", "b", "c"})
public static class TestSub {
String b;
String c;
TestSubSub d;

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

public TestSubSub getD() {
return d;
}

public void setD(TestSubSub d) {
this.d = d;
}

public static class TestSubSub {
String a;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}
}
}
}

@Test
public void testProto() throws IOException {

TestObject testClass = new TestObject();
ProtobufMapper om = new ProtobufMapper();
ProtobufSchema s = ProtobufSchemaLoader.std.load(new ByteArrayInputStream(PROTO.getBytes()));
ObjectReader r = om.readerFor(TestObject.class).with(s);
ObjectWriter w = om.writer(s);
ByteArrayOutputStream out = new ByteArrayOutputStream() {
@Override
public synchronized void write(byte[] b, int off, int len) {
super.write(b, off, len);
System.out.println("Off " + off + " len " + len);
}
};
testClass.a = "value";
testClass.b = new TestObject.TestSub();
testClass.b.b = "value-b";
testClass.b.c = "valc";
// if this following row is commented out, test succeeds with old code
testClass.b.d = new TestObject.TestSub.TestSubSub();
testClass.b.d.a = "a-value!";

w.writeValue(out, testClass);
System.out.println("Size: " + out.size());

TestObject res = r.readValue(out.toByteArray());

Assert.assertEquals("value", res.a);
Assert.assertEquals("valc", res.b.c);
Assert.assertEquals("value-b", res.b.b);
}

}

0 comments on commit 219d597

Please sign in to comment.