diff --git a/.github/workflows/validatePullRequest.yml b/.github/workflows/validatePullRequest.yml index 7311379a3..37bd58919 100644 --- a/.github/workflows/validatePullRequest.yml +++ b/.github/workflows/validatePullRequest.yml @@ -65,8 +65,8 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.x + dotnet-version: 9.x - name: Validate Trimming warnings - run: dotnet publish -c Release -r win-x64 /p:TreatWarningsAsErrors=true /warnaserror -f net8.0 + run: dotnet publish -c Release -r win-x64 /p:TreatWarningsAsErrors=true /warnaserror -f net9.0 working-directory: ./tests/Microsoft.Graph.DotnetCore.Core.Trimming \ No newline at end of file diff --git a/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj b/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj index 719f2d253..465623ae7 100644 --- a/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj +++ b/src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj @@ -35,6 +35,7 @@ true true true + true True README.md NU5048;NETSDK1202 @@ -62,16 +63,16 @@ - - + + - - + + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContent.cs b/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContent.cs index 1679a9649..cc782d4a6 100644 --- a/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContent.cs +++ b/src/Microsoft.Graph.Core/Requests/Content/BatchRequestContent.cs @@ -17,6 +17,7 @@ namespace Microsoft.Graph using System.Threading; using System.Threading.Tasks; using Microsoft.Kiota.Abstractions; + using Microsoft.Kiota.Abstractions.Authentication; /// /// A implementation to handle json batch requests. @@ -71,7 +72,7 @@ public BatchRequestContent(IRequestAdapter requestAdapter, params BatchRequestSt if (batchRequestSteps == null) throw new ArgumentNullException(nameof(batchRequestSteps)); - if (batchRequestSteps.Count() > CoreConstants.BatchRequest.MaxNumberOfRequests) + if (batchRequestSteps.Length > CoreConstants.BatchRequest.MaxNumberOfRequests) throw new ArgumentException(string.Format(ErrorConstants.Messages.MaximumValueExceeded, "Number of batch request steps", CoreConstants.BatchRequest.MaxNumberOfRequests)); this.Headers.ContentType = new MediaTypeHeaderValue(CoreConstants.MimeTypeNames.Application.Json); @@ -87,7 +88,12 @@ public BatchRequestContent(IRequestAdapter requestAdapter, params BatchRequestSt AddBatchRequestStep(requestStep); } - this.RequestAdapter = requestAdapter ?? throw new ArgumentNullException(nameof(requestAdapter)); + // as we only use the adapter to serialize request using the `ConvertToNativeRequestAsync` interface, + // we don't want to make extra request to the authentication provider as the request does not need the authentication header. + this.RequestAdapter = new BaseGraphRequestAdapter(new AnonymousAuthenticationProvider()) + { + BaseUrl = requestAdapter.BaseUrl + }; } /// diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs index fe798971e..4ce0801fc 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs @@ -181,7 +181,7 @@ public void BatchRequestContent_RemoveBatchRequestStepWithIdForNonExistingId() Assert.False(isSuccess); Assert.True(batchRequestContent.BatchRequestSteps.Count.Equals(2)); - Assert.Same(batchRequestStep2.DependsOn.First(), batchRequestContent.BatchRequestSteps["2"].DependsOn.First()); + Assert.Same(batchRequestStep2.DependsOn[0], batchRequestContent.BatchRequestSteps["2"].DependsOn[0]); } [Fact] @@ -311,6 +311,29 @@ public async System.Threading.Tasks.Task BatchRequestContent_GetBatchRequestCont Assert.Equal(expectedContent, requestContent); } + [Fact] + public async System.Threading.Tasks.Task BatchRequestContent_GetBatchRequestContentFromRequestInformationDoesNotAddAuthHeaderAsync() + { + BatchRequestContent batchRequestContent = new BatchRequestContent(client); + RequestInformation requestInformation = new RequestInformation() { HttpMethod = Method.GET, UrlTemplate = REQUEST_URL }; + await batchRequestContent.AddBatchRequestStepAsync(requestInformation, "2"); + + string requestContent; + // We get the contents of the stream as string for comparison. + using (Stream requestStream = await batchRequestContent.GetBatchRequestContentAsync()) + using (StreamReader reader = new StreamReader(requestStream)) + { + requestContent = await reader.ReadToEndAsync(); + } + + string expectedContent = "{\"requests\":[{\"id\":\"2\",\"url\":\"/me\",\"method\":\"GET\"}]}"; //Auth Header Absent. + + Assert.NotNull(requestContent); + Assert.IsType(batchRequestContent.RequestAdapter); + Assert.True(batchRequestContent.BatchRequestSteps.Count.Equals(1)); + Assert.Equal(expectedContent, requestContent); + } + [Fact] public async System.Threading.Tasks.Task BatchRequestContent_GetBatchRequestContentSupportsNonJsonPayloadAsync() { @@ -331,7 +354,7 @@ public async System.Threading.Tasks.Task BatchRequestContent_GetBatchRequestCont string requestContent; // we do this to get a version of the json that is indented using (Stream requestStream = await batchRequestContent.GetBatchRequestContentAsync()) - using (JsonDocument jsonDocument = JsonDocument.Parse(requestStream)) + using (JsonDocument jsonDocument = await JsonDocument.ParseAsync(requestStream)) { requestContent = JsonSerializer.Serialize(jsonDocument.RootElement, new JsonSerializerOptions() { WriteIndented = true }); } @@ -409,7 +432,7 @@ public async System.Threading.Tasks.Task BatchRequestContent_GetBatchRequestCont string requestContent; // we do this to get a version of the json that is indented using (Stream requestStream = await batchRequestContent.GetBatchRequestContentAsync()) - using (JsonDocument jsonDocument = JsonDocument.Parse(requestStream)) + using (JsonDocument jsonDocument = await JsonDocument.ParseAsync(requestStream)) { requestContent = JsonSerializer.Serialize(jsonDocument.RootElement, new JsonSerializerOptions() { WriteIndented = true }); } @@ -532,7 +555,7 @@ public void BatchRequestContent_AddBatchRequestStepWithHttpRequestMessageToBatch // Assert var exception = Assert.Throws(() => batchRequestContent.AddBatchRequestStep(extraHttpRequestMessage));//Assert we throw exception on excess add - //Assert.Equal(ErrorConstants.Codes.MaximumValueExceeded, exception.Error.Code); + Assert.Equal(string.Format(ErrorConstants.Messages.MaximumValueExceeded, "Number of batch request steps", CoreConstants.BatchRequest.MaxNumberOfRequests), exception.Message); Assert.NotNull(batchRequestContent.BatchRequestSteps); Assert.True(batchRequestContent.BatchRequestSteps.Count.Equals(CoreConstants.BatchRequest.MaxNumberOfRequests)); } @@ -612,7 +635,7 @@ public async Task BatchRequestContent_AddBatchRequestStepWithBaseRequestToBatchR var exception = await Assert.ThrowsAsync(() => batchRequestContent.AddBatchRequestStepAsync(extraRequestInformation)); // Assert - //Assert.Equal(ErrorConstants.Codes.MaximumValueExceeded, exception.Error.Code); + Assert.Equal(string.Format(ErrorConstants.Messages.MaximumValueExceeded, "Number of batch request steps", CoreConstants.BatchRequest.MaxNumberOfRequests), exception.Message); Assert.NotNull(batchRequestContent.BatchRequestSteps); Assert.True(batchRequestContent.BatchRequestSteps.Count.Equals(CoreConstants.BatchRequest.MaxNumberOfRequests)); } diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Trimming/Microsoft.Graph.DotnetCore.Core.Trimming.csproj b/tests/Microsoft.Graph.DotnetCore.Core.Trimming/Microsoft.Graph.DotnetCore.Core.Trimming.csproj index 928343674..3bfa7a49d 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Trimming/Microsoft.Graph.DotnetCore.Core.Trimming.csproj +++ b/tests/Microsoft.Graph.DotnetCore.Core.Trimming/Microsoft.Graph.DotnetCore.Core.Trimming.csproj @@ -1,7 +1,7 @@ Exe - net8.0 + net9.0 enable enable true diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json b/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json index d251ac4e7..b81865078 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json +++ b/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "8.0.101", /* https://github.com/dotnet/maui/wiki/.NET-7-and-.NET-MAUI */ + "version": "9.0.102", /* https://github.com/dotnet/maui/wiki/.NET-7-and-.NET-MAUI */ "rollForward": "major" } }