From c8ad77475634f77468cd237dd10bcff978a2372a Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 10 Jan 2025 19:58:16 -0800 Subject: [PATCH] Fix JRuby conformance issue by loosening Java's `JSONFormatter` implementation to be more accepting of `DynamicMessage` instances of well-known types that have a descriptor with a different object identity but the same full name. PiperOrigin-RevId: 714314780 --- conformance/failure_list_jruby.txt | 1 - .../java/com/google/protobuf/AbstractMessage.java | 11 +++++++++++ .../java/com/google/protobuf/util/JsonFormat.java | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/conformance/failure_list_jruby.txt b/conformance/failure_list_jruby.txt index 778b9b9e62d5e..b02f9a5ecf429 100644 --- a/conformance/failure_list_jruby.txt +++ b/conformance/failure_list_jruby.txt @@ -144,4 +144,3 @@ Required.Editions_Proto2.ProtobufInput.UnknownOrdering.ProtobufOutput Required.Editions_Proto3.ProtobufInput.UnknownOrdering.ProtobufOutput Required.Proto2.ProtobufInput.UnknownOrdering.ProtobufOutput Required.Proto3.ProtobufInput.UnknownOrdering.ProtobufOutput -Required.*.JsonInput.AnyWithNoType.JsonOutput # Failed to parse input or produce output. \ No newline at end of file diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java index fbf1518759306..891e422ed69f2 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java @@ -119,6 +119,17 @@ public int getSerializedSize() { return memoizedSize; } + /* + * This method will only ever return true if `this` and `other` have the same descriptor instance + * for their type (including a gencode message compared to a `DynamicMessage` constructed using + * the same descriptor instance). + * + * For reasons of backward compatibility, a comparison + * involving `DynamicMessage` that is constructed using semantically the same descriptor which + * was loaded separately (such that the reference identity of the descriptors does not match) will + * always return false even if there is otherwise no skew between the descriptors and the contents + * of the instances. + */ @Override public boolean equals(final Object other) { if (other == this) { diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java index 94d356450b0ba..844c91f9d13f6 100644 --- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java +++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java @@ -862,7 +862,7 @@ public void print(PrinterImpl printer, MessageOrBuilder message) throws IOExcept /** Prints google.protobuf.Any */ private void printAny(MessageOrBuilder message) throws IOException { - if (Any.getDefaultInstance().equals(message)) { + if (message.getDefaultInstanceForType().equals(message)) { generator.print("{}"); return; }