Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Fixed incorrect implementation of content type id best match #2582

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 67 additions & 4 deletions Core/OfficeDevPnP.Core/Extensions/FieldAndContentTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,12 +1809,75 @@ public static ContentTypeId BestMatch(this ContentTypeCollection contentTypes, s
var ctx = contentTypes.Context;
contentTypes.EnsureProperties(c => c.Include(ct => ct.Id));

var res = contentTypes.Where(c => c.Id.StringValue.StartsWith(contentTypeId, StringComparison.InvariantCultureIgnoreCase)).OrderBy(c => c.Id.StringValue.Length).FirstOrDefault();
if (res != null)
return BestMatch(contentTypeId, contentTypes);
}

/// <summary>
/// Searches for the content type with the closest match to the specified content type ID.
/// If the search finds two matches, the shorter ID is returned.
/// </summary>
/// <param name="contentTypes">Content type collection to search</param>
/// <param name="contentTypeId">Content type id for the content type to search</param>
/// <returns>Content type Id object or null if was not found</returns>
public static ContentTypeId BestMatch(this ContentTypeCollection contentTypes, ContentTypeId contentTypeId)
{
if (contentTypeId == null)
{
return res.Id;
throw new ArgumentNullException(nameof(contentTypeId));
}
return null;
return BestMatch(contentTypes, contentTypeId.StringValue);
}

internal static int CountCommonBytes(this ContentTypeId thisId, ContentTypeId id)
{
return thisId.CountCommonBytes(id.StringValue);
}

internal static int CountCommonBytes(this ContentTypeId thisId, string id)
{
string thisIdValue = thisId.StringValue.Substring(2).ToLower();
string otherIdValue = id.Substring(2).ToLower();

int index = 0;
while ((index * 2 + 1 < thisIdValue.Length) && ((index * 2 + 1 < otherIdValue.Length) && (thisIdValue[index * 2] == otherIdValue[index * 2]) && (thisIdValue[index * 2 + 1] == otherIdValue[index * 2 + 1])))
{
index++;
}
return index;
}

/// <summary>
/// Searches for the content type with the closest match to this content type id.
/// If the search finds two matches, the shorter ID is returned.
/// </summary>
/// <param name="contentTypeId">Content type id for the content type to search</param>
/// <param name="contentTypeCollection">Content type collection to search</param>
/// <returns>Content type Id object or null if was not found</returns>

public static ContentTypeId BestMatch(ContentTypeId contentTypeId, IEnumerable<ContentType> contentTypeCollection)
{
return BestMatch(contentTypeId.StringValue, contentTypeCollection);
}

private static ContentTypeId BestMatch(string contentTypeId, IEnumerable<ContentType> contentTypeCollection)
{
ContentTypeId bestMatch = null;
int bestMatchCommonBytes = 0;
foreach (ContentType contentType in contentTypeCollection)
{
int commonBytes = contentType.Id.CountCommonBytes(contentTypeId);
if (commonBytes > bestMatchCommonBytes)
{
bestMatch = contentType.Id;
bestMatchCommonBytes = commonBytes;
continue;
}
if ((commonBytes == bestMatchCommonBytes) && (contentType.Id.StringValue.Length < bestMatch.StringValue.Length))
{
bestMatch = contentType.Id;
}
}
return bestMatch;
}

/// <summary>
Expand Down