flatland/clojure-protobuf — Issue #21: Failed to parse output of protobuf-write.
Author: hsjunnesson
Created: 2012-02-24 09:16:59+00:00
State: open

Using leiningen 0.7.0, I've created a stand alone project which this issue is easily reproducible.

`test.proto`:

```
option java_package = "com.test.models";

message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
repeated string likes = 4;
}
```

Then I compile that with `lein protobuf compile test.proto`.

Then I do something like this in a repl

```clojure
(use 'protobuf.core)
(import 'com.test.models.Test)
(import 'com.test.models.Test$Person)
(import 'java.io.FileOutputStream)

(def Person (protodef Test$Person))
(def p (protobuf Person {:id 123 :name "Bob Smith"}))
(protobuf-write (FileOutputStream. "person.data") p)
```

Now if I try and decode that as raw input in the terminal: `echo person.data | protoc --decode_raw`

I get `Failed to parse input.`

Similarly, if I go back to the repl and try this

```clojure
(import 'java.io.BufferedInputStream)
(protobuf-load-stream Person (BufferedInputStream. (FileInputStream. "person.data")))
```

I get an exception

```
While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either than the input has been truncated or that an embedded message misreported its own length.
[Thrown class com.google.protobuf.InvalidProtocolBufferException]

Restarts:
0: [QUIT] Quit to the SLIME top level

Backtrace:
0: com.google.protobuf.InvalidProtocolBufferException.truncatedMessage(Unknown Source)
1: com.google.protobuf.CodedInputStream.refillBuffer(Unknown Source)
2: com.google.protobuf.CodedInputStream.readRawBytes(Unknown Source)
3: com.google.protobuf.CodedInputStream.readBytes(Unknown Source)
4: com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(Unknown Source)
5: com.google.protobuf.AbstractMessage$Builder.mergeFieldFrom(Unknown Source)
6: com.google.protobuf.AbstractMessage$Builder.mergeFrom(Unknown Source)
7: com.google.protobuf.AbstractMessage$Builder.mergeFrom(Unknown Source)
8: com.google.protobuf.DynamicMessage.parseFrom(Unknown Source)
9: protobuf.core.PersistentProtocolBufferMap$Def.parseFrom(Unknown Source)
10: protobuf.core.PersistentProtocolBufferMap.parseFrom(Unknown Source)
11: protobuf.core$protobuf_load_stream.invoke(core.clj:88)
```