Skip to content

Commit 06e7cab

Browse files
protobuf-github-botzhangskz
authored andcommitted
Breaking change: Remove the deprecated always_print_primitive_fields option from Java, Python and C++ JSON parsers.
The replacement always_print_without_presence_fields should be used instead, which is very similar but has consistent handling of optional fields by not affecting them. PiperOrigin-RevId: 604381178
1 parent b5beba3 commit 06e7cab

File tree

9 files changed

+40
-643
lines changed

9 files changed

+40
-643
lines changed

java/util/src/main/java/com/google/protobuf/util/JsonFormat.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,34 +178,6 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) {
178178
sortingMapKeys);
179179
}
180180

181-
/**
182-
* Creates a new {@link Printer} that will always print fields unless they are a message type or
183-
* in a oneof.
184-
*
185-
* <p>Note that this does print Proto2 Optional but does not print Proto3 Optional fields, as
186-
* the latter is represented using a synthetic oneof.
187-
*
188-
* <p>The new Printer clones all other configurations from the current {@link Printer}.
189-
*
190-
* @deprecated Prefer {@link #alwaysPrintFieldsWithNoPresence}
191-
*/
192-
@Deprecated
193-
public Printer includingDefaultValueFields() {
194-
if (shouldPrintDefaults != ShouldPrintDefaults.ONLY_IF_PRESENT) {
195-
throw new IllegalStateException(
196-
"JsonFormat includingDefaultValueFields has already been set.");
197-
}
198-
return new Printer(
199-
registry,
200-
oldRegistry,
201-
ShouldPrintDefaults.ALWAYS_PRINT_EXCEPT_MESSAGES_AND_ONEOFS,
202-
ImmutableSet.of(),
203-
preservingProtoFieldNames,
204-
omittingInsignificantWhitespace,
205-
printingEnumsAsInts,
206-
sortingMapKeys);
207-
}
208-
209181
/**
210182
* Creates a new {@link Printer} that will also print default-valued fields if their
211183
* FieldDescriptors are found in the supplied set. Empty repeated fields and map fields will be

java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java

Lines changed: 0 additions & 302 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,312 +1527,10 @@ public void testDefaultValueOptionsProto3() throws Exception {
15271527
+ " \"repeatedRecursive\": []\n"
15281528
+ "}";
15291529

1530-
// includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence() should
1531-
// behave identically on the proto3 test message:
1532-
assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
1533-
.isEqualTo(expectedJsonWithDefaults);
15341530
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message))
15351531
.isEqualTo(expectedJsonWithDefaults);
15361532
}
15371533

1538-
@Test
1539-
public void testDefaultValueForSpecificFieldsOptionProto3() throws Exception {
1540-
TestAllTypes message = TestAllTypes.getDefaultInstance();
1541-
Set<FieldDescriptor> fixedFields = new HashSet<>();
1542-
for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1543-
if (fieldDesc.getName().contains("_fixed")) {
1544-
fixedFields.add(fieldDesc);
1545-
}
1546-
}
1547-
1548-
assertThat(JsonFormat.printer().includingDefaultValueFields(fixedFields).print(message))
1549-
.isEqualTo(
1550-
"{\n"
1551-
+ " \"optionalFixed32\": 0,\n"
1552-
+ " \"optionalFixed64\": \"0\",\n"
1553-
+ " \"repeatedFixed32\": [],\n"
1554-
+ " \"repeatedFixed64\": []\n"
1555-
+ "}");
1556-
1557-
TestAllTypes messageNonDefaults =
1558-
message.toBuilder().setOptionalInt64(1234).setOptionalFixed32(3232).build();
1559-
assertThat(
1560-
JsonFormat.printer().includingDefaultValueFields(fixedFields).print(messageNonDefaults))
1561-
.isEqualTo(
1562-
"{\n"
1563-
+ " \"optionalInt64\": \"1234\",\n"
1564-
+ " \"optionalFixed32\": 3232,\n"
1565-
+ " \"optionalFixed64\": \"0\",\n"
1566-
+ " \"repeatedFixed32\": [],\n"
1567-
+ " \"repeatedFixed64\": []\n"
1568-
+ "}");
1569-
}
1570-
1571-
@Test
1572-
public void testDefaultValueForSpecificFieldsProto3_doesntHonorMessageFields() throws Exception {
1573-
TestAllTypes message = TestAllTypes.getDefaultInstance();
1574-
Set<FieldDescriptor> fixedFields =
1575-
ImmutableSet.of(
1576-
TestAllTypes.getDescriptor().findFieldByName("optional_bool"),
1577-
TestAllTypes.getDescriptor().findFieldByName("optional_recursive"));
1578-
1579-
assertThat(JsonFormat.printer().includingDefaultValueFields(fixedFields).print(message))
1580-
.isEqualTo("{\n \"optionalBool\": false\n}");
1581-
}
1582-
1583-
@Test
1584-
public void testRejectChangingDefaultFieldOptionMultipleTimes() throws Exception {
1585-
Set<FieldDescriptor> fixedFields = new HashSet<>();
1586-
for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1587-
if (fieldDesc.getName().contains("_fixed")) {
1588-
fixedFields.add(fieldDesc);
1589-
}
1590-
}
1591-
try {
1592-
JsonFormat.printer().includingDefaultValueFields().includingDefaultValueFields();
1593-
assertWithMessage("IllegalStateException is expected.").fail();
1594-
} catch (IllegalStateException e) {
1595-
// Expected.
1596-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1597-
.that(e.getMessage().contains("includingDefaultValueFields"))
1598-
.isTrue();
1599-
}
1600-
1601-
try {
1602-
JsonFormat.printer().includingDefaultValueFields().includingDefaultValueFields(fixedFields);
1603-
assertWithMessage("IllegalStateException is expected.").fail();
1604-
} catch (IllegalStateException e) {
1605-
// Expected.
1606-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1607-
.that(e.getMessage().contains("includingDefaultValueFields"))
1608-
.isTrue();
1609-
}
1610-
1611-
try {
1612-
JsonFormat.printer().includingDefaultValueFields(fixedFields).includingDefaultValueFields();
1613-
assertWithMessage("IllegalStateException is expected.").fail();
1614-
} catch (IllegalStateException e) {
1615-
// Expected.
1616-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1617-
.that(e.getMessage().contains("includingDefaultValueFields"))
1618-
.isTrue();
1619-
}
1620-
1621-
try {
1622-
JsonFormat.printer()
1623-
.includingDefaultValueFields(fixedFields)
1624-
.includingDefaultValueFields(fixedFields);
1625-
assertWithMessage("IllegalStateException is expected.").fail();
1626-
} catch (IllegalStateException e) {
1627-
// Expected.
1628-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1629-
.that(e.getMessage().contains("includingDefaultValueFields"))
1630-
.isTrue();
1631-
}
1632-
1633-
Set<FieldDescriptor> intFields = new HashSet<>();
1634-
for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
1635-
if (fieldDesc.getName().contains("_int")) {
1636-
intFields.add(fieldDesc);
1637-
}
1638-
}
1639-
1640-
try {
1641-
JsonFormat.printer()
1642-
.includingDefaultValueFields(intFields)
1643-
.includingDefaultValueFields(fixedFields);
1644-
assertWithMessage("IllegalStateException is expected.").fail();
1645-
} catch (IllegalStateException e) {
1646-
// Expected.
1647-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1648-
.that(e.getMessage().contains("includingDefaultValueFields"))
1649-
.isTrue();
1650-
}
1651-
1652-
try {
1653-
JsonFormat.printer().includingDefaultValueFields(null);
1654-
assertWithMessage("IllegalArgumentException is expected.").fail();
1655-
} catch (IllegalArgumentException e) {
1656-
// Expected.
1657-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1658-
.that(e.getMessage().contains("includingDefaultValueFields"))
1659-
.isTrue();
1660-
}
1661-
1662-
try {
1663-
JsonFormat.printer().includingDefaultValueFields(Collections.<FieldDescriptor>emptySet());
1664-
assertWithMessage("IllegalArgumentException is expected.").fail();
1665-
} catch (IllegalArgumentException e) {
1666-
// Expected.
1667-
assertWithMessage("Exception message should mention includingDefaultValueFields.")
1668-
.that(e.getMessage().contains("includingDefaultValueFields"))
1669-
.isTrue();
1670-
}
1671-
}
1672-
1673-
@Test
1674-
public void testDefaultValuesOptionProto3Maps() throws Exception {
1675-
TestMap mapMessage = TestMap.getDefaultInstance();
1676-
assertThat(JsonFormat.printer().print(mapMessage)).isEqualTo("{\n}");
1677-
assertThat(JsonFormat.printer().includingDefaultValueFields().print(mapMessage))
1678-
.isEqualTo(
1679-
"{\n"
1680-
+ " \"int32ToInt32Map\": {\n"
1681-
+ " },\n"
1682-
+ " \"int64ToInt32Map\": {\n"
1683-
+ " },\n"
1684-
+ " \"uint32ToInt32Map\": {\n"
1685-
+ " },\n"
1686-
+ " \"uint64ToInt32Map\": {\n"
1687-
+ " },\n"
1688-
+ " \"sint32ToInt32Map\": {\n"
1689-
+ " },\n"
1690-
+ " \"sint64ToInt32Map\": {\n"
1691-
+ " },\n"
1692-
+ " \"fixed32ToInt32Map\": {\n"
1693-
+ " },\n"
1694-
+ " \"fixed64ToInt32Map\": {\n"
1695-
+ " },\n"
1696-
+ " \"sfixed32ToInt32Map\": {\n"
1697-
+ " },\n"
1698-
+ " \"sfixed64ToInt32Map\": {\n"
1699-
+ " },\n"
1700-
+ " \"boolToInt32Map\": {\n"
1701-
+ " },\n"
1702-
+ " \"stringToInt32Map\": {\n"
1703-
+ " },\n"
1704-
+ " \"int32ToInt64Map\": {\n"
1705-
+ " },\n"
1706-
+ " \"int32ToUint32Map\": {\n"
1707-
+ " },\n"
1708-
+ " \"int32ToUint64Map\": {\n"
1709-
+ " },\n"
1710-
+ " \"int32ToSint32Map\": {\n"
1711-
+ " },\n"
1712-
+ " \"int32ToSint64Map\": {\n"
1713-
+ " },\n"
1714-
+ " \"int32ToFixed32Map\": {\n"
1715-
+ " },\n"
1716-
+ " \"int32ToFixed64Map\": {\n"
1717-
+ " },\n"
1718-
+ " \"int32ToSfixed32Map\": {\n"
1719-
+ " },\n"
1720-
+ " \"int32ToSfixed64Map\": {\n"
1721-
+ " },\n"
1722-
+ " \"int32ToFloatMap\": {\n"
1723-
+ " },\n"
1724-
+ " \"int32ToDoubleMap\": {\n"
1725-
+ " },\n"
1726-
+ " \"int32ToBoolMap\": {\n"
1727-
+ " },\n"
1728-
+ " \"int32ToStringMap\": {\n"
1729-
+ " },\n"
1730-
+ " \"int32ToBytesMap\": {\n"
1731-
+ " },\n"
1732-
+ " \"int32ToMessageMap\": {\n"
1733-
+ " },\n"
1734-
+ " \"int32ToEnumMap\": {\n"
1735-
+ " }\n"
1736-
+ "}");
1737-
}
1738-
1739-
@Test
1740-
public void testDefaultValueOptionsProto3Oneofs() throws Exception {
1741-
TestOneof oneofMessage = TestOneof.getDefaultInstance();
1742-
assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n}");
1743-
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
1744-
.isEqualTo("{\n}");
1745-
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
1746-
.isEqualTo("{\n}");
1747-
1748-
oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
1749-
assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n \"oneofInt32\": 42\n}");
1750-
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
1751-
.isEqualTo("{\n \"oneofInt32\": 42\n}");
1752-
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
1753-
.isEqualTo("{\n \"oneofInt32\": 42\n}");
1754-
1755-
TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
1756-
mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", oneofBuilder);
1757-
oneofMessage = oneofBuilder.build();
1758-
assertThat(JsonFormat.printer().print(oneofMessage))
1759-
.isEqualTo("{\n \"oneofNullValue\": null\n}");
1760-
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
1761-
.isEqualTo("{\n \"oneofNullValue\": null\n}");
1762-
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
1763-
.isEqualTo("{\n \"oneofNullValue\": null\n}");
1764-
}
1765-
1766-
@Test
1767-
public void testIncludingDefaultValueOptionsWithProto2Optional() throws Exception {
1768-
TestAllTypesProto2 message = TestAllTypesProto2.getDefaultInstance();
1769-
assertThat(JsonFormat.printer().print(message)).isEqualTo("{\n}");
1770-
// includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence()
1771-
// behave differently on a proto2 message: the former includes the proto2 explicit presence
1772-
// fields and the latter does not.
1773-
assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
1774-
.isEqualTo(
1775-
"{\n"
1776-
+ " \"optionalInt32\": 0,\n"
1777-
+ " \"optionalInt64\": \"0\",\n"
1778-
+ " \"optionalUint32\": 0,\n"
1779-
+ " \"optionalUint64\": \"0\",\n"
1780-
+ " \"optionalSint32\": 0,\n"
1781-
+ " \"optionalSint64\": \"0\",\n"
1782-
+ " \"optionalFixed32\": 0,\n"
1783-
+ " \"optionalFixed64\": \"0\",\n"
1784-
+ " \"optionalSfixed32\": 0,\n"
1785-
+ " \"optionalSfixed64\": \"0\",\n"
1786-
+ " \"optionalFloat\": 0.0,\n"
1787-
+ " \"optionalDouble\": 0.0,\n"
1788-
+ " \"optionalBool\": false,\n"
1789-
+ " \"optionalString\": \"\",\n"
1790-
+ " \"optionalBytes\": \"\",\n"
1791-
+ " \"optionalNestedEnum\": \"FOO\",\n"
1792-
+ " \"repeatedInt32\": [],\n"
1793-
+ " \"repeatedInt64\": [],\n"
1794-
+ " \"repeatedUint32\": [],\n"
1795-
+ " \"repeatedUint64\": [],\n"
1796-
+ " \"repeatedSint32\": [],\n"
1797-
+ " \"repeatedSint64\": [],\n"
1798-
+ " \"repeatedFixed32\": [],\n"
1799-
+ " \"repeatedFixed64\": [],\n"
1800-
+ " \"repeatedSfixed32\": [],\n"
1801-
+ " \"repeatedSfixed64\": [],\n"
1802-
+ " \"repeatedFloat\": [],\n"
1803-
+ " \"repeatedDouble\": [],\n"
1804-
+ " \"repeatedBool\": [],\n"
1805-
+ " \"repeatedString\": [],\n"
1806-
+ " \"repeatedBytes\": [],\n"
1807-
+ " \"repeatedNestedMessage\": [],\n"
1808-
+ " \"repeatedNestedEnum\": [],\n"
1809-
+ " \"optionalAliasedEnum\": \"ALIAS_FOO\",\n"
1810-
+ " \"repeatedRecursive\": []\n"
1811-
+ "}");
1812-
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message))
1813-
.isEqualTo(
1814-
"{\n"
1815-
+ " \"repeatedInt32\": [],\n"
1816-
+ " \"repeatedInt64\": [],\n"
1817-
+ " \"repeatedUint32\": [],\n"
1818-
+ " \"repeatedUint64\": [],\n"
1819-
+ " \"repeatedSint32\": [],\n"
1820-
+ " \"repeatedSint64\": [],\n"
1821-
+ " \"repeatedFixed32\": [],\n"
1822-
+ " \"repeatedFixed64\": [],\n"
1823-
+ " \"repeatedSfixed32\": [],\n"
1824-
+ " \"repeatedSfixed64\": [],\n"
1825-
+ " \"repeatedFloat\": [],\n"
1826-
+ " \"repeatedDouble\": [],\n"
1827-
+ " \"repeatedBool\": [],\n"
1828-
+ " \"repeatedString\": [],\n"
1829-
+ " \"repeatedBytes\": [],\n"
1830-
+ " \"repeatedNestedMessage\": [],\n"
1831-
+ " \"repeatedNestedEnum\": [],\n"
1832-
+ " \"repeatedRecursive\": []\n"
1833-
+ "}");
1834-
}
1835-
18361534
@Test
18371535
public void testDefaultValueForSpecificFieldsOptionProto2() throws Exception {
18381536
TestAllTypesProto2 message = TestAllTypesProto2.getDefaultInstance();

0 commit comments

Comments
 (0)