Skip to content

Commit

Permalink
Extend Proto2 JSON test cases to cover more of the preexisting Proto3…
Browse files Browse the repository at this point in the history
… JSON cases.

PiperOrigin-RevId: 595998208
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 5, 2024
1 parent 0555d64 commit 42ecd61
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 88 deletions.
175 changes: 87 additions & 88 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1671,79 +1671,69 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTests() {
if (!run_proto3_tests_) {
RunValidJsonTestWithValidator(
"StoresDefaultPrimitive", REQUIRED,
R"({
"FieldName13": 0
})",
[](const Json::Value& value) { return value.isMember("FieldName13"); });
std::vector<const FieldDescriptor*> extensions;
MessageType::GetDescriptor()->file()->pool()->FindAllExtensions(
MessageType::GetDescriptor(), &extensions);
RunValidJsonTestWithValidator("FieldNameExtension", RECOMMENDED,
absl::Substitute(R"({
"[$0]": 1
})",
extensions[0]->full_name()),
[&](const Json::Value& value) {
return value.isMember(absl::StrCat(
"[", extensions[0]->full_name(), "]"));
});
return;
}
RunValidJsonTest("HelloWorld", REQUIRED,
"{\"optionalString\":\"Hello, World!\"}",
"optional_string: 'Hello, World!'");

// NOTE: The spec for JSON support is still being sorted out, these may not
// all be correct.

RunJsonTestsForFieldNameConvention();
RunJsonTestsForNonRepeatedTypes();
RunJsonTestsForRepeatedTypes();
RunJsonTestsForNullTypes();
RunJsonTestsForWrapperTypes();
RunJsonTestsForFieldMask();
RunJsonTestsForStruct();
RunJsonTestsForValue();
RunJsonTestsForAny();

if (run_proto3_tests_) {
RunJsonTestsForWrapperTypes();
RunJsonTestsForFieldMask();
RunJsonTestsForStruct();
RunJsonTestsForValue();
RunJsonTestsForAny();
} else {
// Currently Proto2 only, but should also be run on Proto3-optional.
RunJsonTestsForStoresDefaultPrimitive();
}

RunJsonTestsForUnknownEnumStringValues();

RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonNumber", REQUIRED,
R"({
"unknown": 1
})",
"");
R"({"unknown": 1})", "");
RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonString", REQUIRED,
R"({
"unknown": "a"
})",
"");
R"({"unknown": "a"})", "");
RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonTrue", REQUIRED,
R"({
"unknown": true
})",
"");
R"({"unknown": true})", "");
RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonFalse", REQUIRED,
R"({
"unknown": false
})",
"");
R"({"unknown": false})", "");
RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonNull", REQUIRED,
R"({
"unknown": null
})",
"");
R"({"unknown": null})", "");
RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonObject", REQUIRED,
R"({
"unknown": {"a": 1}
})",
"");
R"({"unknown": {"a": 1}})", "");

ExpectParseFailureForJson("RejectTopLevelNull", REQUIRED, "null");
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<
MessageType>::RunJsonTestsForStoresDefaultPrimitive() {
RunValidJsonTestWithValidator(
"StoresDefaultPrimitive", REQUIRED,
R"({
"FieldName13": 0
})",
[](const Json::Value& value) { return value.isMember("FieldName13"); });
std::vector<const FieldDescriptor*> extensions;
MessageType::GetDescriptor()->file()->pool()->FindAllExtensions(
MessageType::GetDescriptor(), &extensions);
RunValidJsonTestWithValidator("FieldNameExtension", RECOMMENDED,
absl::Substitute(R"({
"[$0]": 1
})",
extensions[0]->full_name()),
[&](const Json::Value& value) {
return value.isMember(absl::StrCat(
"[", extensions[0]->full_name(), "]"));
});
}

template <typename MessageType>
void BinaryAndJsonConformanceSuiteImpl<
MessageType>::RunJsonTestsForUnknownEnumStringValues() {
Expand Down Expand Up @@ -2009,12 +1999,14 @@ void BinaryAndJsonConformanceSuiteImpl<
value.isMember("fieldName15") && value.isMember("fieldName16") &&
value.isMember("fieldName17") && value.isMember("FieldName18");
});
RunValidJsonTestWithValidator(
"SkipsDefaultPrimitive", REQUIRED,
R"({
"FieldName13": 0
})",
[](const Json::Value& value) { return !value.isMember("FieldName13"); });

if (run_proto3_tests_) {
RunValidJsonTestWithValidator("SkipsDefaultPrimitive", REQUIRED,
R"({"FieldName13": 0})",
[](const Json::Value& value) {
return !value.isMember("FieldName13");
});
}
}

template <typename MessageType>
Expand Down Expand Up @@ -2271,19 +2263,23 @@ void BinaryAndJsonConformanceSuiteImpl<
// Enum fields.
RunValidJsonTest("EnumField", REQUIRED, R"({"optionalNestedEnum": "FOO"})",
"optional_nested_enum: FOO");

// Enum fields with alias
RunValidJsonTest("EnumFieldWithAlias", REQUIRED,
R"({"optionalAliasedEnum": "ALIAS_BAZ"})",
"optional_aliased_enum: ALIAS_BAZ");
RunValidJsonTest("EnumFieldWithAliasUseAlias", REQUIRED,
R"({"optionalAliasedEnum": "MOO"})",
"optional_aliased_enum: ALIAS_BAZ");
RunValidJsonTest("EnumFieldWithAliasLowerCase", REQUIRED,
R"({"optionalAliasedEnum": "moo"})",
"optional_aliased_enum: ALIAS_BAZ");
RunValidJsonTest("EnumFieldWithAliasDifferentCase", REQUIRED,
R"({"optionalAliasedEnum": "bAz"})",
"optional_aliased_enum: ALIAS_BAZ");
if (run_proto3_tests_) {
RunValidJsonTest("EnumFieldWithAlias", REQUIRED,
R"({"optionalAliasedEnum": "ALIAS_BAZ"})",
"optional_aliased_enum: ALIAS_BAZ");
RunValidJsonTest("EnumFieldWithAliasUseAlias", REQUIRED,
R"({"optionalAliasedEnum": "MOO"})",
"optional_aliased_enum: ALIAS_BAZ");
RunValidJsonTest("EnumFieldWithAliasLowerCase", REQUIRED,
R"({"optionalAliasedEnum": "moo"})",
"optional_aliased_enum: ALIAS_BAZ");
RunValidJsonTest("EnumFieldWithAliasDifferentCase", REQUIRED,
R"({"optionalAliasedEnum": "bAz"})",
"optional_aliased_enum: ALIAS_BAZ");
}

// Enum values must be represented as strings.
ExpectParseFailureForJson("EnumFieldNotQuoted", REQUIRED,
R"({"optionalNestedEnum": FOO})");
Expand All @@ -2292,13 +2288,16 @@ void BinaryAndJsonConformanceSuiteImpl<
R"({"optionalNestedEnum": 0})", "optional_nested_enum: FOO");
RunValidJsonTest("EnumFieldNumericValueNonZero", REQUIRED,
R"({"optionalNestedEnum": 1})", "optional_nested_enum: BAR");
// Unknown enum values are represented as numeric values.
RunValidJsonTestWithValidator(
"EnumFieldUnknownValue", REQUIRED, R"({"optionalNestedEnum": 123})",
[](const Json::Value& value) {
return value["optionalNestedEnum"].type() == Json::intValue &&
value["optionalNestedEnum"].asInt() == 123;
});

if (run_proto3_tests_) {
// Unknown enum values are represented as numeric values.
RunValidJsonTestWithValidator(
"EnumFieldUnknownValue", REQUIRED, R"({"optionalNestedEnum": 123})",
[](const Json::Value& value) {
return value["optionalNestedEnum"].type() == Json::intValue &&
value["optionalNestedEnum"].asInt() == 123;
});
}

// String fields.
RunValidJsonTest("StringField", REQUIRED,
Expand Down Expand Up @@ -3000,18 +2999,18 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForValue() {
}
]
)");
RunValidJsonTestWithValidator(
"NullValueInOtherOneofOldFormat", RECOMMENDED,
R"({"oneofNullValue": "NULL_VALUE"})", [](const Json::Value& value) {
return (value.isMember("oneofNullValue") &&
value["oneofNullValue"].isNull());
});
RunValidJsonTestWithValidator(
"NullValueInOtherOneofNewFormat", RECOMMENDED,
R"({"oneofNullValue": null})", [](const Json::Value& value) {
return (value.isMember("oneofNullValue") &&
value["oneofNullValue"].isNull());
});
RunValidJsonTestWithValidator("NullValueInOtherOneofOldFormat", RECOMMENDED,
R"({"oneofNullValue": "NULL_VALUE"})",
[](const Json::Value& value) {
return (value.isMember("oneofNullValue") &&
value["oneofNullValue"].isNull());
});
RunValidJsonTestWithValidator("NullValueInOtherOneofNewFormat", RECOMMENDED,
R"({"oneofNullValue": null})",
[](const Json::Value& value) {
return (value.isMember("oneofNullValue") &&
value["oneofNullValue"].isNull());
});
RunValidJsonTestWithValidator(
"NullValueInNormalMessage", RECOMMENDED, R"({"optionalNullValue": null})",
[](const Json::Value& value) { return value.empty(); });
Expand Down
1 change: 1 addition & 0 deletions conformance/binary_json_conformance_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class BinaryAndJsonConformanceSuiteImpl {
void RunBinaryPerformanceTests();
void RunJsonPerformanceTests();
void RunJsonTests();
void RunJsonTestsForStoresDefaultPrimitive();
void RunJsonTestsForFieldNameConvention();
void RunJsonTestsForNonRepeatedTypes();
void RunJsonTestsForRepeatedTypes();
Expand Down
44 changes: 44 additions & 0 deletions conformance/failure_list_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,47 @@ Recommended.Proto3.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace
Recommended.Editions_Proto3.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace
Recommended.Proto2.JsonInput.FieldNameExtension.Validator
Recommended.Editions_Proto2.JsonInput.FieldNameExtension.Validator
Recommended.Editions_Proto2.JsonInput.BoolFieldDoubleQuotedFalse
Recommended.Editions_Proto2.JsonInput.BoolFieldDoubleQuotedTrue
Recommended.Editions_Proto2.JsonInput.FieldNameDuplicate
Recommended.Editions_Proto2.JsonInput.FieldNameDuplicateDifferentCasing1
Recommended.Editions_Proto2.JsonInput.FieldNameDuplicateDifferentCasing2
Recommended.Editions_Proto2.JsonInput.FieldNameNotQuoted
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.MapFieldValueIsNull
Recommended.Editions_Proto2.JsonInput.RepeatedFieldMessageElementIsNull
Recommended.Editions_Proto2.JsonInput.RepeatedFieldPrimitiveElementIsNull
Recommended.Editions_Proto2.JsonInput.RepeatedFieldTrailingComma
Recommended.Editions_Proto2.JsonInput.RepeatedFieldTrailingCommaWithNewlines
Recommended.Editions_Proto2.JsonInput.RepeatedFieldTrailingCommaWithSpace
Recommended.Editions_Proto2.JsonInput.RepeatedFieldTrailingCommaWithSpaceCommaSpace
Recommended.Editions_Proto2.JsonInput.StringFieldSingleQuoteBoth
Recommended.Editions_Proto2.JsonInput.StringFieldSingleQuoteKey
Recommended.Editions_Proto2.JsonInput.StringFieldSingleQuoteValue
Recommended.Editions_Proto2.JsonInput.StringFieldUppercaseEscapeLetter
Recommended.Editions_Proto2.JsonInput.TrailingCommaInAnObject
Recommended.Editions_Proto2.JsonInput.TrailingCommaInAnObjectWithNewlines
Recommended.Editions_Proto2.JsonInput.TrailingCommaInAnObjectWithSpace
Recommended.Editions_Proto2.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace
Recommended.Proto2.JsonInput.BoolFieldDoubleQuotedFalse
Recommended.Proto2.JsonInput.BoolFieldDoubleQuotedTrue
Recommended.Proto2.JsonInput.FieldNameDuplicate
Recommended.Proto2.JsonInput.FieldNameDuplicateDifferentCasing1
Recommended.Proto2.JsonInput.FieldNameDuplicateDifferentCasing2
Recommended.Proto2.JsonInput.FieldNameNotQuoted
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto2.JsonInput.MapFieldValueIsNull
Recommended.Proto2.JsonInput.RepeatedFieldMessageElementIsNull
Recommended.Proto2.JsonInput.RepeatedFieldPrimitiveElementIsNull
Recommended.Proto2.JsonInput.RepeatedFieldTrailingComma
Recommended.Proto2.JsonInput.RepeatedFieldTrailingCommaWithNewlines
Recommended.Proto2.JsonInput.RepeatedFieldTrailingCommaWithSpace
Recommended.Proto2.JsonInput.RepeatedFieldTrailingCommaWithSpaceCommaSpace
Recommended.Proto2.JsonInput.StringFieldSingleQuoteBoth
Recommended.Proto2.JsonInput.StringFieldSingleQuoteKey
Recommended.Proto2.JsonInput.StringFieldSingleQuoteValue
Recommended.Proto2.JsonInput.StringFieldUppercaseEscapeLetter
Recommended.Proto2.JsonInput.TrailingCommaInAnObject
Recommended.Proto2.JsonInput.TrailingCommaInAnObjectWithNewlines
Recommended.Proto2.JsonInput.TrailingCommaInAnObjectWithSpace
Recommended.Proto2.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace
33 changes: 33 additions & 0 deletions conformance/failure_list_java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,36 @@ Required.Proto3.JsonInput.Int32FieldPlusSign
Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotBool
Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt
Required.Proto3.JsonInput.StringFieldNotAString
Recommended.Proto2.JsonInput.BoolFieldAllCapitalFalse
Recommended.Proto2.JsonInput.BoolFieldAllCapitalTrue
Recommended.Proto2.JsonInput.BoolFieldCamelCaseFalse
Recommended.Proto2.JsonInput.BoolFieldCamelCaseTrue
Recommended.Proto2.JsonInput.BoolFieldDoubleQuotedFalse
Recommended.Proto2.JsonInput.BoolFieldDoubleQuotedTrue
Recommended.Proto2.JsonInput.BoolMapFieldKeyNotQuoted
Recommended.Proto2.JsonInput.DoubleFieldInfinityNotQuoted
Recommended.Proto2.JsonInput.DoubleFieldNanNotQuoted
Recommended.Proto2.JsonInput.DoubleFieldNegativeInfinityNotQuoted
Recommended.Proto2.JsonInput.FieldNameDuplicate
Recommended.Proto2.JsonInput.FieldNameNotQuoted
Recommended.Proto2.JsonInput.FloatFieldInfinityNotQuoted
Recommended.Proto2.JsonInput.FloatFieldNanNotQuoted
Recommended.Proto2.JsonInput.FloatFieldNegativeInfinityNotQuoted
Recommended.Proto2.JsonInput.Int32MapFieldKeyNotQuoted
Recommended.Proto2.JsonInput.Int64MapFieldKeyNotQuoted
Recommended.Proto2.JsonInput.JsonWithComments
Recommended.Proto2.JsonInput.StringFieldSingleQuoteBoth
Recommended.Proto2.JsonInput.StringFieldSingleQuoteKey
Recommended.Proto2.JsonInput.StringFieldSingleQuoteValue
Recommended.Proto2.JsonInput.StringFieldSurrogateInWrongOrder
Recommended.Proto2.JsonInput.StringFieldUnpairedHighSurrogate
Recommended.Proto2.JsonInput.StringFieldUnpairedLowSurrogate
Recommended.Proto2.JsonInput.Uint32MapFieldKeyNotQuoted
Recommended.Proto2.JsonInput.Uint64MapFieldKeyNotQuoted
Required.Proto2.JsonInput.EnumFieldNotQuoted
Required.Proto2.JsonInput.Int32FieldLeadingZero
Required.Proto2.JsonInput.Int32FieldNegativeWithLeadingZero
Required.Proto2.JsonInput.Int32FieldPlusSign
Required.Proto2.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotBool
Required.Proto2.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt
Required.Proto2.JsonInput.StringFieldNotAString
6 changes: 6 additions & 0 deletions conformance/failure_list_python.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.Protobu
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
6 changes: 6 additions & 0 deletions conformance/failure_list_python_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.Protobu
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
6 changes: 6 additions & 0 deletions conformance/failure_list_python_upb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.Protobu
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput

0 comments on commit 42ecd61

Please sign in to comment.