OData / OData/AspNetCoreOData

OData function no longer working as expected

Open
#444 17 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Jan 18, 2022.

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

Description

I have just updated a project to NET 6 and OData 8.0.6.. I have a function that is no longer working as expected.

Method Definition:

public virtual async Task<IActionResult> GetComparitiveTable(
    [FromODataUri] string cultureCode,
    ODataQueryOptions<ComparitiveLocalizableString> options)
{
    if (!CheckPermission(ReadPermission))
    {
        return Unauthorized();
    }
    else
    {
        int tenantId = GetTenantId();
        var connection = GetDisposableConnection();

        var query = connection.Query(x => x.TenantId == tenantId && (x.CultureCode == null || x.CultureCode == cultureCode))
		    .ToHashSet()
            .GroupBy(x => x.TextKey)
            .Select(grp => new ComparitiveLocalizableString
            {
                Key = grp.Key,
                InvariantValue = grp.First(x => x.CultureCode == null).TextValue,
                LocalizedValue = grp.FirstOrDefault(x => x.CultureCode == cultureCode) == null
                    ? string.Empty
                    : grp.First(x => x.CultureCode == cultureCode).TextValue
            })
            .AsQueryable();

        var results = options.ApplyTo(query, IgnoreQueryOptions);
        var response = await Task.FromResult((results as IQueryable<ComparitiveLocalizableString>).ToHashSet());
        return Ok(response);
    }
}

Note that an entity is queried (LocalizableString) and a complex type is returned (ComparitiveLocalizableString).

Definition of LocalizableString:

public class LocalizableString : TenantEntity<Guid>
{
	public string CultureCode { get; set; }

	public string TextKey { get; set; }

	public string TextValue { get; set; }
}

Definition of ComparitiveLocalizableString:

public class ComparitiveLocalizableString
{
    public string Key { get; set; }

    public string InvariantValue { get; set; }

    public string LocalizedValue { get; set; }
}

OData Function Setup:

var getComparitiveTableFunction = builder.EntityType<LocalizableString>().Collection.Function("GetComparitiveTable");
getComparitiveTableFunction.Parameter<string>("cultureCode");
getComparitiveTableFunction.Returns<IActionResult>();

URL:

${this.apiUrl}/Default.GetComparitiveTable(cultureCode='${this.cultureCode}')

Error:

Microsoft.OData.ODataException: The key value (Default.GetComparitiveTable(cultureCode='en-US')) from request is not valid. The key value should be format of type 'Edm.Guid'.

I'm not trying to query by the entity's key, but by it's CultureCode, as I have specified in the OData function definition (and which has been working fine for several versions of AspNetCoreOData until now).

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.