Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate from Piper for C++, Java, and Python #7477

Merged
merged 5 commits into from May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 58 additions & 31 deletions conformance/binary_json_conformance_suite.cc
Expand Up @@ -556,24 +556,24 @@ void BinaryAndJsonConformanceSuite::RunValidProtobufTestWithMessage(
equivalent_text_format, is_proto3);
}

// According to proto3 JSON specification, JSON serializers follow more strict
// According to proto JSON specification, JSON serializers follow more strict
// rules than parsers (e.g., a serializer must serialize int32 values as JSON
// numbers while the parser is allowed to accept them as JSON strings). This
// method allows strict checking on a proto3 JSON serializer by inspecting
// method allows strict checking on a proto JSON serializer by inspecting
// the JSON output directly.
void BinaryAndJsonConformanceSuite::RunValidJsonTestWithValidator(
const string& test_name, ConformanceLevel level, const string& input_json,
const Validator& validator) {
TestAllTypesProto3 prototype;
ConformanceRequestSetting setting(
level, conformance::JSON, conformance::JSON,
conformance::JSON_TEST,
prototype, test_name, input_json);
const Validator& validator, bool is_proto3) {
std::unique_ptr<Message> prototype = NewTestMessage(is_proto3);
ConformanceRequestSetting setting(level, conformance::JSON, conformance::JSON,
conformance::JSON_TEST, *prototype,
test_name, input_json);
const ConformanceRequest& request = setting.GetRequest();
ConformanceResponse response;
string effective_test_name =
StrCat(setting.ConformanceLevelToString(level),
".Proto3.JsonInput.", test_name, ".Validator");
is_proto3 ? ".Proto3.JsonInput." : ".Proto2.JsonInput.",
test_name, ".Validator");

RunTest(effective_test_name, request, &response);

Expand Down Expand Up @@ -1800,7 +1800,8 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldNameConvention() {
value.isMember("fieldName2") &&
value.isMember("FieldName3") &&
value.isMember("fieldName4");
});
},
true);
RunValidJsonTestWithValidator(
"FieldNameWithNumbers", REQUIRED,
R"({
Expand All @@ -1810,7 +1811,8 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldNameConvention() {
[](const Json::Value& value) {
return value.isMember("field0name5") &&
value.isMember("field0Name6");
});
},
true);
RunValidJsonTestWithValidator(
"FieldNameWithMixedCases", REQUIRED,
R"({
Expand All @@ -1828,7 +1830,8 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldNameConvention() {
value.isMember("FieldName10") &&
value.isMember("FIELDNAME11") &&
value.isMember("FIELDName12");
});
},
true);
RunValidJsonTestWithValidator(
"FieldNameWithDoubleUnderscores", RECOMMENDED,
R"({
Expand All @@ -1846,7 +1849,22 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldNameConvention() {
value.isMember("fieldName16") &&
value.isMember("fieldName17") &&
value.isMember("FieldName18");
});
},
true);
RunValidJsonTestWithValidator(
"StoresDefaultPrimitive", REQUIRED,
R"({
"FieldName13": 0
})",
[](const Json::Value& value) { return value.isMember("FieldName13"); },
false);
RunValidJsonTestWithValidator(
"SkipsDefaultPrimitive", REQUIRED,
R"({
"FieldName13": 0
})",
[](const Json::Value& value) { return !value.isMember("FieldName13"); },
true);
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForNonRepeatedTypes() {
Expand Down Expand Up @@ -1995,19 +2013,19 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForNonRepeatedTypes() {

// 64-bit values are serialized as strings.
RunValidJsonTestWithValidator(
"Int64FieldBeString", RECOMMENDED,
R"({"optionalInt64": 1})",
"Int64FieldBeString", RECOMMENDED, R"({"optionalInt64": 1})",
[](const Json::Value& value) {
return value["optionalInt64"].type() == Json::stringValue &&
value["optionalInt64"].asString() == "1";
});
},
true);
RunValidJsonTestWithValidator(
"Uint64FieldBeString", RECOMMENDED,
R"({"optionalUint64": 1})",
"Uint64FieldBeString", RECOMMENDED, R"({"optionalUint64": 1})",
[](const Json::Value& value) {
return value["optionalUint64"].type() == Json::stringValue &&
value["optionalUint64"].asString() == "1";
});
},
true);

// Bool fields.
RunValidJsonTest(
Expand Down Expand Up @@ -2223,12 +2241,12 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForNonRepeatedTypes() {
"optional_nested_enum: BAR");
// Unknown enum values are represented as numeric values.
RunValidJsonTestWithValidator(
"EnumFieldUnknownValue", REQUIRED,
R"({"optionalNestedEnum": 123})",
"EnumFieldUnknownValue", REQUIRED, R"({"optionalNestedEnum": 123})",
[](const Json::Value& value) {
return value["optionalNestedEnum"].type() == Json::intValue &&
value["optionalNestedEnum"].asInt() == 123;
});
},
true);

// String fields.
RunValidJsonTest(
Expand Down Expand Up @@ -2712,25 +2730,29 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForWrapperTypes() {
R"({"optionalDuration": "1.000000000s"})",
[](const Json::Value& value) {
return value["optionalDuration"].asString() == "1s";
});
},
true);
RunValidJsonTestWithValidator(
"DurationHas3FractionalDigits", RECOMMENDED,
R"({"optionalDuration": "1.010000000s"})",
[](const Json::Value& value) {
return value["optionalDuration"].asString() == "1.010s";
});
},
true);
RunValidJsonTestWithValidator(
"DurationHas6FractionalDigits", RECOMMENDED,
R"({"optionalDuration": "1.000010000s"})",
[](const Json::Value& value) {
return value["optionalDuration"].asString() == "1.000010s";
});
},
true);
RunValidJsonTestWithValidator(
"DurationHas9FractionalDigits", RECOMMENDED,
R"({"optionalDuration": "1.000000010s"})",
[](const Json::Value& value) {
return value["optionalDuration"].asString() == "1.000000010s";
});
},
true);

// Timestamp
RunValidJsonTest(
Expand Down Expand Up @@ -2794,34 +2816,39 @@ void BinaryAndJsonConformanceSuite::RunJsonTestsForWrapperTypes() {
R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
[](const Json::Value& value) {
return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
});
},
true);
RunValidJsonTestWithValidator(
"TimestampHasZeroFractionalDigit", RECOMMENDED,
R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
[](const Json::Value& value) {
return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
});
},
true);
RunValidJsonTestWithValidator(
"TimestampHas3FractionalDigits", RECOMMENDED,
R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
[](const Json::Value& value) {
return value["optionalTimestamp"].asString() ==
"1970-01-01T00:00:00.010Z";
});
},
true);
RunValidJsonTestWithValidator(
"TimestampHas6FractionalDigits", RECOMMENDED,
R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
[](const Json::Value& value) {
return value["optionalTimestamp"].asString() ==
"1970-01-01T00:00:00.000010Z";
});
},
true);
RunValidJsonTestWithValidator(
"TimestampHas9FractionalDigits", RECOMMENDED,
R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
[](const Json::Value& value) {
return value["optionalTimestamp"].asString() ==
"1970-01-01T00:00:00.000000010Z";
});
},
true);
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldMask() {
Expand Down
3 changes: 2 additions & 1 deletion conformance/binary_json_conformance_suite.h
Expand Up @@ -95,7 +95,8 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
void RunValidJsonTestWithValidator(const std::string& test_name,
ConformanceLevel level,
const std::string& input_json,
const Validator& validator);
const Validator& validator,
bool is_proto3);
void ExpectParseFailureForJson(const std::string& test_name,
ConformanceLevel level,
const std::string& input_json);
Expand Down
1 change: 1 addition & 0 deletions conformance/failure_list_java.txt
Expand Up @@ -45,3 +45,4 @@ Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValu
Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE
Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE
Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE
Required.Proto2.JsonInput.StoresDefaultPrimitive.Validator
Expand Up @@ -25,7 +25,7 @@ public static partial class UnittestProto3OptionalReflection {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"Ci5nb29nbGUvcHJvdG9idWYvdW5pdHRlc3RfcHJvdG8zX29wdGlvbmFsLnBy",
"b3RvEhFwcm90b2J1Zl91bml0dGVzdCKBCgoSVGVzdFByb3RvM09wdGlvbmFs",
"b3RvEhFwcm90b2J1Zl91bml0dGVzdCKxCgoSVGVzdFByb3RvM09wdGlvbmFs",
"EhsKDm9wdGlvbmFsX2ludDMyGAEgASgFSACIAQESGwoOb3B0aW9uYWxfaW50",
"NjQYAiABKANIAYgBARIcCg9vcHRpb25hbF91aW50MzIYAyABKA1IAogBARIc",
"Cg9vcHRpb25hbF91aW50NjQYBCABKARIA4gBARIcCg9vcHRpb25hbF9zaW50",
Expand All @@ -42,24 +42,25 @@ public static partial class UnittestProto3OptionalReflection {
"dGVkX21lc3NhZ2UYEyABKAsyMy5wcm90b2J1Zl91bml0dGVzdC5UZXN0UHJv",
"dG8zT3B0aW9uYWwuTmVzdGVkTWVzc2FnZUICKAFIEYgBARJTChRvcHRpb25h",
"bF9uZXN0ZWRfZW51bRgVIAEoDjIwLnByb3RvYnVmX3VuaXR0ZXN0LlRlc3RQ",
"cm90bzNPcHRpb25hbC5OZXN0ZWRFbnVtSBKIAQEaJwoNTmVzdGVkTWVzc2Fn",
"ZRIPCgJiYhgBIAEoBUgAiAEBQgUKA19iYiJKCgpOZXN0ZWRFbnVtEg8KC1VO",
"U1BFQ0lGSUVEEAASBwoDRk9PEAESBwoDQkFSEAISBwoDQkFaEAMSEAoDTkVH",
"EP///////////wFCEQoPX29wdGlvbmFsX2ludDMyQhEKD19vcHRpb25hbF9p",
"bnQ2NEISChBfb3B0aW9uYWxfdWludDMyQhIKEF9vcHRpb25hbF91aW50NjRC",
"EgoQX29wdGlvbmFsX3NpbnQzMkISChBfb3B0aW9uYWxfc2ludDY0QhMKEV9v",
"cHRpb25hbF9maXhlZ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QX29wdGlvbmFsX3N0cmluZ0IRCg9fb3B0aW9uYWxfYnl0ZXNC",
"EAoOX29wdGlvbmFsX2NvcmRCGgoYX29wdGlvbmFsX25lc3RlZF9tZXNzYWdl",
"QhYKFF9sYXp5X25lc3RlZF9tZXNzYWdlQhcKFV9vcHRpb25hbF9uZXN0ZWRf",
"ZW51bUIlCiFjb20uZ29vZ2xlLnByb3RvYnVmLnRlc3RpbmcucHJvdG9QAWIG",
"cHJvdG8z"));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { },
new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {
new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufUnittest.TestProto3Optional), global::ProtobufUnittest.TestProto3Optional.Parser, new[]{ "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalCord", "OptionalNestedMessage", "LazyNestedMessage", "OptionalNestedEnum" }, new[]{ "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalCord", "OptionalNestedMessage", "LazyNestedMessage", "OptionalNestedEnum" }, new[]{ typeof(global::ProtobufUnittest.TestProto3Optional.Types.NestedEnum) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufUnittest.TestProto3Optional.Types.NestedMessage), global::ProtobufUnittest.TestProto3Optional.Types.NestedMessage.Parser, new[]{ "Bb" }, new[]{ "Bb" }, null, null, null)})
new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufUnittest.TestProto3Optional), global::ProtobufUnittest.TestProto3Optional.Parser, new[]{ "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalCord", "OptionalNestedMessage", "LazyNestedMessage", "OptionalNestedEnum", "SingularInt32", "SingularInt64" }, new[]{ "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalCord", "OptionalNestedMessage", "LazyNestedMessage", "OptionalNestedEnum" }, new[]{ typeof(global::ProtobufUnittest.TestProto3Optional.Types.NestedEnum) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufUnittest.TestProto3Optional.Types.NestedMessage), global::ProtobufUnittest.TestProto3Optional.Types.NestedMessage.Parser, new[]{ "Bb" }, new[]{ "Bb" }, null, null, null)})
}));
}
#endregion
Expand Down Expand Up @@ -112,6 +113,8 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
optionalNestedMessage_ = other.optionalNestedMessage_ != null ? other.optionalNestedMessage_.Clone() : null;
lazyNestedMessage_ = other.lazyNestedMessage_ != null ? other.lazyNestedMessage_.Clone() : null;
optionalNestedEnum_ = other.optionalNestedEnum_;
singularInt32_ = other.singularInt32_;
singularInt64_ = other.singularInt64_;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
}

Expand Down Expand Up @@ -516,6 +519,31 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
_hasBits0 &= ~8192;
}

/// <summary>Field number for the "singular_int32" field.</summary>
public const int SingularInt32FieldNumber = 22;
private int singularInt32_;
/// <summary>
/// Add some non-optional fields to verify we can mix them.
/// </summary>
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public int SingularInt32 {
get { return singularInt32_; }
set {
singularInt32_ = value;
}
}

/// <summary>Field number for the "singular_int64" field.</summary>
public const int SingularInt64FieldNumber = 23;
private long singularInt64_;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public long SingularInt64 {
get { return singularInt64_; }
set {
singularInt64_ = value;
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override bool Equals(object other) {
return Equals(other as TestProto3Optional);
Expand Down Expand Up @@ -548,6 +576,8 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
if (!object.Equals(OptionalNestedMessage, other.OptionalNestedMessage)) return false;
if (!object.Equals(LazyNestedMessage, other.LazyNestedMessage)) return false;
if (OptionalNestedEnum != other.OptionalNestedEnum) return false;
if (SingularInt32 != other.SingularInt32) return false;
if (SingularInt64 != other.SingularInt64) return false;
return Equals(_unknownFields, other._unknownFields);
}

Expand All @@ -573,6 +603,8 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
if (optionalNestedMessage_ != null) hash ^= OptionalNestedMessage.GetHashCode();
if (lazyNestedMessage_ != null) hash ^= LazyNestedMessage.GetHashCode();
if (HasOptionalNestedEnum) hash ^= OptionalNestedEnum.GetHashCode();
if (SingularInt32 != 0) hash ^= SingularInt32.GetHashCode();
if (SingularInt64 != 0L) hash ^= SingularInt64.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
Expand Down Expand Up @@ -662,6 +694,14 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
output.WriteRawTag(168, 1);
output.WriteEnum((int) OptionalNestedEnum);
}
if (SingularInt32 != 0) {
output.WriteRawTag(176, 1);
output.WriteInt32(SingularInt32);
}
if (SingularInt64 != 0L) {
output.WriteRawTag(184, 1);
output.WriteInt64(SingularInt64);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
Expand Down Expand Up @@ -727,6 +767,12 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
if (HasOptionalNestedEnum) {
size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) OptionalNestedEnum);
}
if (SingularInt32 != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32Size(SingularInt32);
}
if (SingularInt64 != 0L) {
size += 2 + pb::CodedOutputStream.ComputeInt64Size(SingularInt64);
}
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
Expand Down Expand Up @@ -801,6 +847,12 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
if (other.HasOptionalNestedEnum) {
OptionalNestedEnum = other.OptionalNestedEnum;
}
if (other.SingularInt32 != 0) {
SingularInt32 = other.SingularInt32;
}
if (other.SingularInt64 != 0L) {
SingularInt64 = other.SingularInt64;
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
}

Expand Down Expand Up @@ -899,6 +951,14 @@ public sealed partial class TestProto3Optional : pb::IMessage<TestProto3Optional
OptionalNestedEnum = (global::ProtobufUnittest.TestProto3Optional.Types.NestedEnum) input.ReadEnum();
break;
}
case 176: {
SingularInt32 = input.ReadInt32();
break;
}
case 184: {
SingularInt64 = input.ReadInt64();
break;
}
}
}
}
Expand Down
Binary file modified csharp/src/Google.Protobuf.Test/testprotos.pb
Binary file not shown.