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

Using DateOnly in a groupby with $apply results in object reference not set #1327

Open
computerwiz7791 opened this issue Oct 21, 2024 · 0 comments
Assignees
Labels
bug Something isn't working P2

Comments

@computerwiz7791
Copy link

Assemblies affected
ASP.NET Core OData 9.x

Describe the bug
When querying a data model that contains a DateOnly property, where I want to group by that property to get summary or just the distinct records, I get an error saying that the object reference not set to an instance of an object. When I change this to use DateTime the problem doesn't exist. Currently using DateOnly as EF 8 now supports DateOnly (date database field).

Project Code To Reproduce


using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.OData.ModelBuilder;

var builder = WebApplication.CreateBuilder(args);

var modelBuilder = new ODataConventionModelBuilder();
var timeRecord = modelBuilder.EntitySet<TimeRecord>(nameof(TimeRecord)).EntityType;
timeRecord.Select().Expand(6).OrderBy().Filter().Count().Page();

// DI
builder.Services
    .AddControllers()
    .AddOData(options => options.EnableQueryFeatures(1000).AddRouteComponents(modelBuilder.GetEdmModel()));

// Configure
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers();});
app.Run();

public class TimeRecord
{
    public long Id { get; set; }
    public DateOnly PeriodDate { get; set; }
    public decimal Hours { get; set; }
    public string Employee { get; set; }
}

public class TimeRecordController : ODataController
{
    [EnableQuery]
    public IQueryable Get()
    {
        return (new List<TimeRecord>()
        {
            new TimeRecord(){Id = 1, Employee = "John", PeriodDate = DateOnly.Parse("2024-10-01"), Hours = 35},
            new TimeRecord(){Id = 2, Employee = "John", PeriodDate = DateOnly.Parse("2024-10-08"), Hours = 15},
            new TimeRecord(){Id = 3, Employee = "Ellen", PeriodDate = DateOnly.Parse("2024-10-08"), Hours = 25},
            new TimeRecord(){Id = 4, Employee = "John", PeriodDate = DateOnly.Parse("2024-10-15"), Hours = 30},
            new TimeRecord(){Id = 5, Employee = "John", PeriodDate = DateOnly.Parse("2024-10-22"), Hours = 27},
        }).AsQueryable();
    }
}

Request
.../TimeRecord?$apply=groupby((periodDate))

Expected behavior
The expected behavior would be an odata list of the groupby property in this case:
[ { "PeriodDate": "2024-10-01" }, { "PeriodDate": "2024-10-08" }, { "PeriodDate": "2024-10-15" }, { "PeriodDate": "2024-10-22" } ]

Screenshots
image

Additional context
Making the following change to ExpressionBinderBase, solves the problem listed above.
image

@computerwiz7791 computerwiz7791 added the bug Something isn't working label Oct 21, 2024
@WanjohiSammy WanjohiSammy self-assigned this Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P2
Projects
None yet
Development

No branches or pull requests

2 participants