Skip to content

Commit

Permalink
fix(toolkit): fix expand properties name resolving for lookup attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ttkoma committed Feb 5, 2021
1 parent 419f7d4 commit e39efca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
35 changes: 29 additions & 6 deletions CrmNx.Xrm.Toolkit/Query/QueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,20 @@ private static bool BuildExpandOptionValue(IWebApiMetadataService metadata, stri

if (expand.ColumnSet.Columns.Any() && !expand.ColumnSet.AllColumns)
{
terms.Add($"{navPropertyName}($select={string.Join(",", expand.ColumnSet.Columns)})");
string selectOptionForExpand;

// relationship.ReferencedEntity - EntityLogicalName for expand

if (relationship != null)
{
BuildSelectOptionValue(metadata, relationship.ReferencedEntity, expand.ColumnSet, out selectOptionForExpand);
}
else
{
selectOptionForExpand = string.Join(",", expand.ColumnSet.Columns);
}

terms.Add($"{navPropertyName}($select={selectOptionForExpand})");
continue;
}
else if (relationship != null)
Expand Down Expand Up @@ -262,16 +275,31 @@ private static bool BuildOrderOptionValue(IWebApiMetadataService webApiMetadata,
return true;
}

/// <summary>
/// Return false if result is string.Empty
/// </summary>
/// <param name="webApiMetadata"></param>
/// <param name="entityLogicalName"></param>
/// <param name="columnSet"></param>
/// <param name="selectOptionValue"></param>
/// <returns></returns>
private static bool BuildSelectOptionValue(IWebApiMetadataService webApiMetadata, string entityLogicalName,
in ColumnSet columnSet, out string selectOptionValue)
{
selectOptionValue = string.Empty;

if (columnSet != null && columnSet.AllColumns)
{
// Not property present - need return AllFields
return false;
}

var entityMd = webApiMetadata.GetEntityMetadata(entityLogicalName);

if (columnSet is null && entityMd != null)
{
selectOptionValue = entityMd.PrimaryIdAttribute;
// Only PrimaryIdAttribute
return true;
}

Expand All @@ -286,11 +314,6 @@ private static bool BuildSelectOptionValue(IWebApiMetadataService webApiMetadata
return true;
}

if (columnSet != null && columnSet.AllColumns)
{
return false;
}

selectOptionValue = entityMd?.PrimaryIdAttribute;
return entityMd?.PrimaryIdAttribute != null;
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/UnitTests/Query/QueryOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ public void GetQueryString_When_Expand_Lookup_Then_Query_IsValid_Test()
var query = options.BuildQueryString(metadata, "account");


WebUtility.UrlDecode(query).Should()
.Be("?$expand=createdby($select=domainname,businessunitid),primarycontactid($select=contactid)");
var decodedQuery = WebUtility.UrlDecode(query);

decodedQuery.Should()
.Be("?$expand=createdby($select=domainname,_businessunitid_value),primarycontactid($select=contactid)");

query.Should()
.Be("?$expand=createdby($select%3Ddomainname,businessunitid),primarycontactid($select%3Dcontactid)");
.Be("?$expand=createdby($select%3Ddomainname,_businessunitid_value),primarycontactid($select%3Dcontactid)");
}

[Fact()]
Expand Down

0 comments on commit e39efca

Please sign in to comment.