Skip to content

Parsing a protobuf message doesn't respect the message schema #130

Open
@bollmann

Description

@bollmann

Hi,

It seems to me that (at least in a scala setting), parsing protobuf messages doesn't faithfully respect the message schema. Consider the following protobuf parser, together with two data objects Greeting and GreetingFrom, and explicit protobuf schema definitions for both:

val mapper: ProtobufMapper = new ProtobufMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)

case class Greeting(author: String, message: String)
case class GreetingFrom(author: String)

val greetingSchema = ProtobufSchemaLoader.std.parse("message Greeting { required string author = 1; required string message = 2; }")
val greetingFromSchema = ProtobufSchemaLoader.std.parse("message GreetingFrom { required string author = 1; }")

Note how GreetingFrom is basically a shorter variant of Greeting. Note also how we explicitly require all fields to be present in the protobuf schema definitions.

Further suppose, we convert a GreetingFrom message to protobuf as follows:

val greetingFrom = GreetingFrom(author = "Max")
val greetingFromProtobuf = mapper
  .writerFor(classOf[GreetingFromMessage])
  .`with`(greetingFromSchema)
  .writeValueAsBytes(greetingFrom)

Then parsing of the greetingFromProtobuf message succeeds even if we only want to accept messages for the different (and in fact, longer) schema for Greeting:

val parsedMessage = mapper
  .readerFor(classOf[Map[String,Any]])
  .`with`(greetingSchema)
  .readValue(greetingFromProtobuf)

In my opinion, parsing the greetingFromProtobuf message according to the different (!) schema in greetingSchema should fail loudly: greetingFromProtobuf misses the message field which is required by greetingSchema, and thus it violates the protobuf schema definition for message Greeting. Hence it should NOT be successfully parsed

However, the above parsing succeeds without any errors. Why is this? Is this a bug in the jackson-data-format-protobuf library? (I'm using version 2.9.4) Or am I using the parser incorrectly?

If people with more jackson (and protobuf inside jackson) knowledge could shed some light on this, I'd highly appreciate it! :-)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions