OData / OData/AspNetCoreOData

Select operator on computed properties doesn't go through the DTO defined getter

Open
#934 4 comments 0 reactions 1 assignee View on GitHub

@habbes is already working on this.

Since May 23, 2023.

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

Description

When running an OData $select operator on a property which has a custom getter which concats data from several properties, the wrong value is returned.

An example of a failure reproduced locally:

Calling the API GET machinese with a select operator to return only machineTags:
localhost:1059/api/machines?$filter=id eq '0002aabb5a73e0648fc1f97448e033f3fd2dd25e'&$select=machineTags
This action returns no machine tags.

However, when requesting for the specific machine or all machines without the select operator - there are tags showing.
This tags are part of another property which is appended to the machine tags in the DTO getter method for MachineTags

Class definitions.

Table("Machines")]
public class MachineDbEntity
{
...
        public List<TagsDbEntity> Tags{ get; set; }

        public string MachineGroup { get; set; }
...
}
public class MachineDto
{
...
        private List<string> m_machineTags;

        public List<string> MachineTags
        {
            get
            {
                if (OnPremTag != null && !m_machineTags.Any(tag => tag.Equals(OnPremTag, StringComparison.OrdinalIgnoreCase)))
                {
                    m_machineTags.Add(OnPremTag);
                }

                return m_machineTags;
            }

            set => m_machineTags = value;
        }

        public string OnPremTag { get; set; }
...
}

Query used:

return machineDbEntities.Select(machine => new MachineDto
    {
        ...
        MachineTags = machine.Tags.Select(tag => tag.TagName).Where(t => t != null).ToList()
        OnPremTag = machine.MachineGroup
        ...
    };

What I've tried testing and did not work:

  • Adding the OnPremTag to the select operator
  • Changing the MachineTags property to IEnumerable
  • Changing getter/setter login in MachineDto

FYI - @habbes

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.