OData/AspNetCoreOData

Issue with Object Expansion After Upgrading AspNetCoreOData

Opened this issue · 4 comments

Issue with Object Expansion After Upgrading AspNetCoreOData

Background
We were previously using AspNetCoreOData v7.14.0 for our API services. In this version, objects within the API response were expanding by default, facilitated by the use of the ComplexType attribute.

Problem
After upgrading our services to .Net8, we updgraded the AspNetCoreOData library to v7.20.0, we encountered an issue where objects were no longer expanding automatically in the API responses. The key points of this problem are:

  1. Object Expansion: In the earlier version (v7.14.0), objects expanded by default in the API response.
  2. ComplexType Attribute: We utilized the ComplexType attribute to ensure default expansion of objects.
  3. Library Upgrade: Post-upgrade to v7.20.0, the ComplexType attribute ceased functioning correctly. The bindings of the Assembly were not created, resulting in the attribute being ineffective.

Attempted Solution
To address this issue, we attempted the following:

  1. Removal of ComplexType Attribute: We removed the ComplexType attribute, which allowed the system to function without errors. However, this change led to objects not expanding by default in the API response.

Current Situation
While the removal of the ComplexType attribute resolved the immediate issue of assembly bindings, it introduced a new problem: the default expansion of objects in the API response is no longer working.

Request for Assistance
We are seeking guidance on how to achieve the default expansion of objects in the API response with AspNetCoreOData v7.20.0, given the current constraints. Specifically, we need:

  1. Alternative to ComplexType Attribute: Suggestions for alternative approaches or attributes that can replace ComplexType and enable default object expansion.
  2. Configuration Adjustments: Recommendations for any configuration changes that might restore the default expansion behavior.
  3. Code Examples: If possible, code snippets or examples demonstrating the correct implementation for default object expansion in the upgraded version.

We appreciate any insights or solutions that can help resolve this issue efficiently.

Please refer to this:

[ComplexType]
[Owned]
[DataContract(Name = "Customer")]
public class Customer
{
[DataMember(Name = "id")] public long Id { get; set; }
[DataMember(Name = "name")] public string Name { get; set; }
[DataMember(Name = "type")] public EnumDetail Type { get; set; }
[DataMember(Name = "status")] public EnumDetail Status { get; set; }
}

Few comments.

  1. Using the ComplexType mechanism as a means to force expansion is a hacky approach: it was never meant for that, so I think you were doing it poorly even before the upgrade
  2. You don't need to override each property name using [DataMember] to achieve camelCasing in the results: just configure the json serializer to do that. This will save you a ton of error-prone hardcoding.
  3. You should be able to use the Expand() method in your EDM building logic with the SelectExpandType.Automatic setting to always expand properties for a give type like this:
builder.EntitySet<Customer>("Customers").EntityType.Expand(SelectExpandType.Automatic);

image

Thank you for the help. It's working now.

Kindly assist us with another issue here.

Subject: Issue with Filtering in AspNetCoreOData After Expanding the Object

Problem Description:

We are currently using AspNetCoreOData version 7.20.0 and have encountered an issue with filtering after expanding objects. The problem manifests as a discrepancy in the data returned for specific OData queries, leading to incorrect results.

OData Query 1:

{{Base_URL}}/finance/odata/v2/payments/PaymentOperation.v2.GetByVendorType?vendor_type='publisher'&$filter=id eq 28965 and vendor/id eq 128493&$count=true&orderby=paid_at desc&$expand=bills($expand=offer;$count=true)

Response :
{
    "@odata.count": 1,
    "value": [
        {
            "id": 28965,
            "paid_at": "2023-10-10T00:11:05.113Z",
            "amount": 700.0000,
            "vendor": {
                "id": 128493
            },
            "bills@odata.count": 4,
            "bills": [
                {
                    "paid_amount": 300.0000,
                    "offer": null
                },
                {
                    "paid_amount": 100.0000,
                    "offer": null
                },
                {
                    "paid_amount": 200.0000,
                    "offer": {
                        "campaign_name": "Default",
                        "completed_at": null,
                        "finance_settled_at": null
                    }
                },
                {
                    "paid_amount": 100.0000,
                    "offer": {
                        "campaign_name": "Default",
                        "completed_at": "2023-10-09T03:09:33Z",
                        "finance_settled_at": "2023-10-09T03:09:33Z"
                    }
                }
            ]
        }
    ]
}

OData Query 2:

{{Base_URL}}/finance/odata/v2/payments/PaymentOperation.v2.GetByVendorType?vendor_type='publisher'&$filter=id eq 28965 and vendor/id eq 128493&$count=true&orderby=paid_at desc&$expand=bills($expand=offer;$count=true;$filter=offer/campaign_name eq 'Default')

Response :
{
    "@odata.count": 1,
    "value": [
        {
            "id": 28965,
            "paid_at": "2023-10-10T00:11:05.113Z",
            "amount": 700.0000,
            "vendor": {
                "id": 128493
            },
            "bills@odata.count": 2,
            "bills": [
                {
                    "paid_amount": 200.0000,
                    "offer": {
                        "campaign_name": "Default",
                        "completed_at": null,
                        "finance_settled_at": null
                    }
                },
                {
                    "paid_amount": 100.0000,
                    "offer": {
                        "campaign_name": "Default",
                        "completed_at": "2023-10-09T03:09:33Z",
                        "finance_settled_at": "2023-10-09T03:09:33Z"
                    }
                }
            ]
        }
    ]
}

Issue:

In the first query, we apply the filter id eq 28965 and vendor/id eq 128493, which correctly returns four records in the bills object with a total sum of 700 of paid_amount in amount field.

However, in the second query, when we add a filter to the expanded offer object ($expand=bills($expand=offer;$count=true;$filter=offer/campaign_name eq 'Default')), it returns only two records in the bills object. Despite this, the amount field at the root level incorrectly remains 700 instead of 300, reflecting the sum of the filtered records.

Request for Assistance:

We seek guidance on how to correctly calculate and display the amount field in the API response when using AspNetCoreOData v7.20.0, given the current constraints. Specifically, we need:

  1. Any alternative options or approaches to achieve the desired result.
  2. Code examples or snippets demonstrating the correct implementation for object expansion and filtering.

We appreciate any insights or solutions that can help resolve this issue efficiently.

@PrachiKakaiya please avoid reopening an existing issue to ask a different question, even if it is a followup. Instead, move the new question to a new issue and just add a mention of your previous one if you want to relate them.

Also, it seems to me like your second question is a better fit for the Discussions area as it doesn't appear to be a bug: you are just looking for guidance on how to create the desired query.