Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1365 #1369 prevent crash on image and location columns #1411

Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion src/sdk/PnP.Core.Test/SharePoint/ListItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,29 @@ public async Task RegularRestUpdateTest()
}
}


[TestMethod]
public async Task ImageAndLocationFieldLoadListDataAsStreamTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
var myList = await context.Web.Lists.GetByTitleAsync("WithImageAndLocation", p => p.Items,
p => p.Fields.QueryProperties(p => p.InternalName, p => p.FieldTypeKind, p => p.TypeAsString, p => p.Title));

await myList.LoadListDataAsStreamAsync(new RenderListDataOptions { });

var item = myList.Items.AsRequested().First();
var lf = item.Values.TryGetValue("LinkFilename", out object lfval);
var loc = item.Values["TestLocation"] as IFieldLocationValue;
var image = item.Values["TestImage"] as IFieldThumbnailValue;

Assert.AreEqual(image.FileName, "river.jpg");
Assert.AreEqual(loc.Latitude, (double)-37.8099m);
}
}


[TestMethod]
public async Task RegularFieldCsomTest()
{
Expand Down Expand Up @@ -3732,7 +3755,7 @@ public async Task ListItemAsFilePropertiesTest()
{
//TestCommon.Instance.Mocking = false;
(string parentLibraryName, _, string documentUrl) = await TestAssets.CreateTestDocumentAsync(0);

try
{
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"648708a1-300e-3000-032a-d8e289f2612c","SPClientServiceRequestDuration":"9","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RegionalSettings\u0022:{\u0022TimeZone\u0022:{\u0022Description\u0022:\u0022(UTC-08:00) Pacific Time (US and Canada)\u0022,\u0022Id\u0022:13,\u0022Information\u0022:{\u0022Bias\u0022:480,\u0022DaylightBias\u0022:-60,\u0022StandardBias\u0022:0}},\u0022AdjustHijriDays\u0022:0,\u0022AlternateCalendarType\u0022:0,\u0022AM\u0022:\u0022AM\u0022,\u0022CalendarType\u0022:1,\u0022Collation\u0022:25,\u0022CollationLCID\u0022:2070,\u0022DateFormat\u0022:0,\u0022DateSeparator\u0022:\u0022/\u0022,\u0022DecimalSeparator\u0022:\u0022.\u0022,\u0022DigitGrouping\u0022:\u00223;0\u0022,\u0022FirstDayOfWeek\u0022:0,\u0022FirstWeekOfYear\u0022:0,\u0022IsEastAsia\u0022:false,\u0022IsRightToLeft\u0022:false,\u0022IsUIRightToLeft\u0022:false,\u0022ListSeparator\u0022:\u0022,\u0022,\u0022LocaleId\u0022:1033,\u0022NegativeSign\u0022:\u0022-\u0022,\u0022NegNumberMode\u0022:1,\u0022PM\u0022:\u0022PM\u0022,\u0022PositiveSign\u0022:\u0022\u0022,\u0022ShowWeeks\u0022:false,\u0022ThousandSeparator\u0022:\u0022,\u0022,\u0022Time24\u0022:false,\u0022TimeMarkerPosition\u0022:0,\u0022TimeSeparator\u0022:\u0022:\u0022,\u0022WorkDayEndHour\u0022:1020,\u0022WorkDays\u0022:62,\u0022WorkDayStartHour\u0022:480},\u0022Id\u0022:\u00224443e8fa-ce60-4ce4-ab87-d0f378185a1b\u0022,\u0022Url\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"648708a1-1058-3000-032a-d3cb1d24c161","SPClientServiceRequestDuration":"11","X-SharePointHealthScore":"0","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022GroupId\u0022:\u00224a873bac-9841-4a22-ad69-ee47f25d01ca\u0022,\u0022Id\u0022:\u0022d54a8d16-b96c-492c-a4cb-7a1906c38058\u0022}"}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"a38708a1-2089-3000-032a-d91fd9043f33","SPClientServiceRequestDuration":"607","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022d\u0022:{\u0022results\u0022:[{\u0022__metadata\u0022:{\u0022id\u0022:\u0022f548d02f-b86e-43e9-9a6f-57c0e291f278\u0022,\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)\u0022,\u0022etag\u0022:\u0022\\\u00224\\\u0022\u0022,\u0022type\u0022:\u0022SP.Data.WithImageAndLocationItem\u0022},\u0022FirstUniqueAncestorSecurableObject\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/FirstUniqueAncestorSecurableObject\u0022}},\u0022RoleAssignments\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/RoleAssignments\u0022}},\u0022AttachmentFiles\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/AttachmentFiles\u0022}},\u0022ContentType\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/ContentType\u0022}},\u0022GetDlpPolicyTip\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/GetDlpPolicyTip\u0022}},\u0022FieldValuesAsHtml\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/FieldValuesAsHtml\u0022}},\u0022FieldValuesAsText\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/FieldValuesAsText\u0022}},\u0022FieldValuesForEdit\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/FieldValuesForEdit\u0022}},\u0022File\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/File\u0022}},\u0022Folder\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/Folder\u0022}},\u0022LikedByInformation\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/LikedByInformation\u0022}},\u0022ParentList\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/ParentList\u0022}},\u0022Properties\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/Properties\u0022}},\u0022Versions\u0022:{\u0022__deferred\u0022:{\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)/Versions\u0022}},\u0022FileSystemObjectType\u0022:0,\u0022Id\u0022:1,\u0022ServerRedirectedEmbedUri\u0022:null,\u0022ServerRedirectedEmbedUrl\u0022:\u0022\u0022,\u0022ContentTypeId\u0022:\u00220x01010051DF58F8CC0D084EBCF152E0693EB788\u0022,\u0022OData__ColorTag\u0022:null,\u0022ComplianceAssetId\u0022:null,\u0022Title\u0022:null,\u0022TestImage\u0022:\u0022{\\\u0022fileName\\\u0022:\\\u0022river.jpg\\\u0022,\\\u0022serverRelativeUrl\\\u0022:\\\u0022/sites/DNTest/SiteAssets/Lists/dfe5e305-80fb-4578-a521-496b47927628/river.jpg\\\u0022,\\\u0022id\\\u0022:\\\u0022cb711ba8-418f-4072-970b-a1afa5f2f624\\\u0022,\\\u0022serverUrl\\\u0022:\\\u0022https://hsftrms.sharepoint.com\\\u0022,\\\u0022thumbnailRenderer\\\u0022:null}\u0022,\u0022TestLocation\u0022:\u0022{\\\u0022EntityType\\\u0022:\\\u0022LocalBusiness\\\u0022,\\\u0022LocationSource\\\u0022:\\\u0022Bing\\\u0022,\\\u0022LocationUri\\\u0022:\\\u0022https://www.bingapis.com/api/v6/localbusinesses/YN3724x3883274883924636278\\\u0022,\\\u0022UniqueId\\\u0022:\\\u0022https://www.bingapis.com/api/v6/localbusinesses/YN3724x3883274883924636278\\\u0022,\\\u0022DisplayName\\\u0022:\\\u0022Melbourne Marriott Hotel\\\u0022,\\\u0022Address\\\u0022:{\\\u0022Street\\\u0022:\\\u0022Corner Exhibition \u0026 Lonsdale Streets\\\u0022,\\\u0022City\\\u0022:\\\u0022Melbourne\\\u0022,\\\u0022State\\\u0022:\\\u0022Victoria\\\u0022,\\\u0022CountryOrRegion\\\u0022:\\\u0022Australia\\\u0022,\\\u0022PostalCode\\\u0022:\\\u00223000\\\u0022},\\\u0022Coordinates\\\u0022:{\\\u0022Latitude\\\u0022:-37.8099,\\\u0022Longitude\\\u0022:144.969}}\u0022,\u0022CountryOrRegion\u0022:\u0022Australia\u0022,\u0022State\u0022:\u0022Victoria\u0022,\u0022City\u0022:\u0022Melbourne\u0022,\u0022PostalCode\u0022:\u00223000\u0022,\u0022Street\u0022:\u0022Corner Exhibition \u0026 Lonsdale Streets\u0022,\u0022GeoLoc\u0022:{\u0022__metadata\u0022:{\u0022type\u0022:\u0022SP.FieldGeolocationValue\u0022},\u0022Altitude\u0022:0,\u0022Latitude\u0022:-37.8099,\u0022Longitude\u0022:144.969,\u0022Measure\u0022:0},\u0022DispName\u0022:\u0022Melbourne Marriott Hotel\u0022,\u0022SecondLocation\u0022:\u0022{\\\u0022EntityType\\\u0022:\\\u0022LocalBusiness\\\u0022,\\\u0022LocationSource\\\u0022:\\\u0022Bing\\\u0022,\\\u0022LocationUri\\\u0022:\\\u0022https://www.bingapis.com/api/v6/localbusinesses/YN2000x670149153\\\u0022,\\\u0022UniqueId\\\u0022:\\\u0022https://www.bingapis.com/api/v6/localbusinesses/YN2000x670149153\\\u0022,\\\u0022DisplayName\\\u0022:\\\u0022Paris France Hotel\\\u0022,\\\u0022Address\\\u0022:{\\\u0022Street\\\u0022:\\\u002272 Rue De Turbigo\\\u0022,\\\u0022City\\\u0022:\\\u0022Paris\\\u0022,\\\u0022State\\\u0022:\\\u0022\\u00cele-de-France\\\u0022,\\\u0022CountryOrRegion\\\u0022:\\\u0022France\\\u0022,\\\u0022PostalCode\\\u0022:\\\u002275003\\\u0022},\\\u0022Coordinates\\\u0022:{\\\u0022Latitude\\\u0022:48.8664,\\\u0022Longitude\\\u0022:2.36055}}\u0022,\u0022CountryOrRegion0\u0022:\u0022France\u0022,\u0022State0\u0022:\u0022\\u00cele-de-France\u0022,\u0022City0\u0022:\u0022Paris\u0022,\u0022PostalCode0\u0022:\u002275003\u0022,\u0022Street0\u0022:\u002272 Rue De Turbigo\u0022,\u0022GeoLoc0\u0022:{\u0022__metadata\u0022:{\u0022type\u0022:\u0022SP.FieldGeolocationValue\u0022},\u0022Altitude\u0022:0,\u0022Latitude\u0022:48.8664,\u0022Longitude\u0022:2.36055,\u0022Measure\u0022:0},\u0022DispName0\u0022:\u0022Paris France Hotel\u0022,\u0022ID\u0022:1,\u0022Created\u0022:\u00222024-02-02T20:02:46\u0022,\u0022AuthorId\u0022:13,\u0022Modified\u0022:\u00222024-02-04T14:17:52\u0022,\u0022EditorId\u0022:13,\u0022OData__CopySource\u0022:null,\u0022CheckoutUserId\u0022:null,\u0022LinkFilename\u0022:\u0022settings.dat\u0022,\u0022OData__UIVersionString\u0022:\u00224.0\u0022,\u0022GUID\u0022:\u0022d602c406-84b6-4acb-bf07-e170aabfb39d\u0022}]}}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"238408a1-c0b3-3000-032a-d392a7a32c1e","SPClientServiceRequestDuration":"29","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022d\u0022:{\u0022results\u0022:[{\u0022__metadata\u0022:{\u0022id\u0022:\u00227ff69d82-ee5c-42a4-9382-84e023de26bd\u0022,\u0022uri\u0022:\u0022https://hsftrms.sharepoint.com/sites/DNTest/_api/Web/Lists(guid\u0027dfe5e305-80fb-4578-a521-496b47927628\u0027)/Items(1)\u0022,\u0022etag\u0022:\u0022\\\u00223\\\u0022\u0022,\u0022type\u0022:\u0022SP.Data.WithImageAndLocationItem\u0022},\u0022Id\u0022:1,\u0022ID\u0022:1,\u0022LinkFilename\u0022:\u0022settings.dat\u0022}]}}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Text.Json;

namespace PnP.Core.Model.SharePoint
{
/// <summary>
/// Represents a location field value
/// </summary>
public sealed class FieldThumbnailValue
: FieldValue, IFieldThumbnailValue
{
/// <summary>
/// Default constructor
/// </summary>
public FieldThumbnailValue() : base()
{
}

internal override string SharePointRestType => "";

internal override Guid CsomType => Guid.Empty;

public string FileName { get => GetValue<string>(); internal set => SetValue(value); }

public string ServerRelativeUrl { get => GetValue<string>(); internal set => SetValue(value); }

public string ServerUrl { get => GetValue<string>(); internal set => SetValue(value); }

public object ThumbnailRenderer { get => GetValue<object>(); internal set => SetValue(value); }

internal override IFieldValue FromJson(JsonElement json)
{
if (json.ValueKind == JsonValueKind.Null || json.ValueKind == JsonValueKind.Undefined)
{
FileName = string.Empty;
}
else
{
return FromListDataAsStream(json.ToObject<Dictionary<string, string>>());
}

// Clear changes
Commit();

return this;
}

internal override IFieldValue FromListDataAsStream(Dictionary<string, string> properties)
{
if (properties.ContainsKey("fileName"))
{
FileName = properties["fileName"];
}
if (properties.ContainsKey("serverRelativeUrl"))
{
ServerRelativeUrl = properties["serverRelativeUrl"];
}
if (properties.ContainsKey("serverUrl"))
{
ServerUrl = properties["serverUrl"];
}
if (properties.ContainsKey("thumbnailRenderer"))
{
ThumbnailRenderer = properties["thumbnailRenderer"];
}
// Clear changes
Commit();

return this;
}

internal override object ToJson()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incomplete as I'm not sure when this is used

{
var updateMessage = new
{

};

return updateMessage;
}

internal override object ToValidateUpdateItemJson()
{
return null;
}

internal override string ToCsomXml()
{
return "";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ private static Tuple<FieldValue, bool> DetectSpecialFieldType(string name, IFiel
case "TaxonomyFieldTypeMulti": return new Tuple<FieldValue, bool>(new FieldTaxonomyValue() { Field = field }, true);
case "TaxonomyFieldType": return new Tuple<FieldValue, bool>(new FieldTaxonomyValue() { Field = field }, false);
case "Location": return new Tuple<FieldValue, bool>(new FieldLocationValue() { Field = field }, false);
case "Thumbnail": return new Tuple<FieldValue, bool>(new FieldThumbnailValue() { Field = field }, false);

default:
{
Expand All @@ -535,7 +536,7 @@ private static string GetJsonPropertyValueAsString(JsonElement propertyValue)
}
else if (propertyValue.ValueKind == JsonValueKind.Number)
{
return propertyValue.GetInt32().ToString();
return propertyValue.GetDouble().ToString();
}
else if (propertyValue.ValueKind == JsonValueKind.Undefined)
{
Expand Down Expand Up @@ -692,6 +693,10 @@ private static object GetJsonPropertyValue(JsonElement propertyValue, FieldType
{
return null;
}
else if (propertyValue.ValueKind == JsonValueKind.Object)
{
return propertyValue.ToObject<System.Dynamic.ExpandoObject>();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a "just in case" for other field types that happen to be treated as objects

}
else
{
return propertyValue.GetString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace PnP.Core.Model.SharePoint
{
/// <summary>
/// Represents a image (thumbnail) field value
/// </summary>
public interface IFieldThumbnailValue
: IFieldValue
{
/// <summary>
/// Filename identifiying this image
/// </summary>
public string FileName { get; }
/// <summary>
/// Server relative URL to access this image
/// </summary>
public string ServerRelativeUrl { get; }
/// <summary>
/// Server URL
/// </summary>
public string ServerUrl { get; }
/// <summary>
/// Thumbnail renderer
/// </summary>
public object ThumbnailRenderer { get; }
}
}
15 changes: 15 additions & 0 deletions src/sdk/PnP.Core/Services/Core/JsonMappingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,21 @@ private async static Task<Tuple<object, string>> ProcessSpecialRestFieldTypeAsyn
fieldValue.IsArray = false;
return new Tuple<object, string>(fieldValue, propertyName);
}
case "Thumbnail":
{
var fieldValue = new FieldThumbnailValue() { Field = field };
if (json.ValueKind != JsonValueKind.Null && json.ValueKind != JsonValueKind.Undefined)
{
var parsedFieldContent = JsonSerializer.Deserialize<JsonElement>(json.GetString());
fieldValue.FromJson(parsedFieldContent);
}
else
{
fieldValue.FromJson(new JsonElement());
}
fieldValue.IsArray = false;
return new Tuple<object, string>(fieldValue, propertyName);
}
default:
{
return new Tuple<object, string>(null, null);
Expand Down
Loading