Skip to content

Commit

Permalink
fix: resolve trimming warnings in project
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Omondi committed Aug 30, 2024
1 parent c608fa3 commit 4b47b57
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ configuration:
# Required status checks to pass before merging. Values can be any string, but if the value does not correspond to any existing status check, the status check will be stuck on pending for status since nothing exists to push an actual status
requiredStatusChecks:
- Build and Test # Contains CodeQL
- Validate Project for Trimming
- license/cla
# Require branches to be up to date before merging. boolean
requiresStrictStatusChecks: true
Expand Down
19 changes: 16 additions & 3 deletions .github/workflows/validatePullRequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ jobs:

- name: Install needed dotnet workloads
run: dotnet workload install android macos ios maccatalyst

- name: Restore nuget dependencies
run: dotnet restore ${{ env.solutionName }}

- name: Lint the code
run: dotnet format --verify-no-changes

- name: Restore nuget dependencies
run: dotnet restore ${{ env.solutionName }}

- name: Build
run: dotnet build ${{ env.solutionName }} -c Debug /p:UseSharedCompilation=false,IncludeMauiTargets=true
Expand All @@ -56,4 +56,17 @@ jobs:
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3

validate-trimming:
name: Validate Project for Trimming
runs-on: windows-latest
steps:
- uses: actions/[email protected]

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x

- name: Validate Trimming warnings
run: dotnet publish -c Release -r win-x64 /p:TreatWarningsAsErrors=true /warnaserror -f net8.0
working-directory: ./tests/Microsoft.Graph.DotnetCore.Core.Trimming
10 changes: 5 additions & 5 deletions src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Validators" Version="8.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.0.2" />
<PackageReference Include="Microsoft.IdentityModel.Validators" Version="8.0.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.11.3" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.11.3" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.11.3" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.12.2" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.11.20">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
16 changes: 7 additions & 9 deletions src/Microsoft.Graph.Core/Tasks/LargeFileUploadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace Microsoft.Graph
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph.Core.Models;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Abstractions.Serialization;
using Microsoft.Kiota.Serialization.Json;

/// <summary>
/// Task to help with resumable large file uploads.
Expand Down Expand Up @@ -63,7 +65,7 @@ public LargeFileUploadTask(IParsable uploadSession, Stream uploadStream, int max
/// <param name="uploadSession"><see cref="IParsable"/> to initialize an <see cref="IUploadSession"/> from</param>
/// <returns>A <see cref="IUploadSession"/> instance</returns>
/// <exception cref="NotImplementedException"></exception>
private IUploadSession ExtractSessionFromParsable(IParsable uploadSession)
internal static IUploadSession ExtractSessionFromParsable(IParsable uploadSession)
{
if (!uploadSession.GetFieldDeserializers().ContainsKey("expirationDateTime"))
throw new ArgumentException("The Parsable does not contain the 'expirationDateTime' property");
Expand All @@ -72,14 +74,10 @@ private IUploadSession ExtractSessionFromParsable(IParsable uploadSession)
if (!uploadSession.GetFieldDeserializers().ContainsKey("uploadUrl"))
throw new ArgumentException("The Parsable does not contain the 'uploadUrl' property");

var uploadSessionType = uploadSession.GetType();

return new UploadSession()
{
ExpirationDateTime = uploadSessionType.GetProperty("ExpirationDateTime").GetValue(uploadSession, null) as DateTimeOffset?,
NextExpectedRanges = uploadSessionType.GetProperty("NextExpectedRanges").GetValue(uploadSession, null) as List<string>,
UploadUrl = uploadSessionType.GetProperty("UploadUrl").GetValue(uploadSession, null) as string
};
// convert to local type as we don't have the type info for the upload session just that it implements IParsable
var uploadSessionStream = KiotaSerializer.SerializeAsStream(CoreConstants.MimeTypeNames.Application.Json, uploadSession, false);// just in case there's a backing store
var uploadSessionJsonNode = new JsonParseNode(JsonDocument.Parse(uploadSessionStream).RootElement);
return uploadSessionJsonNode.GetObjectValue(UploadSession.CreateFromDiscriminatorValue);
}

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions src/Microsoft.Graph.Core/Tasks/PageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace Microsoft.Graph
using System.Threading.Tasks;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Serialization;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

/*
Spec https://github.com/microsoftgraph/msgraph-sdk-design/blob/main/tasks/PageIteratorTask.md
Expand All @@ -21,7 +24,11 @@ namespace Microsoft.Graph
/// </summary>
/// <typeparam name="TEntity">The Microsoft Graph entity type returned in the result set.</typeparam>
/// <typeparam name="TCollectionPage">The Microsoft Graph collection response type returned in the collection response.</typeparam>
#if NET5_0_OR_GREATER
public class PageIterator<TEntity, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TCollectionPage> where TCollectionPage : IParsable, IAdditionalDataHolder, new()
#else
public class PageIterator<TEntity, TCollectionPage> where TCollectionPage : IParsable, IAdditionalDataHolder, new()
#endif
{
private IRequestAdapter _requestAdapter;
private TCollectionPage _currentPage;
Expand Down Expand Up @@ -364,7 +371,7 @@ public async Task ResumeAsync(CancellationToken token)
/// <exception cref="ArgumentException">Thrown when the object doesn't contain a collection inside it</exception>
private static List<TEntity> ExtractEntityListFromParsable(TCollectionPage parsableCollection)
{
return parsableCollection.GetType().GetProperty("Value")?.GetValue(parsableCollection, null) as List<TEntity> ?? throw new ArgumentException("The Parsable does not contain a collection property");
return typeof(TCollectionPage).GetProperty("Value")?.GetValue(parsableCollection, null) as List<TEntity> ?? throw new ArgumentException("The Parsable does not contain a collection property");
}

/// <summary>
Expand All @@ -375,7 +382,7 @@ private static List<TEntity> ExtractEntityListFromParsable(TCollectionPage parsa
/// <returns></returns>
private static string ExtractNextLinkFromParsable(TCollectionPage parsableCollection, string nextLinkPropertyName = "OdataNextLink")
{
var nextLinkProperty = parsableCollection.GetType().GetProperty(nextLinkPropertyName);
var nextLinkProperty = typeof(TCollectionPage).GetProperty(nextLinkPropertyName);
if (nextLinkProperty != null &&
nextLinkProperty.GetValue(parsableCollection, null) is string nextLinkString
&& !string.IsNullOrEmpty(nextLinkString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ public void ThrowsOnEmptyStream()
Assert.NotNull(exception);
}
[Fact]
public void ParsesUploadSessionWithoutReflection()
{

var uploadSession = new UploadSession
{
NextExpectedRanges = new List<string>() { "0-" },
UploadUrl = "http://localhost",
ExpirationDateTime = DateTimeOffset.Parse("2019-11-07T06:39:31.499Z")
};

var parsedSession = LargeFileUploadTask<TestDriveItem>.ExtractSessionFromParsable(uploadSession);

Assert.Equal(uploadSession.UploadUrl, parsedSession.UploadUrl);
Assert.Equal(uploadSession.ExpirationDateTime, parsedSession.ExpirationDateTime);
Assert.Equal(uploadSession.NextExpectedRanges.Count, parsedSession.NextExpectedRanges.Count);
Assert.Equal(uploadSession.NextExpectedRanges[0], parsedSession.NextExpectedRanges[0]);
}
[Fact]
public void AllowsVariableSliceSize()
{
byte[] mockData = new byte[1000000];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<InvariantGlobalization>true</InvariantGlobalization>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<PublishTrimmed>true</PublishTrimmed>
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Graph.Core\Microsoft.Graph.Core.csproj" />
<TrimmerRootAssembly Include="Microsoft.Graph.Core" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions tests/Microsoft.Graph.DotnetCore.Core.Trimming/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.Graph.DotnetCore.Core.Trimming;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
6 changes: 6 additions & 0 deletions tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.101", /* https://github.com/dotnet/maui/wiki/.NET-7-and-.NET-MAUI */
"rollForward": "major"
}
}

0 comments on commit 4b47b57

Please sign in to comment.