From fc41e78d7a5ead031f23ba2e1dcf35fdf941751a Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 5 Feb 2024 05:51:51 -0800 Subject: [PATCH] Rename the 'includingDefaultValueWithoutPresenceFields' and 'always_print_without_presence_fields' to 'alwaysPrintFieldsWithNoPresence' in the Cpp, Py and Java JSON serializers for consistency. PiperOrigin-RevId: 604292220 --- .../com/google/protobuf/util/JsonFormat.java | 7 ++--- .../google/protobuf/util/JsonFormatTest.java | 17 +++++------ .../protobuf/internal/json_format_test.py | 8 ++--- python/google/protobuf/json_format.py | 30 +++++++++---------- src/google/protobuf/json/internal/unparser.cc | 4 +-- src/google/protobuf/json/internal/writer.h | 4 +-- src/google/protobuf/json/json.cc | 8 ++--- src/google/protobuf/json/json.h | 4 +-- src/google/protobuf/json/json_test.cc | 2 +- 9 files changed, 40 insertions(+), 44 deletions(-) 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 f8d2e5b999da..929168395557 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 @@ -187,7 +187,7 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) { * *

The new Printer clones all other configurations from the current {@link Printer}. * - * @deprecated Prefer {@link #includingDefaultValueWithoutPresenceFields} + * @deprecated Prefer {@link #alwaysPrintFieldsWithNoPresence} */ @Deprecated public Printer includingDefaultValueFields() { @@ -241,10 +241,9 @@ public Printer includingDefaultValueFields(Set fieldsToAlwaysOu * presence scalars set to their default value). The new Printer clones all other configurations * from the current {@link Printer}. */ - public Printer includingDefaultValueWithoutPresenceFields() { + public Printer alwaysPrintFieldsWithNoPresence() { if (shouldPrintDefaults != ShouldPrintDefaults.ONLY_IF_PRESENT) { - throw new IllegalStateException( - "JsonFormat includingDefaultValueFields has already been set."); + throw new IllegalStateException("Only one of the JsonFormat defaults options can be set."); } return new Printer( registry, diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java index 96a71907befc..287eff9b206a 100644 --- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java @@ -1527,11 +1527,11 @@ public void testDefaultValueOptionsProto3() throws Exception { + " \"repeatedRecursive\": []\n" + "}"; - // includingDefaultValueFields() and includingDefaultValueWithoutPresenceFields() should + // includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence() should // behave identically on the proto3 test message: assertThat(JsonFormat.printer().includingDefaultValueFields().print(message)) .isEqualTo(expectedJsonWithDefaults); - assertThat(JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(message)) + assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message)) .isEqualTo(expectedJsonWithDefaults); } @@ -1742,16 +1742,14 @@ public void testDefaultValueOptionsProto3Oneofs() throws Exception { assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n}"); assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage)) .isEqualTo("{\n}"); - assertThat( - JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage)) + assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage)) .isEqualTo("{\n}"); oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build(); assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n \"oneofInt32\": 42\n}"); assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage)) .isEqualTo("{\n \"oneofInt32\": 42\n}"); - assertThat( - JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage)) + assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage)) .isEqualTo("{\n \"oneofInt32\": 42\n}"); TestOneof.Builder oneofBuilder = TestOneof.newBuilder(); @@ -1761,8 +1759,7 @@ public void testDefaultValueOptionsProto3Oneofs() throws Exception { .isEqualTo("{\n \"oneofNullValue\": null\n}"); assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage)) .isEqualTo("{\n \"oneofNullValue\": null\n}"); - assertThat( - JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage)) + assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage)) .isEqualTo("{\n \"oneofNullValue\": null\n}"); } @@ -1770,7 +1767,7 @@ public void testDefaultValueOptionsProto3Oneofs() throws Exception { public void testIncludingDefaultValueOptionsWithProto2Optional() throws Exception { TestAllTypesProto2 message = TestAllTypesProto2.getDefaultInstance(); assertThat(JsonFormat.printer().print(message)).isEqualTo("{\n}"); - // includingDefaultValueFields() and includingDefaultValueWithoutPresenceFields() + // includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence() // behave differently on a proto2 message: the former includes the proto2 explicit presence // fields and the latter does not. assertThat(JsonFormat.printer().includingDefaultValueFields().print(message)) @@ -1812,7 +1809,7 @@ public void testIncludingDefaultValueOptionsWithProto2Optional() throws Exceptio + " \"optionalAliasedEnum\": \"ALIAS_FOO\",\n" + " \"repeatedRecursive\": []\n" + "}"); - assertThat(JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(message)) + assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message)) .isEqualTo( "{\n" + " \"repeatedInt32\": [],\n" diff --git a/python/google/protobuf/internal/json_format_test.py b/python/google/protobuf/internal/json_format_test.py index 8e94cebc094b..e46622b5ea24 100644 --- a/python/google/protobuf/internal/json_format_test.py +++ b/python/google/protobuf/internal/json_format_test.py @@ -330,7 +330,7 @@ def testProto3Optional_IncludingDefaultValueWithoutPresenceFields(self): self.assertEqual( json.loads( json_format.MessageToJson( - message, including_default_value_without_presence_fields=True + message, always_print_fields_with_no_presence=True ) ), json.loads('{"repeatedInt32": [], "repeatedNestedMessage": []}'), @@ -339,7 +339,7 @@ def testProto3Optional_IncludingDefaultValueWithoutPresenceFields(self): self.assertEqual( json.loads( json_format.MessageToJson( - message, including_default_value_without_presence_fields=True + message, always_print_fields_with_no_presence=True ) ), json.loads( @@ -391,7 +391,7 @@ def testProto2_IncludingDefaultValueWithoutPresenceFields(self): self.assertEqual( json.loads( json_format.MessageToJson( - message, including_default_value_without_presence_fields=True + message, always_print_fields_with_no_presence=True ) ), json.loads('{"repeatedInt32": [], "repeatedNestedMessage": []}'), @@ -400,7 +400,7 @@ def testProto2_IncludingDefaultValueWithoutPresenceFields(self): self.assertEqual( json.loads( json_format.MessageToJson( - message, including_default_value_without_presence_fields=True + message, always_print_fields_with_no_presence=True ) ), json.loads( diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py index 9694f7fd915f..558b6aa587fc 100644 --- a/python/google/protobuf/json_format.py +++ b/python/google/protobuf/json_format.py @@ -80,19 +80,19 @@ def MessageToJson( descriptor_pool=None, float_precision=None, ensure_ascii=True, - including_default_value_without_presence_fields=False, + always_print_fields_with_no_presence=False, ): """Converts protobuf message to JSON format. Args: message: The protocol buffers message instance to serialize. including_default_value_fields: (DEPRECATED: use - including_default_value_without_presence_fields to correctly treats proto2 - and proto3 optional the same). If True, fields without presence (implicit + always_print_fields_with_no_presence which correctly treats proto2 + and proto3 optionals the same). If True, fields without presence (implicit presence scalars, repeated fields, and map fields) and Proto2 optional scalars will always be serialized. Singular message fields, oneof fields and Proto3 optional scalars are not affected by this option. - including_default_value_without_presence_fields: If True, fields without + always_print_fields_with_no_presence: If True, fields without presence (implicit presence scalars, repeated fields, and map fields) will always be serialized. Any field that supports presence is not affected by this option (including singular message fields and oneof fields). @@ -119,7 +119,7 @@ def MessageToJson( use_integers_for_enums, descriptor_pool, float_precision, - including_default_value_without_presence_fields + always_print_fields_with_no_presence ) return printer.ToJsonString(message, indent, sort_keys, ensure_ascii) @@ -127,7 +127,7 @@ def MessageToJson( def MessageToDict( message, including_default_value_fields=False, - including_default_value_without_presence_fields=False, + always_print_fields_with_no_presence=False, preserving_proto_field_name=False, use_integers_for_enums=False, descriptor_pool=None, @@ -140,12 +140,12 @@ def MessageToDict( Args: message: The protocol buffers message instance to serialize. including_default_value_fields: (DEPRECATED: use - including_default_value_without_presence_fields to correctly treats proto2 + always_print_fields_with_no_presence to correctly treats proto2 and proto3 optional the same). If True, fields without presence (implicit presence scalars, repeated fields, and map fields) and Proto2 optional scalars will always be serialized. Singular message fields, oneof fields and Proto3 optional scalars are not affected by this option. - including_default_value_without_presence_fields: If True, fields without + always_print_fields_with_no_presence: If True, fields without presence (implicit presence scalars, repeated fields, and map fields) will always be serialized. Any field that supports presence is not affected by this option (including singular message fields and oneof fields). @@ -166,7 +166,7 @@ def MessageToDict( use_integers_for_enums, descriptor_pool, float_precision, - including_default_value_without_presence_fields, + always_print_fields_with_no_presence, ) # pylint: disable=protected-access return printer._MessageToJsonObject(message) @@ -190,11 +190,11 @@ def __init__( use_integers_for_enums=False, descriptor_pool=None, float_precision=None, - including_default_value_without_presence_fields=False, + always_print_fields_with_no_presence=False, ): self.including_default_value_fields = including_default_value_fields - self.including_default_value_without_presence_fields = ( - including_default_value_without_presence_fields + self.always_print_fields_with_no_presence = ( + always_print_fields_with_no_presence ) self.preserving_proto_field_name = preserving_proto_field_name self.use_integers_for_enums = use_integers_for_enums @@ -257,7 +257,7 @@ def _RegularMessageToJsonObject(self, message, js): # Serialize default value if including_default_value_fields is True. if ( self.including_default_value_fields - or self.including_default_value_without_presence_fields + or self.always_print_fields_with_no_presence ): message_descriptor = message.DESCRIPTOR for field in message_descriptor.fields: @@ -274,10 +274,10 @@ def _RegularMessageToJsonObject(self, message, js): ): continue - # including_default_value_without_presence_fields doesn't apply to + # always_print_fields_with_no_presence doesn't apply to # any field which supports presence. if ( - self.including_default_value_without_presence_fields + self.always_print_fields_with_no_presence and field.has_presence ): continue diff --git a/src/google/protobuf/json/internal/unparser.cc b/src/google/protobuf/json/internal/unparser.cc index 53f4a9aaac17..a7dcf920a2ae 100644 --- a/src/google/protobuf/json/internal/unparser.cc +++ b/src/google/protobuf/json/internal/unparser.cc @@ -435,7 +435,7 @@ absl::Status WriteField(JsonWriter& writer, const Msg& msg, } else if (Traits::GetSize(field, msg) == 0) { // We can only get here if one of the always_print options is true. ABSL_DCHECK(writer.options().always_print_primitive_fields || - writer.options().always_print_without_presence_fields); + writer.options().always_print_fields_with_no_presence); if (Traits::FieldType(field) == FieldDescriptor::TYPE_GROUP) { // We do not yet have full group support, but this is required so that we @@ -465,7 +465,7 @@ absl::Status WriteFields(JsonWriter& writer, const Msg& msg, Traits::FieldType(field) == FieldDescriptor::TYPE_MESSAGE; has |= !is_singular_message && !Traits::IsOneof(field); } - if (writer.options().always_print_without_presence_fields) { + if (writer.options().always_print_fields_with_no_presence) { has |= Traits::IsRepeated(field) || Traits::IsImplicitPresence(field); } diff --git a/src/google/protobuf/json/internal/writer.h b/src/google/protobuf/json/internal/writer.h index ad66f5789f1a..902792fc55c7 100644 --- a/src/google/protobuf/json/internal/writer.h +++ b/src/google/protobuf/json/internal/writer.h @@ -43,14 +43,14 @@ struct WriterOptions { // - Proto2 optional and required scalar fields which are not present (but not // Proto3 optional scalar fields). // Note: This option is deprecated in favor of - // always_print_without_presence_fields which treats proto2 and proto3 + // always_print_fields_with_no_presence which treats proto2 and proto3 // optionals the same and will be removed in an upcoming release. bool always_print_primitive_fields = false; // Whether to always print fields which do not support presence if they would // otherwise be omitted, namely: // - Implicit presence fields set to their 0 value // - Empty lists and maps - bool always_print_without_presence_fields = false; + bool always_print_fields_with_no_presence = false; // Whether to always print enums as ints. By default they are rendered as // strings. bool always_print_enums_as_ints = false; diff --git a/src/google/protobuf/json/json.cc b/src/google/protobuf/json/json.cc index f294bb33ceca..903b4a273ce8 100644 --- a/src/google/protobuf/json/json.cc +++ b/src/google/protobuf/json/json.cc @@ -34,8 +34,8 @@ absl::Status BinaryToJsonStream(google::protobuf::util::TypeResolver* resolver, opts.preserve_proto_field_names = options.preserve_proto_field_names; opts.always_print_enums_as_ints = options.always_print_enums_as_ints; opts.always_print_primitive_fields = options.always_print_primitive_fields; - opts.always_print_without_presence_fields = - options.always_print_without_presence_fields; + opts.always_print_fields_with_no_presence = + options.always_print_fields_with_no_presence; opts.unquote_int64_if_possible = options.unquote_int64_if_possible; // TODO: Drop this setting. @@ -90,8 +90,8 @@ absl::Status MessageToJsonString(const Message& message, std::string* output, opts.preserve_proto_field_names = options.preserve_proto_field_names; opts.always_print_enums_as_ints = options.always_print_enums_as_ints; opts.always_print_primitive_fields = options.always_print_primitive_fields; - opts.always_print_without_presence_fields = - options.always_print_without_presence_fields; + opts.always_print_fields_with_no_presence = + options.always_print_fields_with_no_presence; opts.unquote_int64_if_possible = options.unquote_int64_if_possible; // TODO: Drop this setting. diff --git a/src/google/protobuf/json/json.h b/src/google/protobuf/json/json.h index 0a310d784368..8543fb5fbd7c 100644 --- a/src/google/protobuf/json/json.h +++ b/src/google/protobuf/json/json.h @@ -46,14 +46,14 @@ struct PrintOptions { // - Proto2 optional and required scalar fields which are not present (but not // Proto3 optional scalar fields). // Note: This option is deprecated in favor of - // always_print_without_presence_fields which treats proto2 and proto3 + // always_print_fields_with_no_presence which treats proto2 and proto3 // optionals the same and will be removed in an upcoming release. bool always_print_primitive_fields = false; // Whether to always print fields which do not support presence if they would // otherwise be omitted, namely: // - Implicit presence fields set to their 0 value // - Empty lists and maps - bool always_print_without_presence_fields = false; + bool always_print_fields_with_no_presence = false; // Whether to always print enums as ints. By default they are rendered as // strings. bool always_print_enums_as_ints = false; diff --git a/src/google/protobuf/json/json_test.cc b/src/google/protobuf/json/json_test.cc index 16ac35e25700..bf19dbfa86cf 100644 --- a/src/google/protobuf/json/json_test.cc +++ b/src/google/protobuf/json/json_test.cc @@ -278,7 +278,7 @@ TEST_P(JsonTest, TestAlwaysPrintWithoutPresenceFields) { EXPECT_THAT(ToJson(m), IsOkAndHolds("{}")); PrintOptions options; - options.always_print_without_presence_fields = true; + options.always_print_fields_with_no_presence = true; EXPECT_THAT(ToJson(m, options), IsOkAndHolds(R"({"boolValue":false,)" R"("int32Value":0,)" R"("int64Value":"0",)"