@@ -1671,79 +1671,69 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::
16711671
16721672template <typename MessageType>
16731673void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTests() {
1674- if (!run_proto3_tests_) {
1675- RunValidJsonTestWithValidator (
1676- " StoresDefaultPrimitive" , REQUIRED,
1677- R"( {
1678- "FieldName13": 0
1679- })" ,
1680- [](const Json::Value& value) { return value.isMember (" FieldName13" ); });
1681- std::vector<const FieldDescriptor*> extensions;
1682- MessageType::GetDescriptor ()->file ()->pool ()->FindAllExtensions (
1683- MessageType::GetDescriptor (), &extensions);
1684- RunValidJsonTestWithValidator (" FieldNameExtension" , RECOMMENDED,
1685- absl::Substitute (R"( {
1686- "[$0]": 1
1687- })" ,
1688- extensions[0 ]->full_name ()),
1689- [&](const Json::Value& value) {
1690- return value.isMember (absl::StrCat (
1691- " [" , extensions[0 ]->full_name (), " ]" ));
1692- });
1693- return ;
1694- }
16951674 RunValidJsonTest (" HelloWorld" , REQUIRED,
16961675 " {\" optionalString\" :\" Hello, World!\" }" ,
16971676 " optional_string: 'Hello, World!'" );
16981677
16991678 // NOTE: The spec for JSON support is still being sorted out, these may not
17001679 // all be correct.
1701-
17021680 RunJsonTestsForFieldNameConvention ();
17031681 RunJsonTestsForNonRepeatedTypes ();
17041682 RunJsonTestsForRepeatedTypes ();
17051683 RunJsonTestsForNullTypes ();
1706- RunJsonTestsForWrapperTypes ();
1707- RunJsonTestsForFieldMask ();
1708- RunJsonTestsForStruct ();
1709- RunJsonTestsForValue ();
1710- RunJsonTestsForAny ();
1684+
1685+ if (run_proto3_tests_) {
1686+ RunJsonTestsForWrapperTypes ();
1687+ RunJsonTestsForFieldMask ();
1688+ RunJsonTestsForStruct ();
1689+ RunJsonTestsForValue ();
1690+ RunJsonTestsForAny ();
1691+ } else {
1692+ // Currently Proto2 only, but should also be run on Proto3-optional.
1693+ RunJsonTestsForStoresDefaultPrimitive ();
1694+ }
1695+
17111696 RunJsonTestsForUnknownEnumStringValues ();
17121697
17131698 RunValidJsonIgnoreUnknownTest (" IgnoreUnknownJsonNumber" , REQUIRED,
1714- R"( {
1715- "unknown": 1
1716- })" ,
1717- " " );
1699+ R"( {"unknown": 1})" , " " );
17181700 RunValidJsonIgnoreUnknownTest (" IgnoreUnknownJsonString" , REQUIRED,
1719- R"( {
1720- "unknown": "a"
1721- })" ,
1722- " " );
1701+ R"( {"unknown": "a"})" , " " );
17231702 RunValidJsonIgnoreUnknownTest (" IgnoreUnknownJsonTrue" , REQUIRED,
1724- R"( {
1725- "unknown": true
1726- })" ,
1727- " " );
1703+ R"( {"unknown": true})" , " " );
17281704 RunValidJsonIgnoreUnknownTest (" IgnoreUnknownJsonFalse" , REQUIRED,
1729- R"( {
1730- "unknown": false
1731- })" ,
1732- " " );
1705+ R"( {"unknown": false})" , " " );
17331706 RunValidJsonIgnoreUnknownTest (" IgnoreUnknownJsonNull" , REQUIRED,
1734- R"( {
1735- "unknown": null
1736- })" ,
1737- " " );
1707+ R"( {"unknown": null})" , " " );
17381708 RunValidJsonIgnoreUnknownTest (" IgnoreUnknownJsonObject" , REQUIRED,
1739- R"( {
1740- "unknown": {"a": 1}
1741- })" ,
1742- " " );
1709+ R"( {"unknown": {"a": 1}})" , " " );
17431710
17441711 ExpectParseFailureForJson (" RejectTopLevelNull" , REQUIRED, " null" );
17451712}
17461713
1714+ template <typename MessageType>
1715+ void BinaryAndJsonConformanceSuiteImpl<
1716+ MessageType>::RunJsonTestsForStoresDefaultPrimitive() {
1717+ RunValidJsonTestWithValidator (
1718+ " StoresDefaultPrimitive" , REQUIRED,
1719+ R"( {
1720+ "FieldName13": 0
1721+ })" ,
1722+ [](const Json::Value& value) { return value.isMember (" FieldName13" ); });
1723+ std::vector<const FieldDescriptor*> extensions;
1724+ MessageType::GetDescriptor ()->file ()->pool ()->FindAllExtensions (
1725+ MessageType::GetDescriptor (), &extensions);
1726+ RunValidJsonTestWithValidator (" FieldNameExtension" , RECOMMENDED,
1727+ absl::Substitute (R"( {
1728+ "[$0]": 1
1729+ })" ,
1730+ extensions[0 ]->full_name ()),
1731+ [&](const Json::Value& value) {
1732+ return value.isMember (absl::StrCat (
1733+ " [" , extensions[0 ]->full_name (), " ]" ));
1734+ });
1735+ }
1736+
17471737template <typename MessageType>
17481738void BinaryAndJsonConformanceSuiteImpl<
17491739 MessageType>::RunJsonTestsForUnknownEnumStringValues() {
@@ -2009,12 +1999,14 @@ void BinaryAndJsonConformanceSuiteImpl<
20091999 value.isMember (" fieldName15" ) && value.isMember (" fieldName16" ) &&
20102000 value.isMember (" fieldName17" ) && value.isMember (" FieldName18" );
20112001 });
2012- RunValidJsonTestWithValidator (
2013- " SkipsDefaultPrimitive" , REQUIRED,
2014- R"( {
2015- "FieldName13": 0
2016- })" ,
2017- [](const Json::Value& value) { return !value.isMember (" FieldName13" ); });
2002+
2003+ if (run_proto3_tests_) {
2004+ RunValidJsonTestWithValidator (" SkipsDefaultPrimitive" , REQUIRED,
2005+ R"( {"FieldName13": 0})" ,
2006+ [](const Json::Value& value) {
2007+ return !value.isMember (" FieldName13" );
2008+ });
2009+ }
20182010}
20192011
20202012template <typename MessageType>
@@ -2271,19 +2263,23 @@ void BinaryAndJsonConformanceSuiteImpl<
22712263 // Enum fields.
22722264 RunValidJsonTest (" EnumField" , REQUIRED, R"( {"optionalNestedEnum": "FOO"})" ,
22732265 " optional_nested_enum: FOO" );
2266+
22742267 // Enum fields with alias
2275- RunValidJsonTest (" EnumFieldWithAlias" , REQUIRED,
2276- R"( {"optionalAliasedEnum": "ALIAS_BAZ"})" ,
2277- " optional_aliased_enum: ALIAS_BAZ" );
2278- RunValidJsonTest (" EnumFieldWithAliasUseAlias" , REQUIRED,
2279- R"( {"optionalAliasedEnum": "MOO"})" ,
2280- " optional_aliased_enum: ALIAS_BAZ" );
2281- RunValidJsonTest (" EnumFieldWithAliasLowerCase" , REQUIRED,
2282- R"( {"optionalAliasedEnum": "moo"})" ,
2283- " optional_aliased_enum: ALIAS_BAZ" );
2284- RunValidJsonTest (" EnumFieldWithAliasDifferentCase" , REQUIRED,
2285- R"( {"optionalAliasedEnum": "bAz"})" ,
2286- " optional_aliased_enum: ALIAS_BAZ" );
2268+ if (run_proto3_tests_) {
2269+ RunValidJsonTest (" EnumFieldWithAlias" , REQUIRED,
2270+ R"( {"optionalAliasedEnum": "ALIAS_BAZ"})" ,
2271+ " optional_aliased_enum: ALIAS_BAZ" );
2272+ RunValidJsonTest (" EnumFieldWithAliasUseAlias" , REQUIRED,
2273+ R"( {"optionalAliasedEnum": "MOO"})" ,
2274+ " optional_aliased_enum: ALIAS_BAZ" );
2275+ RunValidJsonTest (" EnumFieldWithAliasLowerCase" , REQUIRED,
2276+ R"( {"optionalAliasedEnum": "moo"})" ,
2277+ " optional_aliased_enum: ALIAS_BAZ" );
2278+ RunValidJsonTest (" EnumFieldWithAliasDifferentCase" , REQUIRED,
2279+ R"( {"optionalAliasedEnum": "bAz"})" ,
2280+ " optional_aliased_enum: ALIAS_BAZ" );
2281+ }
2282+
22872283 // Enum values must be represented as strings.
22882284 ExpectParseFailureForJson (" EnumFieldNotQuoted" , REQUIRED,
22892285 R"( {"optionalNestedEnum": FOO})" );
@@ -2292,13 +2288,16 @@ void BinaryAndJsonConformanceSuiteImpl<
22922288 R"( {"optionalNestedEnum": 0})" , " optional_nested_enum: FOO" );
22932289 RunValidJsonTest (" EnumFieldNumericValueNonZero" , REQUIRED,
22942290 R"( {"optionalNestedEnum": 1})" , " optional_nested_enum: BAR" );
2295- // Unknown enum values are represented as numeric values.
2296- RunValidJsonTestWithValidator (
2297- " EnumFieldUnknownValue" , REQUIRED, R"( {"optionalNestedEnum": 123})" ,
2298- [](const Json::Value& value) {
2299- return value[" optionalNestedEnum" ].type () == Json::intValue &&
2300- value[" optionalNestedEnum" ].asInt () == 123 ;
2301- });
2291+
2292+ if (run_proto3_tests_) {
2293+ // Unknown enum values are represented as numeric values.
2294+ RunValidJsonTestWithValidator (
2295+ " EnumFieldUnknownValue" , REQUIRED, R"( {"optionalNestedEnum": 123})" ,
2296+ [](const Json::Value& value) {
2297+ return value[" optionalNestedEnum" ].type () == Json::intValue &&
2298+ value[" optionalNestedEnum" ].asInt () == 123 ;
2299+ });
2300+ }
23022301
23032302 // String fields.
23042303 RunValidJsonTest (" StringField" , REQUIRED,
@@ -3000,18 +2999,18 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForValue() {
30002999 }
30013000 ]
30023001 )" );
3003- RunValidJsonTestWithValidator (
3004- " NullValueInOtherOneofOldFormat " , RECOMMENDED ,
3005- R"( {"oneofNullValue": "NULL_VALUE"} )" , [](const Json::Value& value) {
3006- return (value.isMember (" oneofNullValue" ) &&
3007- value[" oneofNullValue" ].isNull ());
3008- });
3009- RunValidJsonTestWithValidator (
3010- " NullValueInOtherOneofNewFormat " , RECOMMENDED ,
3011- R"( {"oneofNullValue": null} )" , [](const Json::Value& value) {
3012- return (value.isMember (" oneofNullValue" ) &&
3013- value[" oneofNullValue" ].isNull ());
3014- });
3002+ RunValidJsonTestWithValidator (" NullValueInOtherOneofOldFormat " , RECOMMENDED,
3003+ R"( {"oneofNullValue": "NULL_VALUE"} )" ,
3004+ [](const Json::Value& value) {
3005+ return (value.isMember (" oneofNullValue" ) &&
3006+ value[" oneofNullValue" ].isNull ());
3007+ });
3008+ RunValidJsonTestWithValidator (" NullValueInOtherOneofNewFormat " , RECOMMENDED,
3009+ R"( {"oneofNullValue": null} )" ,
3010+ [](const Json::Value& value) {
3011+ return (value.isMember (" oneofNullValue" ) &&
3012+ value[" oneofNullValue" ].isNull ());
3013+ });
30153014 RunValidJsonTestWithValidator (
30163015 " NullValueInNormalMessage" , RECOMMENDED, R"( {"optionalNullValue": null})" ,
30173016 [](const Json::Value& value) { return value.empty (); });
0 commit comments