OData / OData/AspNetCoreOData

$apply=groupby cannot return correct propertyName with JsonProperty

Open
#990 0 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Jul 18, 2023.

bug investigating
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Assemblies affected

<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.OData.NewtonsoftJson" Version="8.2.0" />

Describe the bug
I'm trying to use the $apply=grouby((***)) to deal with distinct value. But I found the query and response don't follow the JsonProperty.

Reproduce steps
As an example, my server just return such JSON Object [{"properties":{"id":1,"name":"a"}},{"properties":{"id":2,"name":"b"}}] in API /employees. And while I apply with OData query, the results are below:

  1. GET /employees?$expand=properties($select=name)

The result is expected. It would return [{"properties":{"name":"a"}},{"properties":{"name":"b"}}]

  1. GET /employees?$apply=groupby((properties/name))

The result is unexpected. It would return [{"Properties":{"Name":"a"}},{"Properties":{"Name":"b"}}]
It doesn't follow the JsonProperty name in lower case "properties" and "name".

Data Model

public class EmployeeResult
{
    [JsonProperty("properties")]
    public EmployeeModel Properties { get; set; }
}

public class EmployeeModel
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
}

EDM (CSDL) Model
I'm using ODataQueryOptions<>.ApplyTo(), so can't get any $metadata successfully.
The controller code is very simple:

    [HttpGet]
    public ActionResult Get(ODataQueryOptions<EmployeeResult> options)
    {
        IEnumerable<EmployeeModel> models = EmployeeService.GetEmployees();
        var res = options.ApplyTo(EmployeeService.GetEmployees()
            .Select(employee => new EmployeeResult { Properties = employee})
            .AsQueryable());

        return new JsonResult(res);
    }

Request/Response
Already describe in Reproduce steps

Expected behavior
The result of GET /employees?$apply=groupby((properties/name)) should be [{"properties":{"name":"a"}},{"properties":{"name":"b"}}]

Screenshots
image

use with echo in command to add a newline after response body

Additional context
N/A

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.