Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,12 @@ void BinaryAndJsonConformanceSuiteImpl<
ExpectParseFailureForJson("Uint64FieldNotInteger", REQUIRED,
R"({"optionalUint64": "0.5"})");

// Parser reject boolean values for integer fields.
ExpectParseFailureForJson("Int32FieldTrueValue", REQUIRED,
R"({"optionalInt32": true})");
ExpectParseFailureForJson("Int32FieldFalseValue", REQUIRED,
R"({"optionalInt32": false})");

// Parser reject non-numeric string values.
ExpectParseFailureForJson("Int32FieldStringValuePartiallyNumeric", REQUIRED,
R"({"optionalInt32": "12abc"})");
Expand Down Expand Up @@ -2696,6 +2702,12 @@ void BinaryAndJsonConformanceSuiteImpl<
ExpectParseFailureForJson("FloatFieldStringValuePartiallyNumericUnicode",
REQUIRED, R"({"optionalFloat": "12谷歌34"})");

// Parser reject boolean values for float fields.
ExpectParseFailureForJson("FloatFieldTrueValue", REQUIRED,
R"({"optionalFloat": true})");
ExpectParseFailureForJson("FloatFieldFalseValue", REQUIRED,
R"({"optionalFloat": false})");

// Double fields.
RunValidJsonTest("DoubleFieldMinPositiveValue", REQUIRED,
R"({"optionalDouble": 2.22507e-308})",
Expand Down Expand Up @@ -2761,6 +2773,12 @@ void BinaryAndJsonConformanceSuiteImpl<
ExpectParseFailureForJson("DoubleFieldStringValueNonNumeric", REQUIRED,
R"({"optionalDouble": "abc"})");

// Parser reject boolean values for double fields.
ExpectParseFailureForJson("DoubleFieldTrueValue", REQUIRED,
R"({"optionalDouble": true})");
ExpectParseFailureForJson("DoubleFieldFalseValue", REQUIRED,
R"({"optionalDouble": false})");

// Enum fields.
RunValidJsonTest("EnumField", REQUIRED, R"({"optionalNestedEnum": "FOO"})",
"optional_nested_enum: FOO");
Expand Down Expand Up @@ -2795,6 +2813,12 @@ void BinaryAndJsonConformanceSuiteImpl<
ExpectParseFailureForJson("EnumFieldSingleElementArrayNumericValue", REQUIRED,
R"({"optionalNestedEnum": [2]})");

// Booleans are not allowed for enum fields.
ExpectParseFailureForJson("EnumFieldTrueValue", REQUIRED,
R"({"optionalNestedEnum": true})");
ExpectParseFailureForJson("EnumFieldFalseValue", REQUIRED,
R"({"optionalNestedEnum": false})");

if (run_proto3_tests_) {
// Unknown enum values are represented as numeric values.
RunValidJsonTestWithValidator(
Expand Down
6 changes: 6 additions & 0 deletions conformance/failure_list_python.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Recommended.*.JsonInput.FieldNameDuplicateDifferentCasing1 # Should have failed to parse, but didn't.
Recommended.*.JsonInput.FieldNameDuplicateDifferentCasing2 # Should have failed to parse, but didn't.
Required.*.JsonInput.DoubleFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.DoubleFieldTrueValue # Should have failed to parse, but didn't.
Required.*.JsonInput.EnumFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.EnumFieldTrueValue # Should have failed to parse, but didn't.
Required.*.JsonInput.FloatFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.FloatFieldTrueValue # Should have failed to parse, but didn't.
Required.*.ProtobufInput.BadTag_FieldNumberTooHigh # Should have failed to parse, but didn't.
Required.*.ProtobufInput.BadTag_FieldNumberSlightlyTooHigh # Should have failed to parse, but didn't.
Required.*.ProtobufInput.BadTag_OverlongVarint # Should have failed to parse, but didn't.
Expand Down
6 changes: 6 additions & 0 deletions conformance/failure_list_python_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

Recommended.*.JsonInput.FieldNameDuplicateDifferentCasing1 # Should have failed to parse, but didn't.
Recommended.*.JsonInput.FieldNameDuplicateDifferentCasing2 # Should have failed to parse, but didn't.
Required.*.JsonInput.DoubleFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.DoubleFieldTrueValue # Should have failed to parse, but didn't.
Required.*.JsonInput.EnumFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.EnumFieldTrueValue # Should have failed to parse, but didn't.
Required.*.JsonInput.FloatFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.FloatFieldTrueValue # Should have failed to parse, but didn't.
Required.*.ProtobufInput.BadTag_FieldNumberSlightlyTooHigh # Should have failed to parse, but didn't.
# TODO: Uncomment once conformance tests can express failures that are not expected to be fixed.
# Recommended.Editions_Proto2.ProtobufInput.RejectInvalidUtf8.String.MapKey # Should have failed to parse, but didn't.
Expand Down
6 changes: 6 additions & 0 deletions conformance/failure_list_python_upb.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Recommended.*.JsonInput.FieldNameDuplicateDifferentCasing1 # Should have failed to parse, but didn't.
Recommended.*.JsonInput.FieldNameDuplicateDifferentCasing2 # Should have failed to parse, but didn't.
Required.*.JsonInput.DoubleFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.DoubleFieldTrueValue # Should have failed to parse, but didn't.
Required.*.JsonInput.EnumFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.EnumFieldTrueValue # Should have failed to parse, but didn't.
Required.*.JsonInput.FloatFieldFalseValue # Should have failed to parse, but didn't.
Required.*.JsonInput.FloatFieldTrueValue # Should have failed to parse, but didn't.
# TODO: Uncomment once conformance tests can express failures that are not expected to be fixed.
# Recommended.Editions_Proto2.ProtobufInput.RejectInvalidUtf8.String.MapKey # Should have failed to parse, but didn't.
# Recommended.Editions_Proto2.ProtobufInput.RejectInvalidUtf8.String.MapValue # Should have failed to parse, but didn't.
Expand Down
85 changes: 85 additions & 0 deletions python/google/protobuf/internal/json_format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,91 @@ def testEnumValueIntOverride(self):
{'armor': 1},
)

def testAliasedEnumValuesSerializeToSharedCustomString(self):
for enum_val in (
json_enumval_custom_string_pb2.Armor.ARMOR_SABATON,
json_enumval_custom_string_pb2.Armor.ARMOR_SOLLERET,
):
msg = json_enumval_custom_string_pb2.Knight(armor=enum_val)
json_output = json_format.MessageToJson(msg)
self.assertEqual(json.loads(json_output), {'armor': 'sabaton'})

def testNumericCustomStringOptionSerializesAsString(self):
msg = json_enumval_custom_string_pb2.Knight(
armor=json_enumval_custom_string_pb2.Armor.ARMOR_HACHI_MAI_DO
)
json_output = json_format.MessageToJson(msg)
self.assertEqual(json.loads(json_output), {'armor': '8'})

def testParseRawEnumNameWithCustomOptionOk(self):
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse('{"armor": "ARMOR_GREAT_HELM"}', msg)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_GREAT_HELM
)

def testParseAliasedCustomStringOk(self):
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse('{"armor": "sabaton"}', msg)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_SABATON
)

def testParseAliasedRawEnumNameOk(self):
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse('{"armor": "ARMOR_SOLLERET"}', msg)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_SOLLERET
)

def testParseNumericCustomStringOptionOk(self):
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse('{"armor": "8"}', msg)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_HACHI_MAI_DO
)

def testParseIntegerInputForEnumWithNumericCustomOptionOk(self):
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse('{"armor": 8}', msg)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_HACHI_MAI_DO
)

def testParseCustomStringSingleElementArrayFails(self):
msg = json_enumval_custom_string_pb2.Knight()
with self.assertRaises(json_format.ParseError):
json_format.Parse('{"armor": ["gr8 helm"]}', msg)

def testParseBooleanInputForEnum(self):
# In Python, bool is a subclass of int (int(True) == 1). We document that
# JSON boolean literals like 'true' are accepted for enum fields and coerced
# to integer enum values.
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse('{"armor": true}', msg)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_GREAT_HELM
)

def testParseCustomStringCaseMismatchFails(self):
msg = json_enumval_custom_string_pb2.Knight()
with self.assertRaises(json_format.ParseError):
json_format.Parse('{"armor": "GR8 HELM"}', msg)

def testParseUnknownEnumStringFails(self):
msg = json_enumval_custom_string_pb2.Knight()
with self.assertRaises(json_format.ParseError):
json_format.Parse('{"armor": "UNKNOWN_ARMOR"}', msg)

def testParseUnknownEnumStringWithIgnoreUnknownFieldsOk(self):
msg = json_enumval_custom_string_pb2.Knight()
json_format.Parse(
'{"armor": "UNKNOWN_ARMOR"}', msg, ignore_unknown_fields=True
)
self.assertEqual(
msg.armor, json_enumval_custom_string_pb2.Armor.ARMOR_UNKNOWN
)


if __name__ == '__main__':
unittest.main()
Loading
Loading