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

[C#] Fix trim warnings #9182

Merged
merged 1 commit into from Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions Makefile.am
Expand Up @@ -182,10 +182,14 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf/Collections/ProtobufEqualityComparers.cs \
csharp/src/Google.Protobuf/Collections/ReadOnlyDictionary.cs \
csharp/src/Google.Protobuf/Collections/RepeatedField.cs \
csharp/src/Google.Protobuf/Compatibility/DynamicallyAccessedMembersAttribute.cs \
csharp/src/Google.Protobuf/Compatibility/DynamicallyAccessedMemberTypes.cs \
csharp/src/Google.Protobuf/Compatibility/MethodInfoExtensions.cs \
csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs \
csharp/src/Google.Protobuf/Compatibility/RequiresUnreferencedCodeAttribute.cs \
csharp/src/Google.Protobuf/Compatibility/StreamExtensions.cs \
csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs \
csharp/src/Google.Protobuf/Compatibility/UnconditionalSuppressMessageAttribute.cs \
csharp/src/Google.Protobuf/Extension.cs \
csharp/src/Google.Protobuf/ExtensionRegistry.cs \
csharp/src/Google.Protobuf/ExtensionSet.cs \
Expand Down
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<!--
This TestProtos project is kept separate from the original test project for many reasons.
Expand All @@ -15,7 +15,7 @@

<!-- Needed for the net45 build to work on Unix. See https://github.com/dotnet/designs/pull/33 -->
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0-preview.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
Expand Up @@ -21,7 +21,7 @@

<!-- Needed for the net45 build to work on Unix. See https://github.com/dotnet/designs/pull/33 -->
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0-preview.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
132 changes: 128 additions & 4 deletions csharp/src/Google.Protobuf.Test/JsonParserTest.cs
Expand Up @@ -35,6 +35,7 @@
using Google.Protobuf.WellKnownTypes;
using NUnit.Framework;
using ProtobufTestMessages.Proto2;
using ProtobufTestMessages.Proto3;
using System;
using UnitTest.Issues.TestProtos;

Expand Down Expand Up @@ -918,10 +919,10 @@ public void Bytes_InvalidBase64(string badBase64)
}

[Test]
[TestCase("\"FOREIGN_BAR\"", ForeignEnum.ForeignBar)]
[TestCase("5", ForeignEnum.ForeignBar)]
[TestCase("100", (ForeignEnum)100)]
public void EnumValid(string value, ForeignEnum expectedValue)
[TestCase("\"FOREIGN_BAR\"", TestProtos.ForeignEnum.ForeignBar)]
[TestCase("5", TestProtos.ForeignEnum.ForeignBar)]
[TestCase("100", (TestProtos.ForeignEnum)100)]
public void EnumValid(string value, TestProtos.ForeignEnum expectedValue)
{
string json = "{ \"singleForeignEnum\": " + value + " }";
var parsed = TestAllTypes.Parser.ParseJson(json);
Expand Down Expand Up @@ -1021,5 +1022,128 @@ internal static string WrapInQuotes(string text)
{
return '"' + text + '"';
}

[Test]
public void ParseAllNullValues()
{
string json = @"{
""optionalInt32"": null,
""optionalInt64"": null,
""optionalUint32"": null,
""optionalUint64"": null,
""optionalSint32"": null,
""optionalSint64"": null,
""optionalFixed32"": null,
""optionalFixed64"": null,
""optionalSfixed32"": null,
""optionalSfixed64"": null,
""optionalFloat"": null,
""optionalDouble"": null,
""optionalBool"": null,
""optionalString"": null,
""optionalBytes"": null,
""optionalNestedEnum"": null,
""optionalNestedMessage"": null,
""repeatedInt32"": null,
""repeatedInt64"": null,
""repeatedUint32"": null,
""repeatedUint64"": null,
""repeatedSint32"": null,
""repeatedSint64"": null,
""repeatedFixed32"": null,
""repeatedFixed64"": null,
""repeatedSfixed32"": null,
""repeatedSfixed64"": null,
""repeatedFloat"": null,
""repeatedDouble"": null,
""repeatedBool"": null,
""repeatedString"": null,
""repeatedBytes"": null,
""repeatedNestedEnum"": null,
""repeatedNestedMessage"": null,
""mapInt32Int32"": null,
""mapBoolBool"": null,
""mapStringNestedMessage"": null
}";

TestAllTypesProto3 message = new TestAllTypesProto3();

message.OptionalInt32 = 1;
message.OptionalInt64 = 1;
message.OptionalUint32 = 1;
message.OptionalUint64 = 1;
message.OptionalSint32 = 1;
message.OptionalSint64 = 1;
message.OptionalFixed32 = 1;
message.OptionalFixed64 = 1;
message.OptionalSfixed32 = 1;
message.OptionalSfixed64 = 1;
message.OptionalFloat = 1;
message.OptionalDouble = 1;
message.OptionalBool = true;
message.OptionalString = "1";
message.OptionalBytes = ByteString.CopyFrom(new byte[] { 1 });
message.OptionalNestedEnum = TestAllTypesProto3.Types.NestedEnum.Bar;
message.OptionalNestedMessage = new TestAllTypesProto3.Types.NestedMessage();
message.RepeatedInt32.Add(1);
message.RepeatedInt64.Add(1);
message.RepeatedUint32.Add(1);
message.RepeatedUint64.Add(1);
message.RepeatedSint32.Add(1);
message.RepeatedSint64.Add(1);
message.RepeatedFixed32.Add(1);
message.RepeatedFixed64.Add(1);
message.RepeatedSfixed32.Add(1);
message.RepeatedSfixed64.Add(1);
message.RepeatedFloat.Add(1);
message.RepeatedDouble.Add(1);
message.RepeatedBool.Add(true);
message.RepeatedString.Add("1");
message.RepeatedBytes.Add(ByteString.CopyFrom(new byte[] { 1 }));
message.RepeatedNestedEnum.Add(TestAllTypesProto3.Types.NestedEnum.Bar);
message.RepeatedNestedMessage.Add(new TestAllTypesProto3.Types.NestedMessage());
message.MapInt32Int32.Add(1, 1);
message.MapBoolBool.Add(true, true);
message.MapStringNestedMessage.Add(" ", new TestAllTypesProto3.Types.NestedMessage());

JsonParser.Default.Merge(message, json);

Assert.AreEqual(0, message.OptionalInt32);
Assert.AreEqual(0, message.OptionalInt64);
Assert.AreEqual(0, message.OptionalUint32);
Assert.AreEqual(0, message.OptionalUint64);
Assert.AreEqual(0, message.OptionalSint32);
Assert.AreEqual(0, message.OptionalSint64);
Assert.AreEqual(0, message.OptionalFixed32);
Assert.AreEqual(0, message.OptionalFixed64);
Assert.AreEqual(0, message.OptionalSfixed32);
Assert.AreEqual(0, message.OptionalSfixed64);
Assert.AreEqual(0, message.OptionalFloat);
Assert.AreEqual(0, message.OptionalDouble);
Assert.AreEqual(false, message.OptionalBool);
Assert.AreEqual("", message.OptionalString);
Assert.AreEqual(ByteString.Empty, message.OptionalBytes);
Assert.AreEqual(TestAllTypesProto3.Types.NestedEnum.Foo, message.OptionalNestedEnum);
Assert.AreEqual(null, message.OptionalNestedMessage);
Assert.AreEqual(0, message.RepeatedInt32.Count);
Assert.AreEqual(0, message.RepeatedInt64.Count);
Assert.AreEqual(0, message.RepeatedUint32.Count);
Assert.AreEqual(0, message.RepeatedUint64.Count);
Assert.AreEqual(0, message.RepeatedSint32.Count);
Assert.AreEqual(0, message.RepeatedSint64.Count);
Assert.AreEqual(0, message.RepeatedFixed32.Count);
Assert.AreEqual(0, message.RepeatedFixed64.Count);
Assert.AreEqual(0, message.RepeatedSfixed32.Count);
Assert.AreEqual(0, message.RepeatedFloat.Count);
Assert.AreEqual(0, message.RepeatedDouble.Count);
Assert.AreEqual(0, message.RepeatedBool.Count);
Assert.AreEqual(0, message.RepeatedString.Count);
Assert.AreEqual(0, message.RepeatedBytes.Count);
Assert.AreEqual(0, message.RepeatedNestedEnum.Count);
Assert.AreEqual(0, message.RepeatedNestedMessage.Count);
Assert.AreEqual(0, message.MapInt32Int32.Count);
Assert.AreEqual(0, message.MapBoolBool.Count);
Assert.AreEqual(0, message.MapStringNestedMessage.Count);
}
}
}
@@ -0,0 +1,127 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2015 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

#if !NET5_0_OR_GREATER
// Copied with permission from https://github.com/dotnet/runtime/tree/8fbf206d0e518b45ca855832e8bfb391afa85972/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add copies of these code analysis attributes for framework versions that don't have them.

Avoids the need to add #ifdef everywhere a code analysis attribute is used.

namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Specifies the types of members that are dynamically accessed.
///
/// This enumeration has a <see cref="FlagsAttribute"/> attribute that allows a
/// bitwise combination of its member values.
/// </summary>
[Flags]
internal enum DynamicallyAccessedMemberTypes
{
/// <summary>
/// Specifies no members.
/// </summary>
None = 0,

/// <summary>
/// Specifies the default, parameterless public constructor.
/// </summary>
PublicParameterlessConstructor = 0x0001,

/// <summary>
/// Specifies all public constructors.
/// </summary>
PublicConstructors = 0x0002 | PublicParameterlessConstructor,

/// <summary>
/// Specifies all non-public constructors.
/// </summary>
NonPublicConstructors = 0x0004,

/// <summary>
/// Specifies all public methods.
/// </summary>
PublicMethods = 0x0008,

/// <summary>
/// Specifies all non-public methods.
/// </summary>
NonPublicMethods = 0x0010,

/// <summary>
/// Specifies all public fields.
/// </summary>
PublicFields = 0x0020,

/// <summary>
/// Specifies all non-public fields.
/// </summary>
NonPublicFields = 0x0040,

/// <summary>
/// Specifies all public nested types.
/// </summary>
PublicNestedTypes = 0x0080,

/// <summary>
/// Specifies all non-public nested types.
/// </summary>
NonPublicNestedTypes = 0x0100,

/// <summary>
/// Specifies all public properties.
/// </summary>
PublicProperties = 0x0200,

/// <summary>
/// Specifies all non-public properties.
/// </summary>
NonPublicProperties = 0x0400,

/// <summary>
/// Specifies all public events.
/// </summary>
PublicEvents = 0x0800,

/// <summary>
/// Specifies all non-public events.
/// </summary>
NonPublicEvents = 0x1000,

/// <summary>
/// Specifies all interfaces implemented by the type.
/// </summary>
Interfaces = 0x2000,

/// <summary>
/// Specifies all members.
/// </summary>
All = ~None
}
}
#endif