protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.35k stars 15.46k forks source link

Java/C++/Python implementation inconsistent with proto json spec when marshaling empty proto in any proto #17099

Open lfolger opened 3 months ago

lfolger commented 3 months ago

See https://github.com/golang/protobuf/issues/1620 for full context.

The protobuf Go implementation gerates a different output and expects a different input when converting a google.protobuf.Empty proto nested in and google.protobuf.Any proto to and from JSON compared to C++, Java and Python.

According to the discussion on the issue, the Go protobuf implementation seems to be consistent with the spec but inconsistent with other languages.

Copying the relevant parts from the other issue:

(not using quotes here to keep the original formatting): start quote from dsnet:

The documentation for google.protobuf.Any.value says:

[The value field] must be a valid serialized protocol buffer of the above specified type.

Elsewhere, the documentation for google.protobuf.Any says:

If the embedded message type is well-known and has a custom JSON representation, that representation will be embedded adding a field value which holds the custom JSON in addition to the @type field.

Then, the documentation for google.protobuf.Empty says:

The JSON representation for Empty is empty JSON object {}.

Thus, it seems that the current behavior is correct.

end quote

Is our interpretation/understanding of the documentation incorrect? Would it be reasonable to adjust the documentation to reflect the behavior of the C++/Java/Python? Alternatively, could the behavior of C++/Java be adjusted to align with the documentation? I assume this is cost prohibitive and even aligning the Go implementation with C++ and Java might not be easy.

honglooker commented 3 months ago

Maybe worth updating docs, depending on your bandwidth, of course @Logofile

ref: https://github.com/protocolbuffers/protobuf/issues/5390

lfolger commented 3 months ago

Given the inconsistencies and the fact that Java/C++ don't accept the currently documented formatting, would it be reasonable to make all parsers accept both formattings?

C++ seems to fail if I try to feed it {"@type":"type.googleapis.com/google.protobuf.Empty","value":{}}.

puellanivis commented 3 months ago

I think the only rational answer is that all parsers should accept both formats—for historical reasons. The only question is what should we align on the “canonical” preferred form.

orloffv commented 2 months ago

Hi,

Is there any plan to address the inconsistency with how empty proto messages are handled across different languages (C++, Java, Python)? Consistency here is important for reliable cross-language usage.

Thanks!