Protocol message tag had invalid wire type.

3,060 views
Skip to first unread message

ritesh singh

unread,
Feb 23, 2023, 7:59:28 PM2/23/23
to Protocol Buffers
Hello, 

I am working on creating a Android Studio plugin for proto data store.
Android uses proto-lite, my current approach, i get the .pb file and .proto file and use the same in intellij plugin to deserialise it.

val psiFile = PsiManager.getInstance(project).findFile(protoFile)
val protoFileContent = psiFile!!.text
val fileDescriptorProto: DescriptorProtos.FileDescriptorProto =
DescriptorProtos.FileDescriptorProto.parseFrom(protoFileContent.byteInputStream())
val fileDescriptor = Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, arrayOfNulls(0))
val messageDescriptor = fileDescriptor.messageTypes[0]
val buffer = pbFile.contentsToByteArray()
val messageBuilder: DynamicMessage.Builder = DynamicMessage.newBuilder(messageDescriptor)
messageBuilder.mergeFrom(buffer)
val message = messageBuilder.build()
val text: String = TextFormat.printToString(message)


Above code is throwing error in parsing step, Protocol message tag had invalid wire type.

Thanks in advance.

Adam Cozzette

unread,
Feb 23, 2023, 8:12:05 PM2/23/23
to ritesh singh, Protocol Buffers
I suspect that your code is getting an error because it's trying to parse a .proto file as a serialized protocol buffer. (This won't work since .proto files use a text representation very different from the standard protobuf binary format.) Probably the best way to fix this problem would be to invoke protoc with --descriptor_set_out to parse the proto file and convert it into a serialized FileDescriptorSet. A FileDescriptorSet is just a protocol buffer, so once you have it in that form you can easily parse it like you would parse any other serialized proto.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/7fe3a019-e846-4792-8d8a-1655a2d93975n%40googlegroups.com.

ritesh singh

unread,
Feb 23, 2023, 8:24:10 PM2/23/23
to Protocol Buffers
Thanks Adam. I will give it a shot. I thought of using protoc  compiler, but what if my proto file contains nested messages
message A {
  B b = 1
}

message B {
}


After running protoc on the proto file, it will generate 2 java classes. I won't know which generated class to use against my .pb file, as my plugin is only aware of .pb and .proto file.

Please correct me, if am wrong.

ritesh singh

unread,
Feb 23, 2023, 8:33:25 PM2/23/23
to Protocol Buffers
Also does it matter if .pb was created using proto lite and android end.

I believe the lite version doesn't store fields, so converting it to json using proto-util won't work and i need to make use of reflection.

Adam Cozzette

unread,
Feb 24, 2023, 2:00:54 PM2/24/23
to ritesh singh, Protocol Buffers
A serialized protocol buffer doesn't include any type information, so before you can parse one you have to know in advance which type you're expecting. If you want to be prepared to accept either A or B, then a good solution is to put both types inside a oneof in a parent message, and then just parse the parent message normally.

It doesn't matter which protobuf implementation created the .pb file, since all implementations use the same wire format.

That is true that the Java lite implementation doesn't support reflection and doesn't include descriptors, so it cannot serialize protos to JSON. If you can stick to the binary format then that would be ideal since that format is useable everywhere.

ritesh singh

unread,
Feb 24, 2023, 2:16:44 PM2/24/23
to Protocol Buffers
Thanks Adam, so basically i am writing an Intellij plugin for Android Studio. 
The plugin will just take the .pb file and .proto file, run the protoc compiler, generate FileDescriptorSet for proto and create a DynamicMessage using generated fileDescriptorSet and .pb.

I was wondering if there a way to generate the json schema, as in key-value pair. As we know, lite version doesn't include fileds, is it possible to do it at run-time, as in extract keys in any phase?

ritesh singh

unread,
Feb 24, 2023, 2:19:16 PM2/24/23
to Protocol Buffers

Or if there's a better different approach instead of relying on FileDescriptorSet and DynamicMessage - considering i have access to only .pb (generate by proto-lite) or .proto file.

Adam Cozzette

unread,
Feb 24, 2023, 4:14:14 PM2/24/23
to ritesh singh, Protocol Buffers
I think DynamicMessage is the right approach since you need to work with proto files that are only known at run time. This will also allow you to generate JSON.

ritesh singh

unread,
Feb 24, 2023, 4:52:08 PM2/24/23
to Protocol Buffers
Thanks, any leads on generating json at run time using dynamicMessage. Any existing api util if you can point me to.

Adam Cozzette

unread,
Feb 24, 2023, 5:17:43 PM2/24/23
to ritesh singh, Protocol Buffers
Once you have the DynamicMessage, you can use the JsonFormat API the same way you would use it with ordinary messages.

ritesh singh

unread,
Feb 24, 2023, 5:27:08 PM2/24/23
to Protocol Buffers
Ahh yes, thanks a lot. I was using wrong JsonFormat com.android.tools.idea.protobuf.util.JsonFormat
Reply all
Reply to author
Forward
0 new messages