Skip to content

Commit

Permalink
Rename the 'includingDefaultValueWithoutPresenceFields' and 'always_p…
Browse files Browse the repository at this point in the history
…rint_without_presence_fields' to 'alwaysPrintFieldsWithNoPresence' in the Cpp, Py and Java JSON serializers for consistency.

PiperOrigin-RevId: 604292220
  • Loading branch information
protobuf-github-bot authored and zhangskz committed Feb 5, 2024
1 parent bc99ca2 commit fc41e78
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) {
*
* <p>The new Printer clones all other configurations from the current {@link Printer}.
*
* @deprecated Prefer {@link #includingDefaultValueWithoutPresenceFields}
* @deprecated Prefer {@link #alwaysPrintFieldsWithNoPresence}
*/
@Deprecated
public Printer includingDefaultValueFields() {
Expand Down Expand Up @@ -241,10 +241,9 @@ public Printer includingDefaultValueFields(Set<FieldDescriptor> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand All @@ -1761,16 +1759,15 @@ 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}");
}

@Test
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))
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions python/google/protobuf/internal/json_format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": []}'),
Expand All @@ -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(
Expand Down Expand Up @@ -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": []}'),
Expand All @@ -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(
Expand Down
30 changes: 15 additions & 15 deletions python/google/protobuf/json_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -119,15 +119,15 @@ 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)


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,
Expand All @@ -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).
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/json/internal/unparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ absl::Status WriteField(JsonWriter& writer, const Msg<Traits>& 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
Expand Down Expand Up @@ -465,7 +465,7 @@ absl::Status WriteFields(JsonWriter& writer, const Msg<Traits>& 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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/json/internal/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/json/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/json/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/json/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",)"
Expand Down

0 comments on commit fc41e78

Please sign in to comment.