Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System.Linq;
using Microsoft.AspNetCore.OData.Abstracts;
using Microsoft.AspNetCore.OData.Extensions;
using Microsoft.AspNetCore.OData.Query.Expressions;
using Microsoft.AspNetCore.OData.Query.Validator;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -25,6 +26,8 @@ public static ODataQuerySettings GetODataQuerySettings(this ODataQueryContext co
returnSettings.CopyFrom(settings);
}

returnSettings.TimeZone ??= context.Request.GetTimeZoneInfo();
Copy link
Member

Choose a reason for hiding this comment

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

Would you like to place this line of code before the copyFrom during the initialization of returnSettings?

The change you've suggested will always override the copyFrom action. Please note that null is a valid value, and users might choose to set returnSettings.TimeZone to null.


return returnSettings;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//-----------------------------------------------------------------------------
// <copyright file="ODataQueryContextExtensionsTests.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace Microsoft.AspNetCore.OData.Tests.Query.Query;

public class ODataQueryContextExtensionsTests
{
[Fact]
public void GetODataQuerySettings_WithContext_SetsTimeZone()
{
// Arrange
ServiceCollection services = new ServiceCollection();
services.AddLogging().AddControllers().AddOData();
IServiceProvider serviceProvider = services.BuildServiceProvider();

Mock<HttpRequest> mockHttpRequest = new Mock<HttpRequest>();
Mock<HttpContext> mockHttpContext = new Mock<HttpContext>();
mockHttpContext.SetupGet(x => x.RequestServices).Returns(serviceProvider);
mockHttpRequest.Setup(x => x.HttpContext)
.Returns(mockHttpContext.Object);

ODataQueryContext context = new ODataQueryContext
{
Request = mockHttpRequest.Object,
};

// Act
ODataQuerySettings querySettings = context.GetODataQuerySettings();

// Assert
Assert.NotNull(querySettings.TimeZone);
}
}