Merge pull request #9496 from protocolbuffers/duplicates
Remove Proto3LiteTest.kt
diff --git a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
index 1a65093..51fa5e0 100644
--- a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
+++ b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
@@ -1,698 +1,705 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2008 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using Google.Protobuf.TestProtos;
-using NUnit.Framework;
-using UnitTest.Issues.TestProtos;
-using Google.Protobuf.WellKnownTypes;
-using Google.Protobuf.Reflection;
-
-using static Google.Protobuf.JsonParserTest; // For WrapInQuotes
-using System.IO;
-using Google.Protobuf.Collections;
-using ProtobufUnittest;
-
-namespace Google.Protobuf
-{
- /// <summary>
- /// Tests for the JSON formatter. Note that in these tests, double quotes are replaced with apostrophes
- /// for the sake of readability (embedding \" everywhere is painful). See the AssertJson method for details.
- /// </summary>
- public class JsonFormatterTest
- {
- [Test]
- public void DefaultValues_WhenOmitted()
- {
- var formatter = JsonFormatter.Default;
-
- AssertJson("{ }", formatter.Format(new ForeignMessage()));
- AssertJson("{ }", formatter.Format(new TestAllTypes()));
- AssertJson("{ }", formatter.Format(new TestMap()));
- }
-
- [Test]
- public void DefaultValues_WhenIncluded()
- {
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
- }
-
- [Test]
- public void EnumAllowAlias()
- {
- var message = new TestEnumAllowAlias
- {
- Value = TestEnumWithDupValue.Foo2,
- };
- var actualText = JsonFormatter.Default.Format(message);
- var expectedText = "{ 'value': 'FOO1' }";
- AssertJson(expectedText, actualText);
- }
-
- [Test]
- public void EnumAsInt()
- {
- var message = new TestAllTypes
- {
- SingleForeignEnum = ForeignEnum.ForeignBar,
- RepeatedForeignEnum = { ForeignEnum.ForeignBaz, (ForeignEnum) 100, ForeignEnum.ForeignFoo }
- };
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatEnumsAsIntegers(true));
- var actualText = formatter.Format(message);
- var expectedText = "{ " +
- "'singleForeignEnum': 5, " +
- "'repeatedForeignEnum': [ 6, 100, 4 ]" +
- " }";
- AssertJson(expectedText, actualText);
- }
-
- [Test]
- public void AllSingleFields()
- {
- var message = new TestAllTypes
- {
- SingleBool = true,
- SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
- SingleDouble = 23.5,
- SingleFixed32 = 23,
- SingleFixed64 = 1234567890123,
- SingleFloat = 12.25f,
- SingleForeignEnum = ForeignEnum.ForeignBar,
- SingleForeignMessage = new ForeignMessage { C = 10 },
- SingleImportEnum = ImportEnum.ImportBaz,
- SingleImportMessage = new ImportMessage { D = 20 },
- SingleInt32 = 100,
- SingleInt64 = 3210987654321,
- SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo,
- SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
- SinglePublicImportMessage = new PublicImportMessage { E = 54 },
- SingleSfixed32 = -123,
- SingleSfixed64 = -12345678901234,
- SingleSint32 = -456,
- SingleSint64 = -12345678901235,
- SingleString = "test\twith\ttabs",
- SingleUint32 = uint.MaxValue,
- SingleUint64 = ulong.MaxValue,
- };
- var actualText = JsonFormatter.Default.Format(message);
-
- // Fields in numeric order
- var expectedText = "{ " +
- "'singleInt32': 100, " +
- "'singleInt64': '3210987654321', " +
- "'singleUint32': 4294967295, " +
- "'singleUint64': '18446744073709551615', " +
- "'singleSint32': -456, " +
- "'singleSint64': '-12345678901235', " +
- "'singleFixed32': 23, " +
- "'singleFixed64': '1234567890123', " +
- "'singleSfixed32': -123, " +
- "'singleSfixed64': '-12345678901234', " +
- "'singleFloat': 12.25, " +
- "'singleDouble': 23.5, " +
- "'singleBool': true, " +
- "'singleString': 'test\\twith\\ttabs', " +
- "'singleBytes': 'AQIDBA==', " +
- "'singleNestedMessage': { 'bb': 35 }, " +
- "'singleForeignMessage': { 'c': 10 }, " +
- "'singleImportMessage': { 'd': 20 }, " +
- "'singleNestedEnum': 'FOO', " +
- "'singleForeignEnum': 'FOREIGN_BAR', " +
- "'singleImportEnum': 'IMPORT_BAZ', " +
- "'singlePublicImportMessage': { 'e': 54 }" +
- " }";
- AssertJson(expectedText, actualText);
- }
-
- [Test]
- public void WithFormatDefaultValues_DoesNotAffectMessageFields()
- {
- var message = new TestAllTypes();
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var json = formatter.Format(message);
- Assert.IsFalse(json.Contains("\"singleNestedMessage\""));
- Assert.IsFalse(json.Contains("\"singleForeignMessage\""));
- Assert.IsFalse(json.Contains("\"singleImportMessage\""));
- }
-
- [Test]
- public void WithFormatDefaultValues_DoesNotAffectProto3OptionalFields()
- {
- var message = new TestProto3Optional();
- message.OptionalInt32 = 0;
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var json = formatter.Format(message);
- // The non-optional proto3 fields are formatted, as is the optional-but-specified field.
- AssertJson("{ 'optionalInt32': 0, 'singularInt32': 0, 'singularInt64': '0' }", json);
- }
-
- [Test]
- public void WithFormatDefaultValues_DoesNotAffectProto2Fields()
- {
- var message = new TestProtos.Proto2.ForeignMessage();
- message.C = 0;
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var json = formatter.Format(message);
- // The specified field is formatted, but the non-specified field (d) is not.
- AssertJson("{ 'c': 0 }", json);
- }
-
- [Test]
- public void WithFormatDefaultValues_DoesNotAffectOneofFields()
- {
- var message = new TestOneof();
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var json = formatter.Format(message);
- AssertJson("{ }", json);
- }
-
- [Test]
- public void RepeatedField()
- {
- AssertJson("{ 'repeatedInt32': [ 1, 2, 3, 4, 5 ] }",
- JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
- }
-
- [Test]
- public void MapField_StringString()
- {
- AssertJson("{ 'mapStringString': { 'with spaces': 'bar', 'a': 'b' } }",
- JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
- }
-
- [Test]
- public void MapField_Int32Int32()
- {
- // The keys are quoted, but the values aren't.
- AssertJson("{ 'mapInt32Int32': { '0': 1, '2': 3 } }",
- JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
- }
-
- [Test]
- public void MapField_BoolBool()
- {
- // The keys are quoted, but the values aren't.
- AssertJson("{ 'mapBoolBool': { 'false': true, 'true': false } }",
- JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
- }
-
- [Test]
- public void NullValueOutsideStruct()
- {
- var message = new NullValueOutsideStruct { NullValue = NullValue.NullValue };
- AssertJson("{ 'nullValue': null }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void NullValueNotInOneof()
- {
- var message = new NullValueNotInOneof();
- AssertJson("{ }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void NullValueNotInOneof_FormatDefaults()
- {
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var message = new NullValueNotInOneof();
- AssertJson("{ 'nullValue': null }", formatter.Format(message));
- }
-
- [TestCase(1.0, "1")]
- [TestCase(double.NaN, "'NaN'")]
- [TestCase(double.PositiveInfinity, "'Infinity'")]
- [TestCase(double.NegativeInfinity, "'-Infinity'")]
- public void DoubleRepresentations(double value, string expectedValueText)
- {
- var message = new TestAllTypes { SingleDouble = value };
- string actualText = JsonFormatter.Default.Format(message);
- string expectedText = "{ 'singleDouble': " + expectedValueText + " }";
- AssertJson(expectedText, actualText);
- }
-
- [Test]
- public void UnknownEnumValueNumeric_SingleField()
- {
- var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
- AssertJson("{ 'singleForeignEnum': 100 }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void UnknownEnumValueNumeric_RepeatedField()
- {
- var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.ForeignBaz, (ForeignEnum) 100, ForeignEnum.ForeignFoo } };
- AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 100, 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void UnknownEnumValueNumeric_MapField()
- {
- var message = new TestMap { MapInt32Enum = { { 1, MapEnum.Foo }, { 2, (MapEnum) 100 }, { 3, MapEnum.Bar } } };
- AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '2': 100, '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void UnknownEnumValue_RepeatedField_AllEntriesUnknown()
- {
- var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
- AssertJson("{ 'repeatedForeignEnum': [ 200, 100 ] }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit
- [TestCase("a\u0601b", "a\\u0601b")] // Ranged
- [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...)
- public void SimpleNonAscii(string text, string encoded)
- {
- var message = new TestAllTypes { SingleString = text };
- AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void SurrogatePairEscaping()
- {
- var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" };
- AssertJson("{ 'singleString': 'a\\ud801\\udc01b' }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void InvalidSurrogatePairsFail()
- {
- // Note: don't use TestCase for these, as the strings can't be reliably represented
- // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/
-
- // Lone low surrogate
- var message = new TestAllTypes { SingleString = "a\uDC01b" };
- Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
-
- // Lone high surrogate
- message = new TestAllTypes { SingleString = "a\uD801b" };
- Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
- }
-
- [Test]
- [TestCase("foo_bar", "fooBar")]
- [TestCase("bananaBanana", "bananaBanana")]
- [TestCase("BANANABanana", "BANANABanana")]
- [TestCase("simple", "simple")]
- [TestCase("ACTION_AND_ADVENTURE", "ACTIONANDADVENTURE")]
- [TestCase("action_and_adventure", "actionAndAdventure")]
- [TestCase("kFoo", "kFoo")]
- [TestCase("HTTPServer", "HTTPServer")]
- [TestCase("CLIENT", "CLIENT")]
- public void ToJsonName(string original, string expected)
- {
- Assert.AreEqual(expected, JsonFormatter.ToJsonName(original));
- }
-
- [Test]
- [TestCase(null, "{ }")]
- [TestCase("x", "{ 'fooString': 'x' }")]
- [TestCase("", "{ 'fooString': '' }")]
- public void Oneof(string fooStringValue, string expectedJson)
- {
- var message = new TestOneof();
- if (fooStringValue != null)
- {
- message.FooString = fooStringValue;
- }
-
- // We should get the same result both with and without "format default values".
- var formatter = JsonFormatter.Default;
- AssertJson(expectedJson, formatter.Format(message));
- formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- AssertJson(expectedJson, formatter.Format(message));
- }
-
- [Test]
- public void WrapperFormatting_Single()
- {
- // Just a few examples, handling both classes and value types, and
- // default vs non-default values
- var message = new TestWellKnownTypes
- {
- Int64Field = 10,
- Int32Field = 0,
- BytesField = ByteString.FromBase64("ABCD"),
- StringField = ""
- };
- var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
- AssertJson(expectedJson, JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void WrapperFormatting_Message()
- {
- Assert.AreEqual("\"\"", JsonFormatter.Default.Format(new StringValue()));
- Assert.AreEqual("0", JsonFormatter.Default.Format(new Int32Value()));
- }
-
- [Test]
- public void WrapperFormatting_FormatDefaultValuesDoesNotFormatNull()
- {
- // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
- var message = new TestWellKnownTypes { Int32Field = 10 };
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var actualJson = formatter.Format(message);
- // This *used* to include "int64Field": null, but that was a bug.
- // WithDefaultValues should not affect message fields, including wrapper types.
- Assert.IsFalse(actualJson.Contains("\"int64Field\": null"));
- Assert.IsTrue(actualJson.Contains("\"int32Field\": 10"));
- }
-
- [Test]
- public void OutputIsInNumericFieldOrder_NoDefaults()
- {
- var formatter = JsonFormatter.Default;
- var message = new TestJsonFieldOrdering { PlainString = "p1", PlainInt32 = 2 };
- AssertJson("{ 'plainString': 'p1', 'plainInt32': 2 }", formatter.Format(message));
- message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
- AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
- message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
- AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
- }
-
- [Test]
- public void OutputIsInNumericFieldOrder_WithDefaults()
- {
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
- var message = new TestJsonFieldOrdering();
- AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
- message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
- AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
- message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
- AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
- }
-
- [Test]
- [TestCase("1970-01-01T00:00:00Z", 0)]
- [TestCase("1970-01-01T00:00:00.000000001Z", 1)]
- [TestCase("1970-01-01T00:00:00.000000010Z", 10)]
- [TestCase("1970-01-01T00:00:00.000000100Z", 100)]
- [TestCase("1970-01-01T00:00:00.000001Z", 1000)]
- [TestCase("1970-01-01T00:00:00.000010Z", 10000)]
- [TestCase("1970-01-01T00:00:00.000100Z", 100000)]
- [TestCase("1970-01-01T00:00:00.001Z", 1000000)]
- [TestCase("1970-01-01T00:00:00.010Z", 10000000)]
- [TestCase("1970-01-01T00:00:00.100Z", 100000000)]
- [TestCase("1970-01-01T00:00:00.120Z", 120000000)]
- [TestCase("1970-01-01T00:00:00.123Z", 123000000)]
- [TestCase("1970-01-01T00:00:00.123400Z", 123400000)]
- [TestCase("1970-01-01T00:00:00.123450Z", 123450000)]
- [TestCase("1970-01-01T00:00:00.123456Z", 123456000)]
- [TestCase("1970-01-01T00:00:00.123456700Z", 123456700)]
- [TestCase("1970-01-01T00:00:00.123456780Z", 123456780)]
- [TestCase("1970-01-01T00:00:00.123456789Z", 123456789)]
- public void TimestampStandalone(string expected, int nanos)
- {
- Assert.AreEqual(WrapInQuotes(expected), new Timestamp { Nanos = nanos }.ToString());
- }
-
- [Test]
- public void TimestampStandalone_FromDateTime()
- {
- // One before and one after the Unix epoch, more easily represented via DateTime.
- Assert.AreEqual("\"1673-06-19T12:34:56Z\"",
- new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp().ToString());
- Assert.AreEqual("\"2015-07-31T10:29:34Z\"",
- new DateTime(2015, 7, 31, 10, 29, 34, DateTimeKind.Utc).ToTimestamp().ToString());
- }
-
- [Test]
- [TestCase(-1, -1)] // Would be valid as duration
- [TestCase(1, Timestamp.MaxNanos + 1)]
- [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)]
- [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, 0)]
- public void TimestampStandalone_NonNormalized(long seconds, int nanoseconds)
- {
- var timestamp = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
- Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(timestamp));
- }
-
- [Test]
- public void TimestampField()
- {
- var message = new TestWellKnownTypes { TimestampField = new Timestamp() };
- AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- [TestCase(0, 0, "0s")]
- [TestCase(1, 0, "1s")]
- [TestCase(-1, 0, "-1s")]
- [TestCase(0, 1, "0.000000001s")]
- [TestCase(0, 10, "0.000000010s")]
- [TestCase(0, 100, "0.000000100s")]
- [TestCase(0, 1000, "0.000001s")]
- [TestCase(0, 10000, "0.000010s")]
- [TestCase(0, 100000, "0.000100s")]
- [TestCase(0, 1000000, "0.001s")]
- [TestCase(0, 10000000, "0.010s")]
- [TestCase(0, 100000000, "0.100s")]
- [TestCase(0, 120000000, "0.120s")]
- [TestCase(0, 123000000, "0.123s")]
- [TestCase(0, 123400000, "0.123400s")]
- [TestCase(0, 123450000, "0.123450s")]
- [TestCase(0, 123456000, "0.123456s")]
- [TestCase(0, 123456700, "0.123456700s")]
- [TestCase(0, 123456780, "0.123456780s")]
- [TestCase(0, 123456789, "0.123456789s")]
- [TestCase(0, -100000000, "-0.100s")]
- [TestCase(1, 100000000, "1.100s")]
- [TestCase(-1, -100000000, "-1.100s")]
- public void DurationStandalone(long seconds, int nanoseconds, string expected)
- {
- var json = JsonFormatter.Default.Format(new Duration { Seconds = seconds, Nanos = nanoseconds });
- Assert.AreEqual(WrapInQuotes(expected), json);
- }
-
- [Test]
- [TestCase(1, 2123456789)]
- [TestCase(1, -100000000)]
- public void DurationStandalone_NonNormalized(long seconds, int nanoseconds)
- {
- var duration = new Duration { Seconds = seconds, Nanos = nanoseconds };
- Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(duration));
- }
-
- [Test]
- public void DurationField()
- {
- var message = new TestWellKnownTypes { DurationField = new Duration() };
- AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void StructSample()
- {
- var message = new Struct
- {
- Fields =
- {
- { "a", Value.ForNull() },
- { "b", Value.ForBool(false) },
- { "c", Value.ForNumber(10.5) },
- { "d", Value.ForString("text") },
- { "e", Value.ForList(Value.ForString("t1"), Value.ForNumber(5)) },
- { "f", Value.ForStruct(new Struct { Fields = { { "nested", Value.ForString("value") } } }) }
- }
- };
- AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString());
- }
-
- [Test]
- [TestCase("foo__bar")]
- [TestCase("foo_3_ar")]
- [TestCase("fooBar")]
- public void FieldMaskInvalid(string input)
- {
- var mask = new FieldMask { Paths = { input } };
- Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(mask));
- }
-
- [Test]
- public void FieldMaskStandalone()
- {
- var fieldMask = new FieldMask { Paths = { "", "single", "with_underscore", "nested.field.name", "nested..double_dot" } };
- Assert.AreEqual("\",single,withUnderscore,nested.field.name,nested..doubleDot\"", fieldMask.ToString());
-
- // Invalid, but we shouldn't create broken JSON...
- fieldMask = new FieldMask { Paths = { "x\\y" } };
- Assert.AreEqual(@"""x\\y""", fieldMask.ToString());
- }
-
- [Test]
- public void FieldMaskField()
- {
- var message = new TestWellKnownTypes { FieldMaskField = new FieldMask { Paths = { "user.display_name", "photo" } } };
- AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFormatter.Default.Format(message));
- }
-
- // SourceContext is an example of a well-known type with no special JSON handling
- [Test]
- public void SourceContextStandalone()
- {
- var message = new SourceContext { FileName = "foo.proto" };
- AssertJson("{ 'fileName': 'foo.proto' }", JsonFormatter.Default.Format(message));
- }
-
- [Test]
- public void AnyWellKnownType()
- {
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(TypeRegistry.FromMessages(Timestamp.Descriptor)));
- var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
- var any = Any.Pack(timestamp);
- AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any));
- }
-
- [Test]
- public void AnyMessageType()
- {
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
- var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
- var any = Any.Pack(message);
- AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest3.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any));
- }
-
- [Test]
- public void AnyMessageType_CustomPrefix()
- {
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
- var message = new TestAllTypes { SingleInt32 = 10 };
- var any = Any.Pack(message, "foo.bar/baz");
- AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest3.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any));
- }
-
- [Test]
- public void AnyNested()
- {
- var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
- var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(registry));
-
- // Nest an Any as the value of an Any.
- var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
- var nestedMessage = Any.Pack(doubleNestedMessage);
- var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
- AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest3.TestAllTypes', 'singleInt32': 20 } } }",
- formatter.Format(message));
- }
-
- [Test]
- public void AnyUnknownType()
- {
- // The default type registry doesn't have any types in it.
- var message = new TestAllTypes();
- var any = Any.Pack(message);
- Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(any));
- }
-
- [Test]
- [TestCase(typeof(BoolValue), true, "true")]
- [TestCase(typeof(Int32Value), 32, "32")]
- [TestCase(typeof(Int64Value), 32L, "\"32\"")]
- [TestCase(typeof(UInt32Value), 32U, "32")]
- [TestCase(typeof(UInt64Value), 32UL, "\"32\"")]
- [TestCase(typeof(StringValue), "foo", "\"foo\"")]
- [TestCase(typeof(FloatValue), 1.5f, "1.5")]
- [TestCase(typeof(DoubleValue), 1.5d, "1.5")]
- public void Wrappers_Standalone(System.Type wrapperType, object value, string expectedJson)
- {
- IMessage populated = (IMessage)Activator.CreateInstance(wrapperType);
- populated.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumber].Accessor.SetValue(populated, value);
- Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(populated));
- }
-
- // Sanity tests for WriteValue. Not particularly comprehensive, as it's all covered above already,
- // as FormatMessage uses WriteValue.
-
- [TestCase(null, "null")]
- [TestCase(1, "1")]
- [TestCase(1L, "'1'")]
- [TestCase(0.5f, "0.5")]
- [TestCase(0.5d, "0.5")]
- [TestCase("text", "'text'")]
- [TestCase("x\ny", @"'x\ny'")]
- [TestCase(ForeignEnum.ForeignBar, "'FOREIGN_BAR'")]
- public void WriteValue_Constant(object value, string expectedJson)
- {
- AssertWriteValue(value, expectedJson);
- }
-
- [Test]
- public void WriteValue_Timestamp()
- {
- var value = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
- AssertWriteValue(value, "'1673-06-19T12:34:56Z'");
- }
-
- [Test]
- public void WriteValue_Message()
- {
- var value = new TestAllTypes { SingleInt32 = 100, SingleInt64 = 3210987654321L };
- AssertWriteValue(value, "{ 'singleInt32': 100, 'singleInt64': '3210987654321' }");
- }
-
- [Test]
- public void WriteValue_List()
- {
- var value = new RepeatedField<int> { 1, 2, 3 };
- AssertWriteValue(value, "[ 1, 2, 3 ]");
- }
-
- [Test]
- public void Proto2_DefaultValuesWritten()
- {
- var value = new ProtobufTestMessages.Proto2.TestAllTypesProto2() { FieldName13 = 0 };
- AssertWriteValue(value, "{ 'FieldName13': 0 }");
- }
-
- private static void AssertWriteValue(object value, string expectedJson)
- {
- var writer = new StringWriter();
- JsonFormatter.Default.WriteValue(writer, value);
- string actual = writer.ToString();
- AssertJson(expectedJson, actual);
- }
-
- /// <summary>
- /// Checks that the actual JSON is the same as the expected JSON - but after replacing
- /// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
- /// to read.
- /// </summary>
- private static void AssertJson(string expectedJsonWithApostrophes, string actualJson)
- {
- var expectedJson = expectedJsonWithApostrophes.Replace("'", "\"");
- Assert.AreEqual(expectedJson, actualJson);
- }
- }
-}
+#region Copyright notice and license
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#endregion
+
+using System;
+using Google.Protobuf.TestProtos;
+using NUnit.Framework;
+using UnitTest.Issues.TestProtos;
+using Google.Protobuf.WellKnownTypes;
+using Google.Protobuf.Reflection;
+
+using static Google.Protobuf.JsonParserTest; // For WrapInQuotes
+using System.IO;
+using Google.Protobuf.Collections;
+using ProtobufUnittest;
+
+namespace Google.Protobuf
+{
+ /// <summary>
+ /// Tests for the JSON formatter. Note that in these tests, double quotes are replaced with apostrophes
+ /// for the sake of readability (embedding \" everywhere is painful). See the AssertJson method for details.
+ /// </summary>
+ public class JsonFormatterTest
+ {
+ [Test]
+ public void DefaultValues_WhenOmitted()
+ {
+ var formatter = JsonFormatter.Default;
+
+ AssertJson("{ }", formatter.Format(new ForeignMessage()));
+ AssertJson("{ }", formatter.Format(new TestAllTypes()));
+ AssertJson("{ }", formatter.Format(new TestMap()));
+ }
+
+ [Test]
+ public void DefaultValues_WhenIncluded()
+ {
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
+ }
+
+ [Test]
+ public void EnumAllowAlias()
+ {
+ var message = new TestEnumAllowAlias
+ {
+ Value = TestEnumWithDupValue.Foo2,
+ };
+ var actualText = JsonFormatter.Default.Format(message);
+ var expectedText = "{ 'value': 'FOO1' }";
+ AssertJson(expectedText, actualText);
+ }
+
+ [Test]
+ public void EnumAsInt()
+ {
+ var message = new TestAllTypes
+ {
+ SingleForeignEnum = ForeignEnum.ForeignBar,
+ RepeatedForeignEnum = { ForeignEnum.ForeignBaz, (ForeignEnum) 100, ForeignEnum.ForeignFoo }
+ };
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatEnumsAsIntegers(true));
+ var actualText = formatter.Format(message);
+ var expectedText = "{ " +
+ "'singleForeignEnum': 5, " +
+ "'repeatedForeignEnum': [ 6, 100, 4 ]" +
+ " }";
+ AssertJson(expectedText, actualText);
+ }
+
+ [Test]
+ public void AllSingleFields()
+ {
+ var message = new TestAllTypes
+ {
+ SingleBool = true,
+ SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
+ SingleDouble = 23.5,
+ SingleFixed32 = 23,
+ SingleFixed64 = 1234567890123,
+ SingleFloat = 12.25f,
+ SingleForeignEnum = ForeignEnum.ForeignBar,
+ SingleForeignMessage = new ForeignMessage { C = 10 },
+ SingleImportEnum = ImportEnum.ImportBaz,
+ SingleImportMessage = new ImportMessage { D = 20 },
+ SingleInt32 = 100,
+ SingleInt64 = 3210987654321,
+ SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo,
+ SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
+ SinglePublicImportMessage = new PublicImportMessage { E = 54 },
+ SingleSfixed32 = -123,
+ SingleSfixed64 = -12345678901234,
+ SingleSint32 = -456,
+ SingleSint64 = -12345678901235,
+ SingleString = "test\twith\ttabs",
+ SingleUint32 = uint.MaxValue,
+ SingleUint64 = ulong.MaxValue,
+ };
+ var actualText = JsonFormatter.Default.Format(message);
+
+ // Fields in numeric order
+ var expectedText = "{ " +
+ "'singleInt32': 100, " +
+ "'singleInt64': '3210987654321', " +
+ "'singleUint32': 4294967295, " +
+ "'singleUint64': '18446744073709551615', " +
+ "'singleSint32': -456, " +
+ "'singleSint64': '-12345678901235', " +
+ "'singleFixed32': 23, " +
+ "'singleFixed64': '1234567890123', " +
+ "'singleSfixed32': -123, " +
+ "'singleSfixed64': '-12345678901234', " +
+ "'singleFloat': 12.25, " +
+ "'singleDouble': 23.5, " +
+ "'singleBool': true, " +
+ "'singleString': 'test\\twith\\ttabs', " +
+ "'singleBytes': 'AQIDBA==', " +
+ "'singleNestedMessage': { 'bb': 35 }, " +
+ "'singleForeignMessage': { 'c': 10 }, " +
+ "'singleImportMessage': { 'd': 20 }, " +
+ "'singleNestedEnum': 'FOO', " +
+ "'singleForeignEnum': 'FOREIGN_BAR', " +
+ "'singleImportEnum': 'IMPORT_BAZ', " +
+ "'singlePublicImportMessage': { 'e': 54 }" +
+ " }";
+ AssertJson(expectedText, actualText);
+ }
+
+ [Test]
+ public void WithFormatDefaultValues_DoesNotAffectMessageFields()
+ {
+ var message = new TestAllTypes();
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var json = formatter.Format(message);
+ Assert.IsFalse(json.Contains("\"singleNestedMessage\""));
+ Assert.IsFalse(json.Contains("\"singleForeignMessage\""));
+ Assert.IsFalse(json.Contains("\"singleImportMessage\""));
+ }
+
+ [Test]
+ public void WithFormatDefaultValues_DoesNotAffectProto3OptionalFields()
+ {
+ var message = new TestProto3Optional();
+ message.OptionalInt32 = 0;
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var json = formatter.Format(message);
+ // The non-optional proto3 fields are formatted, as is the optional-but-specified field.
+ AssertJson("{ 'optionalInt32': 0, 'singularInt32': 0, 'singularInt64': '0' }", json);
+ }
+
+ [Test]
+ public void WithFormatDefaultValues_DoesNotAffectProto2Fields()
+ {
+ var message = new TestProtos.Proto2.ForeignMessage();
+ message.C = 0;
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var json = formatter.Format(message);
+ // The specified field is formatted, but the non-specified field (d) is not.
+ AssertJson("{ 'c': 0 }", json);
+ }
+
+ [Test]
+ public void WithFormatDefaultValues_DoesNotAffectOneofFields()
+ {
+ var message = new TestOneof();
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var json = formatter.Format(message);
+ AssertJson("{ }", json);
+ }
+
+ [Test]
+ public void RepeatedField()
+ {
+ AssertJson("{ 'repeatedInt32': [ 1, 2, 3, 4, 5 ] }",
+ JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
+ }
+
+ [Test]
+ public void MapField_StringString()
+ {
+ AssertJson("{ 'mapStringString': { 'with spaces': 'bar', 'a': 'b' } }",
+ JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
+ }
+
+ [Test]
+ public void MapField_Int32Int32()
+ {
+ // The keys are quoted, but the values aren't.
+ AssertJson("{ 'mapInt32Int32': { '0': 1, '2': 3 } }",
+ JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
+ }
+
+ [Test]
+ public void MapField_BoolBool()
+ {
+ // The keys are quoted, but the values aren't.
+ AssertJson("{ 'mapBoolBool': { 'false': true, 'true': false } }",
+ JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
+ }
+
+ [Test]
+ public void NullValueOutsideStruct()
+ {
+ var message = new NullValueOutsideStruct { NullValue = NullValue.NullValue };
+ AssertJson("{ 'nullValue': null }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void NullValueNotInOneof()
+ {
+ var message = new NullValueNotInOneof();
+ AssertJson("{ }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void NullValueNotInOneof_FormatDefaults()
+ {
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var message = new NullValueNotInOneof();
+ AssertJson("{ 'nullValue': null }", formatter.Format(message));
+ }
+
+ [TestCase(1.0, "1")]
+ [TestCase(double.NaN, "'NaN'")]
+ [TestCase(double.PositiveInfinity, "'Infinity'")]
+ [TestCase(double.NegativeInfinity, "'-Infinity'")]
+ public void DoubleRepresentations(double value, string expectedValueText)
+ {
+ var message = new TestAllTypes { SingleDouble = value };
+ string actualText = JsonFormatter.Default.Format(message);
+ string expectedText = "{ 'singleDouble': " + expectedValueText + " }";
+ AssertJson(expectedText, actualText);
+ }
+
+ [Test]
+ public void UnknownEnumValueNumeric_SingleField()
+ {
+ var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
+ AssertJson("{ 'singleForeignEnum': 100 }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void UnknownEnumValueNumeric_RepeatedField()
+ {
+ var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.ForeignBaz, (ForeignEnum) 100, ForeignEnum.ForeignFoo } };
+ AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 100, 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void UnknownEnumValueNumeric_MapField()
+ {
+ var message = new TestMap { MapInt32Enum = { { 1, MapEnum.Foo }, { 2, (MapEnum) 100 }, { 3, MapEnum.Bar } } };
+ AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '2': 100, '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void UnknownEnumValue_RepeatedField_AllEntriesUnknown()
+ {
+ var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
+ AssertJson("{ 'repeatedForeignEnum': [ 200, 100 ] }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit
+ [TestCase("a\u0601b", "a\\u0601b")] // Ranged
+ [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...)
+ public void SimpleNonAscii(string text, string encoded)
+ {
+ var message = new TestAllTypes { SingleString = text };
+ AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void SurrogatePairEscaping()
+ {
+ var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" };
+ AssertJson("{ 'singleString': 'a\\ud801\\udc01b' }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void InvalidSurrogatePairsFail()
+ {
+ // Note: don't use TestCase for these, as the strings can't be reliably represented
+ // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/
+
+ // Lone low surrogate
+ var message = new TestAllTypes { SingleString = "a\uDC01b" };
+ Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
+
+ // Lone high surrogate
+ message = new TestAllTypes { SingleString = "a\uD801b" };
+ Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ [TestCase("foo_bar", "fooBar")]
+ [TestCase("bananaBanana", "bananaBanana")]
+ [TestCase("BANANABanana", "BANANABanana")]
+ [TestCase("simple", "simple")]
+ [TestCase("ACTION_AND_ADVENTURE", "ACTIONANDADVENTURE")]
+ [TestCase("action_and_adventure", "actionAndAdventure")]
+ [TestCase("kFoo", "kFoo")]
+ [TestCase("HTTPServer", "HTTPServer")]
+ [TestCase("CLIENT", "CLIENT")]
+ public void ToJsonName(string original, string expected)
+ {
+ Assert.AreEqual(expected, JsonFormatter.ToJsonName(original));
+ }
+
+ [Test]
+ [TestCase(null, "{ }")]
+ [TestCase("x", "{ 'fooString': 'x' }")]
+ [TestCase("", "{ 'fooString': '' }")]
+ public void Oneof(string fooStringValue, string expectedJson)
+ {
+ var message = new TestOneof();
+ if (fooStringValue != null)
+ {
+ message.FooString = fooStringValue;
+ }
+
+ // We should get the same result both with and without "format default values".
+ var formatter = JsonFormatter.Default;
+ AssertJson(expectedJson, formatter.Format(message));
+ formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ AssertJson(expectedJson, formatter.Format(message));
+ }
+
+ [Test]
+ public void WrapperFormatting_Single()
+ {
+ // Just a few examples, handling both classes and value types, and
+ // default vs non-default values
+ var message = new TestWellKnownTypes
+ {
+ Int64Field = 10,
+ Int32Field = 0,
+ BytesField = ByteString.FromBase64("ABCD"),
+ StringField = ""
+ };
+ var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
+ AssertJson(expectedJson, JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void WrapperFormatting_Message()
+ {
+ Assert.AreEqual("\"\"", JsonFormatter.Default.Format(new StringValue()));
+ Assert.AreEqual("0", JsonFormatter.Default.Format(new Int32Value()));
+ }
+
+ [Test]
+ public void WrapperFormatting_FormatDefaultValuesDoesNotFormatNull()
+ {
+ // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
+ var message = new TestWellKnownTypes { Int32Field = 10 };
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var actualJson = formatter.Format(message);
+ // This *used* to include "int64Field": null, but that was a bug.
+ // WithDefaultValues should not affect message fields, including wrapper types.
+ Assert.IsFalse(actualJson.Contains("\"int64Field\": null"));
+ Assert.IsTrue(actualJson.Contains("\"int32Field\": 10"));
+ }
+
+ [Test]
+ public void OutputIsInNumericFieldOrder_NoDefaults()
+ {
+ var formatter = JsonFormatter.Default;
+ var message = new TestJsonFieldOrdering { PlainString = "p1", PlainInt32 = 2 };
+ AssertJson("{ 'plainString': 'p1', 'plainInt32': 2 }", formatter.Format(message));
+ message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
+ AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
+ message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
+ AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
+ }
+
+ [Test]
+ public void OutputIsInNumericFieldOrder_WithDefaults()
+ {
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
+ var message = new TestJsonFieldOrdering();
+ AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
+ message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
+ AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
+ message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
+ AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
+ }
+
+ [Test]
+ [TestCase("1970-01-01T00:00:00Z", 0)]
+ [TestCase("1970-01-01T00:00:00.000000001Z", 1)]
+ [TestCase("1970-01-01T00:00:00.000000010Z", 10)]
+ [TestCase("1970-01-01T00:00:00.000000100Z", 100)]
+ [TestCase("1970-01-01T00:00:00.000001Z", 1000)]
+ [TestCase("1970-01-01T00:00:00.000010Z", 10000)]
+ [TestCase("1970-01-01T00:00:00.000100Z", 100000)]
+ [TestCase("1970-01-01T00:00:00.001Z", 1000000)]
+ [TestCase("1970-01-01T00:00:00.010Z", 10000000)]
+ [TestCase("1970-01-01T00:00:00.100Z", 100000000)]
+ [TestCase("1970-01-01T00:00:00.120Z", 120000000)]
+ [TestCase("1970-01-01T00:00:00.123Z", 123000000)]
+ [TestCase("1970-01-01T00:00:00.123400Z", 123400000)]
+ [TestCase("1970-01-01T00:00:00.123450Z", 123450000)]
+ [TestCase("1970-01-01T00:00:00.123456Z", 123456000)]
+ [TestCase("1970-01-01T00:00:00.123456700Z", 123456700)]
+ [TestCase("1970-01-01T00:00:00.123456780Z", 123456780)]
+ [TestCase("1970-01-01T00:00:00.123456789Z", 123456789)]
+ public void TimestampStandalone(string expected, int nanos)
+ {
+ Assert.AreEqual(WrapInQuotes(expected), new Timestamp { Nanos = nanos }.ToString());
+ }
+
+ [Test]
+ public void TimestampStandalone_FromDateTime()
+ {
+ // One before and one after the Unix epoch, more easily represented via DateTime.
+ Assert.AreEqual("\"1673-06-19T12:34:56Z\"",
+ new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp().ToString());
+ Assert.AreEqual("\"2015-07-31T10:29:34Z\"",
+ new DateTime(2015, 7, 31, 10, 29, 34, DateTimeKind.Utc).ToTimestamp().ToString());
+ }
+
+ [Test]
+ [TestCase(-1, -1)] // Would be valid as duration
+ [TestCase(1, Timestamp.MaxNanos + 1)]
+ [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)]
+ [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, 0)]
+ public void TimestampStandalone_NonNormalized(long seconds, int nanoseconds)
+ {
+ var timestamp = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
+ Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(timestamp));
+ }
+
+ [Test]
+ public void TimestampField()
+ {
+ var message = new TestWellKnownTypes { TimestampField = new Timestamp() };
+ AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ [TestCase(0, 0, "0s")]
+ [TestCase(1, 0, "1s")]
+ [TestCase(-1, 0, "-1s")]
+ [TestCase(0, 1, "0.000000001s")]
+ [TestCase(0, 10, "0.000000010s")]
+ [TestCase(0, 100, "0.000000100s")]
+ [TestCase(0, 1000, "0.000001s")]
+ [TestCase(0, 10000, "0.000010s")]
+ [TestCase(0, 100000, "0.000100s")]
+ [TestCase(0, 1000000, "0.001s")]
+ [TestCase(0, 10000000, "0.010s")]
+ [TestCase(0, 100000000, "0.100s")]
+ [TestCase(0, 120000000, "0.120s")]
+ [TestCase(0, 123000000, "0.123s")]
+ [TestCase(0, 123400000, "0.123400s")]
+ [TestCase(0, 123450000, "0.123450s")]
+ [TestCase(0, 123456000, "0.123456s")]
+ [TestCase(0, 123456700, "0.123456700s")]
+ [TestCase(0, 123456780, "0.123456780s")]
+ [TestCase(0, 123456789, "0.123456789s")]
+ [TestCase(0, -100000000, "-0.100s")]
+ [TestCase(1, 100000000, "1.100s")]
+ [TestCase(-1, -100000000, "-1.100s")]
+ public void DurationStandalone(long seconds, int nanoseconds, string expected)
+ {
+ var json = JsonFormatter.Default.Format(new Duration { Seconds = seconds, Nanos = nanoseconds });
+ Assert.AreEqual(WrapInQuotes(expected), json);
+ }
+
+ [Test]
+ [TestCase(1, 2123456789)]
+ [TestCase(1, -100000000)]
+ public void DurationStandalone_NonNormalized(long seconds, int nanoseconds)
+ {
+ var duration = new Duration { Seconds = seconds, Nanos = nanoseconds };
+ Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(duration));
+ }
+
+ [Test]
+ public void DurationField()
+ {
+ var message = new TestWellKnownTypes { DurationField = new Duration() };
+ AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void StructSample()
+ {
+ var message = new Struct
+ {
+ Fields =
+ {
+ { "a", Value.ForNull() },
+ { "b", Value.ForBool(false) },
+ { "c", Value.ForNumber(10.5) },
+ { "d", Value.ForString("text") },
+ { "e", Value.ForList(Value.ForString("t1"), Value.ForNumber(5)) },
+ { "f", Value.ForStruct(new Struct { Fields = { { "nested", Value.ForString("value") } } }) }
+ }
+ };
+ AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString());
+ }
+
+ [Test]
+ [TestCase("foo__bar")]
+ [TestCase("foo_3_ar")]
+ [TestCase("fooBar")]
+ public void FieldMaskInvalid(string input)
+ {
+ var mask = new FieldMask { Paths = { input } };
+ Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(mask));
+ }
+
+ [Test]
+ public void FieldMaskStandalone()
+ {
+ var fieldMask = new FieldMask { Paths = { "", "single", "with_underscore", "nested.field.name", "nested..double_dot" } };
+ Assert.AreEqual("\",single,withUnderscore,nested.field.name,nested..doubleDot\"", fieldMask.ToString());
+
+ // Invalid, but we shouldn't create broken JSON...
+ fieldMask = new FieldMask { Paths = { "x\\y" } };
+ Assert.AreEqual(@"""x\\y""", fieldMask.ToString());
+ }
+
+ [Test]
+ public void FieldMaskField()
+ {
+ var message = new TestWellKnownTypes { FieldMaskField = new FieldMask { Paths = { "user.display_name", "photo" } } };
+ AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFormatter.Default.Format(message));
+ }
+
+ // SourceContext is an example of a well-known type with no special JSON handling
+ [Test]
+ public void SourceContextStandalone()
+ {
+ var message = new SourceContext { FileName = "foo.proto" };
+ AssertJson("{ 'fileName': 'foo.proto' }", JsonFormatter.Default.Format(message));
+ }
+
+ [Test]
+ public void AnyWellKnownType()
+ {
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(TypeRegistry.FromMessages(Timestamp.Descriptor)));
+ var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
+ var any = Any.Pack(timestamp);
+ AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any));
+ }
+
+ [Test]
+ public void AnyMessageType()
+ {
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
+ var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
+ var any = Any.Pack(message);
+ AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest3.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any));
+ }
+
+ [Test]
+ public void AnyMessageType_CustomPrefix()
+ {
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
+ var message = new TestAllTypes { SingleInt32 = 10 };
+ var any = Any.Pack(message, "foo.bar/baz");
+ AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest3.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any));
+ }
+
+ [Test]
+ public void AnyNested()
+ {
+ var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
+ var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(registry));
+
+ // Nest an Any as the value of an Any.
+ var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
+ var nestedMessage = Any.Pack(doubleNestedMessage);
+ var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
+ AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest3.TestAllTypes', 'singleInt32': 20 } } }",
+ formatter.Format(message));
+ }
+
+ [Test]
+ public void AnyUnknownType()
+ {
+ // The default type registry doesn't have any types in it.
+ var message = new TestAllTypes();
+ var any = Any.Pack(message);
+ Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(any));
+ }
+
+ [Test]
+ [TestCase(typeof(BoolValue), true, "true")]
+ [TestCase(typeof(Int32Value), 32, "32")]
+ [TestCase(typeof(Int64Value), 32L, "\"32\"")]
+ [TestCase(typeof(UInt32Value), 32U, "32")]
+ [TestCase(typeof(UInt64Value), 32UL, "\"32\"")]
+ [TestCase(typeof(StringValue), "foo", "\"foo\"")]
+ [TestCase(typeof(FloatValue), 1.5f, "1.5")]
+ [TestCase(typeof(DoubleValue), 1.5d, "1.5")]
+ public void Wrappers_Standalone(System.Type wrapperType, object value, string expectedJson)
+ {
+ IMessage populated = (IMessage)Activator.CreateInstance(wrapperType);
+ populated.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumber].Accessor.SetValue(populated, value);
+ Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(populated));
+ }
+
+ // Sanity tests for WriteValue. Not particularly comprehensive, as it's all covered above already,
+ // as FormatMessage uses WriteValue.
+
+ [TestCase(null, "null")]
+ [TestCase(1, "1")]
+ [TestCase(1L, "'1'")]
+ [TestCase(0.5f, "0.5")]
+ [TestCase(0.5d, "0.5")]
+ [TestCase("text", "'text'")]
+ [TestCase("x\ny", @"'x\ny'")]
+ [TestCase(ForeignEnum.ForeignBar, "'FOREIGN_BAR'")]
+ public void WriteValue_Constant(object value, string expectedJson)
+ {
+ AssertWriteValue(value, expectedJson);
+ }
+
+ [Test]
+ public void WriteValue_Timestamp()
+ {
+ var value = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
+ AssertWriteValue(value, "'1673-06-19T12:34:56Z'");
+ }
+
+ [Test]
+ public void WriteValue_Message()
+ {
+ var value = new TestAllTypes { SingleInt32 = 100, SingleInt64 = 3210987654321L };
+ AssertWriteValue(value, "{ 'singleInt32': 100, 'singleInt64': '3210987654321' }");
+ }
+
+ [Test]
+ public void WriteValue_Message_PreserveNames()
+ {
+ var value = new TestAllTypes { SingleInt32 = 100, SingleInt64 = 3210987654321L };
+ AssertWriteValue(value, "{ 'single_int32': 100, 'single_int64': '3210987654321' }", JsonFormatter.Settings.Default.WithPreserveProtoFieldNames(true));
+ }
+
+ [Test]
+ public void WriteValue_List()
+ {
+ var value = new RepeatedField<int> { 1, 2, 3 };
+ AssertWriteValue(value, "[ 1, 2, 3 ]");
+ }
+
+ [Test]
+ public void Proto2_DefaultValuesWritten()
+ {
+ var value = new ProtobufTestMessages.Proto2.TestAllTypesProto2() { FieldName13 = 0 };
+ AssertWriteValue(value, "{ 'FieldName13': 0 }");
+ }
+
+ private static void AssertWriteValue(object value, string expectedJson, JsonFormatter.Settings settings = null)
+ {
+ var writer = new StringWriter();
+ new JsonFormatter(settings ?? JsonFormatter.Settings.Default).WriteValue(writer, value);
+ string actual = writer.ToString();
+ AssertJson(expectedJson, actual);
+ }
+
+ /// <summary>
+ /// Checks that the actual JSON is the same as the expected JSON - but after replacing
+ /// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
+ /// to read.
+ /// </summary>
+ private static void AssertJson(string expectedJsonWithApostrophes, string actualJson)
+ {
+ var expectedJson = expectedJsonWithApostrophes.Replace("'", "\"");
+ Assert.AreEqual(expectedJson, actualJson);
+ }
+ }
+}
diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs
index 17fdc7f..db7dc5c 100644
--- a/csharp/src/Google.Protobuf/JsonFormatter.cs
+++ b/csharp/src/Google.Protobuf/JsonFormatter.cs
@@ -233,7 +233,14 @@
writer.Write(PropertySeparator);
}
- WriteString(writer, accessor.Descriptor.JsonName);
+ if (settings.PreserveProtoFieldNames)
+ {
+ WriteString(writer, accessor.Descriptor.Name);
+ }
+ else
+ {
+ WriteString(writer, accessor.Descriptor.JsonName);
+ }
writer.Write(NameValueSeparator);
WriteValue(writer, value);
@@ -816,6 +823,11 @@
/// </summary>
public bool FormatEnumsAsIntegers { get; }
+ /// <summary>
+ /// Whether to use the original proto field names as defined in the .proto file. Defaults to false.
+ /// </summary>
+ public bool PreserveProtoFieldNames { get; }
+
/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified formatting of default values
@@ -832,7 +844,7 @@
/// </summary>
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
- public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(formatDefaultValues, typeRegistry, false)
+ public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(formatDefaultValues, typeRegistry, false, false)
{
}
@@ -842,32 +854,41 @@
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages. TypeRegistry.Empty will be used if it is null.</param>
/// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
+ /// <param name="preserveProtoFieldNames"><c>true</c> to preserve proto field names; <c>false</c> to convert them to lowerCamelCase.</param>
private Settings(bool formatDefaultValues,
TypeRegistry typeRegistry,
- bool formatEnumsAsIntegers)
+ bool formatEnumsAsIntegers,
+ bool preserveProtoFieldNames)
{
FormatDefaultValues = formatDefaultValues;
TypeRegistry = typeRegistry ?? TypeRegistry.Empty;
FormatEnumsAsIntegers = formatEnumsAsIntegers;
+ PreserveProtoFieldNames = preserveProtoFieldNames;
}
/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified formatting of default values and the current settings.
/// </summary>
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
- public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers);
+ public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames);
/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified type registry and the current settings.
/// </summary>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
- public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers);
+ public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames);
/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified enums formatting option and the current settings.
/// </summary>
/// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
- public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers);
+ public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers, PreserveProtoFieldNames);
+
+ /// <summary>
+ /// Creates a new <see cref="Settings"/> object with the specified field name formatting option and the current settings.
+ /// </summary>
+ /// <param name="preserveProtoFieldNames"><c>true</c> to preserve proto field names; <c>false</c> to convert them to lowerCamelCase.</param>
+ public Settings WithPreserveProtoFieldNames(bool preserveProtoFieldNames) => new Settings(FormatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, preserveProtoFieldNames);
}
// Effectively a cache of mapping from enum values to the original name as specified in the proto file,
diff --git a/docs/options.md b/docs/options.md
index e4ca729..dbb3563 100644
--- a/docs/options.md
+++ b/docs/options.md
@@ -300,3 +300,7 @@
1. Protoc-gen-go-svc
* Website: https://github.com/dane/protoc-gen-go-svc
* Extension: 1140
+
+1. Embedded Proto
+ * Website: https://EmbeddedProto.com
+ * Extension: 1141
diff --git a/kokoro/linux/benchmark/build.sh b/kokoro/linux/benchmark/build.sh
index dee7c47..f470989 100755
--- a/kokoro/linux/benchmark/build.sh
+++ b/kokoro/linux/benchmark/build.sh
@@ -1,18 +1,18 @@
#!/bin/bash
#
+# This is the top-level script we give to Kokoro as the entry point for
+# running the "pull request" project:
+#
+# This script selects a specific Dockerfile (for building a Docker image) and
+# a script to run inside that image. Then we delegate to the general
+# build_and_run_docker.sh script.
+
# Change to repo root
cd $(dirname $0)/../../..
-set -ex
-
-# Install openJDK 11 (required by the java benchmarks)
-sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 78BD65473CB3BD13
-sudo add-apt-repository ppa:openjdk-r/ppa
-sudo apt-get update
-sudo apt-get install -y openjdk-11-jdk-headless
-
-# use java 11
-sudo update-java-alternatives --set /usr/lib/jvm/java-1.11.0-openjdk-amd64
-java -version
-
-./tests.sh benchmark
+export DOCKERHUB_ORGANIZATION=protobuftesting
+export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/java_stretch
+export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh
+export OUTPUT_DIR=testoutput
+export TEST_SET="benchmark"
+./kokoro/linux/build_and_run_docker.sh
diff --git a/kokoro/linux/benchmark/run.sh b/kokoro/linux/benchmark/run.sh
index acd8737..963f779 100755
--- a/kokoro/linux/benchmark/run.sh
+++ b/kokoro/linux/benchmark/run.sh
@@ -23,7 +23,7 @@
./configure CXXFLAGS="-fPIC -O2"
make -j8
pushd python
-virtualenv -p python3 env
+python3 -m venv env
source env/bin/activate
python3 setup.py build --cpp_implementation
pip3 install --install-option="--cpp_implementation" .
diff --git a/kokoro/linux/dockerfile/test/java_stretch/Dockerfile b/kokoro/linux/dockerfile/test/java_stretch/Dockerfile
index 7e1feea..8eeb6a2 100644
--- a/kokoro/linux/dockerfile/test/java_stretch/Dockerfile
+++ b/kokoro/linux/dockerfile/test/java_stretch/Dockerfile
@@ -11,6 +11,7 @@
build-essential \
bzip2 \
ccache \
+ cmake \
curl \
gcc \
git \
@@ -21,6 +22,7 @@
libtool \
make \
parallel \
+ pkg-config \
time \
wget \
# Java dependencies
@@ -29,6 +31,7 @@
# Required for the gtest build.
python2 \
# Python dependencies
+ python3-dev \
python3-setuptools \
python3-pip \
python3-venv \
diff --git a/kokoro/linux/dockerfile/test/python35/Dockerfile b/kokoro/linux/dockerfile/test/python35/Dockerfile
deleted file mode 100644
index 50ee184..0000000
--- a/kokoro/linux/dockerfile/test/python35/Dockerfile
+++ /dev/null
@@ -1,31 +0,0 @@
-FROM python:3.5-buster
-
-# Install dependencies. We start with the basic ones require to build protoc
-# and the C++ build
-RUN apt-get update && apt-get install -y \
- autoconf \
- autotools-dev \
- build-essential \
- bzip2 \
- ccache \
- curl \
- gcc \
- git \
- libc6 \
- libc6-dbg \
- libc6-dev \
- libgtest-dev \
- libtool \
- make \
- parallel \
- time \
- wget \
- && apt-get clean \
- && rm -rf /var/lib/apt/lists/*
-
-# Install Python libraries.
-RUN python -m pip install --no-cache-dir --upgrade \
- pip \
- setuptools \
- tox \
- wheel
diff --git a/kokoro/linux/dockerfile/test/python36/Dockerfile b/kokoro/linux/dockerfile/test/python36/Dockerfile
deleted file mode 100644
index 742503e..0000000
--- a/kokoro/linux/dockerfile/test/python36/Dockerfile
+++ /dev/null
@@ -1,31 +0,0 @@
-FROM python:3.6-buster
-
-# Install dependencies. We start with the basic ones require to build protoc
-# and the C++ build
-RUN apt-get update && apt-get install -y \
- autoconf \
- autotools-dev \
- build-essential \
- bzip2 \
- ccache \
- curl \
- gcc \
- git \
- libc6 \
- libc6-dbg \
- libc6-dev \
- libgtest-dev \
- libtool \
- make \
- parallel \
- time \
- wget \
- && apt-get clean \
- && rm -rf /var/lib/apt/lists/*
-
-# Install Python libraries.
-RUN python -m pip install --no-cache-dir --upgrade \
- pip \
- setuptools \
- tox \
- wheel
diff --git a/kokoro/linux/python27/build.sh b/kokoro/linux/python27/build.sh
deleted file mode 100755
index 41531a1..0000000
--- a/kokoro/linux/python27/build.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-#
-# This is the top-level script we give to Kokoro as the entry point for
-# running the "pull request" project:
-#
-# This script selects a specific Dockerfile (for building a Docker image) and
-# a script to run inside that image. Then we delegate to the general
-# build_and_run_docker.sh script.
-
-# Change to repo root
-cd $(dirname $0)/../../..
-
-export DOCKERHUB_ORGANIZATION=protobuftesting
-export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python27
-export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh
-export OUTPUT_DIR=testoutput
-export TEST_SET="python27"
-./kokoro/linux/build_and_run_docker.sh
diff --git a/kokoro/linux/python27/continuous.cfg b/kokoro/linux/python27/continuous.cfg
deleted file mode 100644
index dd98469..0000000
--- a/kokoro/linux/python27/continuous.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python27/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python27/presubmit.cfg b/kokoro/linux/python27/presubmit.cfg
deleted file mode 100644
index dd98469..0000000
--- a/kokoro/linux/python27/presubmit.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python27/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python27_cpp/build.sh b/kokoro/linux/python27_cpp/build.sh
deleted file mode 100755
index 1a972ee..0000000
--- a/kokoro/linux/python27_cpp/build.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-#
-# This is the top-level script we give to Kokoro as the entry point for
-# running the "pull request" project:
-#
-# This script selects a specific Dockerfile (for building a Docker image) and
-# a script to run inside that image. Then we delegate to the general
-# build_and_run_docker.sh script.
-
-# Change to repo root
-cd $(dirname $0)/../../..
-
-export DOCKERHUB_ORGANIZATION=protobuftesting
-export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python27
-export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh
-export OUTPUT_DIR=testoutput
-export TEST_SET="python27_cpp"
-./kokoro/linux/build_and_run_docker.sh
diff --git a/kokoro/linux/python27_cpp/continuous.cfg b/kokoro/linux/python27_cpp/continuous.cfg
deleted file mode 100644
index ace22d0..0000000
--- a/kokoro/linux/python27_cpp/continuous.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python27_cpp/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python27_cpp/presubmit.cfg b/kokoro/linux/python27_cpp/presubmit.cfg
deleted file mode 100644
index ace22d0..0000000
--- a/kokoro/linux/python27_cpp/presubmit.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python27_cpp/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python35/build.sh b/kokoro/linux/python35/build.sh
deleted file mode 100755
index 66ea03f..0000000
--- a/kokoro/linux/python35/build.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-#
-# This is the top-level script we give to Kokoro as the entry point for
-# running the "pull request" project:
-#
-# This script selects a specific Dockerfile (for building a Docker image) and
-# a script to run inside that image. Then we delegate to the general
-# build_and_run_docker.sh script.
-
-# Change to repo root
-cd $(dirname $0)/../../..
-
-export DOCKERHUB_ORGANIZATION=protobuftesting
-export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python35
-export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh
-export OUTPUT_DIR=testoutput
-export TEST_SET="python35"
-./kokoro/linux/build_and_run_docker.sh
diff --git a/kokoro/linux/python35/continuous.cfg b/kokoro/linux/python35/continuous.cfg
deleted file mode 100644
index 2b3e12c..0000000
--- a/kokoro/linux/python35/continuous.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python35/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python35/presubmit.cfg b/kokoro/linux/python35/presubmit.cfg
deleted file mode 100644
index 2b3e12c..0000000
--- a/kokoro/linux/python35/presubmit.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python35/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python35_cpp/build.sh b/kokoro/linux/python35_cpp/build.sh
deleted file mode 100755
index 4d0dbd4..0000000
--- a/kokoro/linux/python35_cpp/build.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-#
-# This is the top-level script we give to Kokoro as the entry point for
-# running the "pull request" project:
-#
-# This script selects a specific Dockerfile (for building a Docker image) and
-# a script to run inside that image. Then we delegate to the general
-# build_and_run_docker.sh script.
-
-# Change to repo root
-cd $(dirname $0)/../../..
-
-export DOCKERHUB_ORGANIZATION=protobuftesting
-export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python35
-export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh
-export OUTPUT_DIR=testoutput
-export TEST_SET="python35_cpp"
-./kokoro/linux/build_and_run_docker.sh
diff --git a/kokoro/linux/python35_cpp/continuous.cfg b/kokoro/linux/python35_cpp/continuous.cfg
deleted file mode 100644
index ad5cc86..0000000
--- a/kokoro/linux/python35_cpp/continuous.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python35_cpp/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/kokoro/linux/python35_cpp/presubmit.cfg b/kokoro/linux/python35_cpp/presubmit.cfg
deleted file mode 100644
index ad5cc86..0000000
--- a/kokoro/linux/python35_cpp/presubmit.cfg
+++ /dev/null
@@ -1,11 +0,0 @@
-# Config file for running tests in Kokoro
-
-# Location of the build script in repository
-build_file: "protobuf/kokoro/linux/python35_cpp/build.sh"
-timeout_mins: 120
-
-action {
- define_artifacts {
- regex: "**/sponge_log.xml"
- }
-}
diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php
index 86463a9..d44377a 100644
--- a/php/src/Google/Protobuf/Internal/MapField.php
+++ b/php/src/Google/Protobuf/Internal/MapField.php
@@ -135,6 +135,7 @@
* @return object The stored element at given key.
* @throws \ErrorException Invalid type for index.
* @throws \ErrorException Non-existing index.
+ * @todo need to add return type mixed (require update php version to 8.0)
*/
#[\ReturnTypeWillChange]
public function offsetGet($key)
@@ -153,6 +154,7 @@
* @throws \ErrorException Invalid type for key.
* @throws \ErrorException Invalid type for value.
* @throws \ErrorException Non-existing key.
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function offsetSet($key, $value)
@@ -212,6 +214,7 @@
* @param object $key The key of the element to be removed.
* @return void
* @throws \ErrorException Invalid type for key.
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function offsetUnset($key)
diff --git a/php/src/Google/Protobuf/Internal/MapFieldIter.php b/php/src/Google/Protobuf/Internal/MapFieldIter.php
index a3c834b..2ff6b44 100644
--- a/php/src/Google/Protobuf/Internal/MapFieldIter.php
+++ b/php/src/Google/Protobuf/Internal/MapFieldIter.php
@@ -67,6 +67,7 @@
* Reset the status of the iterator
*
* @return void
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function rewind()
@@ -78,6 +79,7 @@
* Return the element at the current position.
*
* @return object The element at the current position.
+ * @todo need to add return type mixed (require update php version to 8.0)
*/
#[\ReturnTypeWillChange]
public function current()
@@ -89,6 +91,7 @@
* Return the current key.
*
* @return object The current key.
+ * @todo need to add return type mixed (require update php version to 8.0)
*/
#[\ReturnTypeWillChange]
public function key()
@@ -119,6 +122,7 @@
* Move to the next position.
*
* @return void
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function next()
diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php
index 704123a..cfe5140 100644
--- a/php/src/Google/Protobuf/Internal/RepeatedField.php
+++ b/php/src/Google/Protobuf/Internal/RepeatedField.php
@@ -121,6 +121,7 @@
* @return object The stored element at given index.
* @throws \ErrorException Invalid type for index.
* @throws \ErrorException Non-existing index.
+ * @todo need to add return type mixed (require update php version to 8.0)
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
@@ -139,6 +140,7 @@
* @throws \ErrorException Invalid type for index.
* @throws \ErrorException Non-existing index.
* @throws \ErrorException Incorrect type of the element.
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
@@ -211,6 +213,7 @@
* @throws \ErrorException Invalid type for index.
* @throws \ErrorException The element to be removed is not at the end of the
* RepeatedField.
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
diff --git a/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php
index 3c85869..ec99b64 100644
--- a/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php
+++ b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php
@@ -70,6 +70,7 @@
* Reset the status of the iterator
*
* @return void
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function rewind()
@@ -81,6 +82,7 @@
* Return the element at the current position.
*
* @return object The element at the current position.
+ * @todo need to add return type mixed (require update php version to 8.0)
*/
#[\ReturnTypeWillChange]
public function current()
@@ -92,6 +94,7 @@
* Return the current position.
*
* @return integer The current position.
+ * @todo need to add return type mixed (require update php version to 8.0)
*/
#[\ReturnTypeWillChange]
public function key()
@@ -103,6 +106,7 @@
* Move to the next position.
*
* @return void
+ * @todo need to add return type void (require update php version to 7.1)
*/
#[\ReturnTypeWillChange]
public function next()
diff --git a/ruby/ext/google/protobuf_c/convert.c b/ruby/ext/google/protobuf_c/convert.c
index 8b98aee..2b3a650 100644
--- a/ruby/ext/google/protobuf_c/convert.c
+++ b/ruby/ext/google/protobuf_c/convert.c
@@ -162,9 +162,9 @@
}
case kUpb_CType_String: {
VALUE utf8 = rb_enc_from_encoding(rb_utf8_encoding());
- if (CLASS_OF(value) == rb_cSymbol) {
+ if (rb_obj_class(value) == rb_cSymbol) {
value = rb_funcall(value, rb_intern("to_s"), 0);
- } else if (CLASS_OF(value) != rb_cString) {
+ } else if (rb_obj_class(value) != rb_cString) {
rb_raise(cTypeError,
"Invalid argument for string field '%s' (given %s).", name,
rb_class2name(CLASS_OF(value)));
@@ -185,7 +185,7 @@
}
case kUpb_CType_Bytes: {
VALUE bytes = rb_enc_from_encoding(rb_ascii8bit_encoding());
- if (CLASS_OF(value) != rb_cString) {
+ if (rb_obj_class(value) != rb_cString) {
rb_raise(cTypeError,
"Invalid argument for bytes field '%s' (given %s).", name,
rb_class2name(CLASS_OF(value)));
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb
index b9d1554..b74aa0f 100755
--- a/ruby/tests/basic.rb
+++ b/ruby/tests/basic.rb
@@ -644,5 +644,19 @@
assert_equal 2, m.map_string_int32.size
assert_equal 1, m.map_string_msg.size
end
+
+ def test_string_with_singleton_class_enabled
+ str = 'foobar'
+ # NOTE: Accessing a singleton class of an object changes its low level class representation
+ # as far as the C API's CLASS_OF() method concerned, exposing the issue
+ str.singleton_class
+ m = proto_module::TestMessage.new(
+ optional_string: str,
+ optional_bytes: str
+ )
+
+ assert_equal str, m.optional_string
+ assert_equal str, m.optional_bytes
+ end
end
end