diff --git a/src/Umbraco.Core/Composing/TypeHelper.cs b/src/Umbraco.Core/Composing/TypeHelper.cs index 95ac68cf04..0acec4da0c 100644 --- a/src/Umbraco.Core/Composing/TypeHelper.cs +++ b/src/Umbraco.Core/Composing/TypeHelper.cs @@ -183,7 +183,7 @@ namespace Umbraco.Cms.Core.Composing /// /// true if [is type assignable from] [the specified contract]; otherwise, false. /// - public static bool IsTypeAssignableFrom(Type contract, Type implementation) + public static bool IsTypeAssignableFrom(Type contract, Type? implementation) { return contract.IsAssignableFrom(implementation); } diff --git a/src/Umbraco.Core/Macros/IMacroRenderer.cs b/src/Umbraco.Core/Macros/IMacroRenderer.cs index fc3319c55c..473959f94f 100644 --- a/src/Umbraco.Core/Macros/IMacroRenderer.cs +++ b/src/Umbraco.Core/Macros/IMacroRenderer.cs @@ -9,6 +9,6 @@ namespace Umbraco.Cms.Core.Macros /// public interface IMacroRenderer { - Task RenderAsync(string macroAlias, IPublishedContent content, IDictionary? macroParams); + Task RenderAsync(string macroAlias, IPublishedContent? content, IDictionary macroParams); } } diff --git a/src/Umbraco.Core/Models/ImageUrlGenerationOptions.cs b/src/Umbraco.Core/Models/ImageUrlGenerationOptions.cs index d414a25852..855c7c00bc 100644 --- a/src/Umbraco.Core/Models/ImageUrlGenerationOptions.cs +++ b/src/Umbraco.Core/Models/ImageUrlGenerationOptions.cs @@ -5,9 +5,9 @@ namespace Umbraco.Cms.Core.Models /// public class ImageUrlGenerationOptions { - public ImageUrlGenerationOptions(string imageUrl) => ImageUrl = imageUrl; + public ImageUrlGenerationOptions(string? imageUrl) => ImageUrl = imageUrl; - public string ImageUrl { get; } + public string? ImageUrl { get; } public int? Width { get; set; } diff --git a/src/Umbraco.Core/Models/PublishedContent/IPublishedModelFactory.cs b/src/Umbraco.Core/Models/PublishedContent/IPublishedModelFactory.cs index 6ebf1c6707..de292a8112 100644 --- a/src/Umbraco.Core/Models/PublishedContent/IPublishedModelFactory.cs +++ b/src/Umbraco.Core/Models/PublishedContent/IPublishedModelFactory.cs @@ -22,7 +22,7 @@ namespace Umbraco.Cms.Core.Models.PublishedContent /// /// The model type alias. /// A List{T} of the strongly-typed model, exposed as an IList. - IList? CreateModelList(string alias); + IList? CreateModelList(string? alias); /// /// Maps a CLR type that may contain model types, to an actual CLR type. diff --git a/src/Umbraco.Core/Models/PublishedContent/ModelType.cs b/src/Umbraco.Core/Models/PublishedContent/ModelType.cs index 065681028e..c5f5731b0c 100644 --- a/src/Umbraco.Core/Models/PublishedContent/ModelType.cs +++ b/src/Umbraco.Core/Models/PublishedContent/ModelType.cs @@ -18,7 +18,7 @@ namespace Umbraco.Cms.Core.Models.PublishedContent /// public class ModelType : Type { - private ModelType(string contentTypeAlias) + private ModelType(string? contentTypeAlias) { if (contentTypeAlias == null) throw new ArgumentNullException(nameof(contentTypeAlias)); if (string.IsNullOrWhiteSpace(contentTypeAlias)) throw new ArgumentException("Value can't be empty or consist only of white-space characters.", nameof(contentTypeAlias)); @@ -41,7 +41,7 @@ namespace Umbraco.Cms.Core.Models.PublishedContent /// /// The published element type alias. /// The model type for the published element type. - public static ModelType For(string alias) + public static ModelType For(string? alias) => new ModelType(alias); /// diff --git a/src/Umbraco.Core/PropertyEditors/DataEditor.cs b/src/Umbraco.Core/PropertyEditors/DataEditor.cs index 07d8383334..5619a1bb87 100644 --- a/src/Umbraco.Core/PropertyEditors/DataEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/DataEditor.cs @@ -25,7 +25,7 @@ namespace Umbraco.Cms.Core.PropertyEditors /// /// Initializes a new instance of the class. /// - public DataEditor(IDataValueEditorFactory? dataValueEditorFactory, EditorType type = EditorType.PropertyValue) + public DataEditor(IDataValueEditorFactory dataValueEditorFactory, EditorType type = EditorType.PropertyValue) { // defaults @@ -60,7 +60,7 @@ namespace Umbraco.Cms.Core.PropertyEditors [DataMember(Name = "alias", IsRequired = true)] public string Alias { get; set; } - protected IDataValueEditorFactory? DataValueEditorFactory { get; } + protected IDataValueEditorFactory DataValueEditorFactory { get; } /// [IgnoreDataMember] diff --git a/src/Umbraco.Core/PropertyEditors/IDataValueEditorFactory.cs b/src/Umbraco.Core/PropertyEditors/IDataValueEditorFactory.cs index 663c7db6d6..4a80d28ae0 100644 --- a/src/Umbraco.Core/PropertyEditors/IDataValueEditorFactory.cs +++ b/src/Umbraco.Core/PropertyEditors/IDataValueEditorFactory.cs @@ -5,7 +5,7 @@ namespace Umbraco.Cms.Core.PropertyEditors { public interface IDataValueEditorFactory { - TDataValueEditor Create(params object[] args) + TDataValueEditor Create(params object?[] args) where TDataValueEditor : class, IDataValueEditor; } } diff --git a/src/Umbraco.Core/PropertyEditors/PropertyEditorCollection.cs b/src/Umbraco.Core/PropertyEditors/PropertyEditorCollection.cs index 6286287e1c..34f72cf5c0 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyEditorCollection.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyEditorCollection.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Linq; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.Manifest; @@ -21,7 +22,7 @@ namespace Umbraco.Cms.Core.PropertyEditors public virtual IDataEditor? this[string? alias] => this.SingleOrDefault(x => x.Alias == alias); - public virtual bool TryGet(string? alias, out IDataEditor? editor) + public virtual bool TryGet(string? alias, [MaybeNullWhen(false)] out IDataEditor editor) { editor = this.FirstOrDefault(x => x.Alias == alias); return editor != null; diff --git a/src/Umbraco.Core/PublishedCache/PublishedElement.cs b/src/Umbraco.Core/PublishedCache/PublishedElement.cs index 80f61fac40..de6eb1b316 100644 --- a/src/Umbraco.Core/PublishedCache/PublishedElement.cs +++ b/src/Umbraco.Core/PublishedCache/PublishedElement.cs @@ -18,7 +18,7 @@ namespace Umbraco.Cms.Core.PublishedCache { // initializes a new instance of the PublishedElement class // within the context of a published snapshot service (eg a published content property value) - public PublishedElement(IPublishedContentType contentType, Guid key, Dictionary values, bool previewing, + public PublishedElement(IPublishedContentType contentType, Guid key, Dictionary? values, bool previewing, PropertyCacheLevel referenceCacheLevel, IPublishedSnapshotAccessor? publishedSnapshotAccessor) { if (key == Guid.Empty) throw new ArgumentException("Empty guid."); diff --git a/src/Umbraco.Core/Strings/IHtmlEncodedString.cs b/src/Umbraco.Core/Strings/IHtmlEncodedString.cs index 9747350f3a..b7c0c27d2d 100644 --- a/src/Umbraco.Core/Strings/IHtmlEncodedString.cs +++ b/src/Umbraco.Core/Strings/IHtmlEncodedString.cs @@ -9,6 +9,6 @@ namespace Umbraco.Cms.Core.Strings /// Returns an HTML-encoded string. /// /// An HTML-encoded string. - string ToHtmlString(); + string? ToHtmlString(); } } diff --git a/src/Umbraco.Infrastructure/Examine/ExamineSearcherModel.cs b/src/Umbraco.Infrastructure/Examine/ExamineSearcherModel.cs index c4b602e430..1fd30de319 100644 --- a/src/Umbraco.Infrastructure/Examine/ExamineSearcherModel.cs +++ b/src/Umbraco.Infrastructure/Examine/ExamineSearcherModel.cs @@ -10,7 +10,7 @@ namespace Umbraco.Cms.Infrastructure.Examine } [DataMember(Name = "name")] - public string Name { get; set; } + public string? Name { get; set; } } diff --git a/src/Umbraco.Infrastructure/Extensions/MediaPicker3ConfigurationExtensions.cs b/src/Umbraco.Infrastructure/Extensions/MediaPicker3ConfigurationExtensions.cs index fe4aa541a0..62a3f96b22 100644 --- a/src/Umbraco.Infrastructure/Extensions/MediaPicker3ConfigurationExtensions.cs +++ b/src/Umbraco.Infrastructure/Extensions/MediaPicker3ConfigurationExtensions.cs @@ -11,7 +11,7 @@ namespace Umbraco.Extensions /// Applies the configuration to ensure only valid crops are kept and have the correct width/height. /// /// The configuration. - public static void ApplyConfiguration(this ImageCropperValue imageCropperValue, MediaPicker3Configuration configuration) + public static void ApplyConfiguration(this ImageCropperValue imageCropperValue, MediaPicker3Configuration? configuration) { var crops = new List(); diff --git a/src/Umbraco.Infrastructure/Models/Blocks/BlockItemData.cs b/src/Umbraco.Infrastructure/Models/Blocks/BlockItemData.cs index 1cd35e7e79..a1e7b46b30 100644 --- a/src/Umbraco.Infrastructure/Models/Blocks/BlockItemData.cs +++ b/src/Umbraco.Infrastructure/Models/Blocks/BlockItemData.cs @@ -36,7 +36,7 @@ namespace Umbraco.Cms.Core.Models.Blocks /// "stringValue":"Some String","numericValue":125,"otherNumeric":null /// [JsonExtensionData] - public Dictionary RawPropertyValues { get; set; } = new Dictionary(); + public Dictionary RawPropertyValues { get; set; } = new Dictionary(); /// /// Used during deserialization to convert the raw property data into data with a property type context diff --git a/src/Umbraco.Infrastructure/Models/Blocks/BlockListLayoutItem.cs b/src/Umbraco.Infrastructure/Models/Blocks/BlockListLayoutItem.cs index 49c3246d84..6df34079f4 100644 --- a/src/Umbraco.Infrastructure/Models/Blocks/BlockListLayoutItem.cs +++ b/src/Umbraco.Infrastructure/Models/Blocks/BlockListLayoutItem.cs @@ -10,10 +10,10 @@ namespace Umbraco.Cms.Core.Models.Blocks { [JsonProperty("contentUdi", Required = Required.Always)] [JsonConverter(typeof(UdiJsonConverter))] - public Udi ContentUdi { get; set; } + public Udi? ContentUdi { get; set; } [JsonProperty("settingsUdi", NullValueHandling = NullValueHandling.Ignore)] [JsonConverter(typeof(UdiJsonConverter))] - public Udi SettingsUdi { get; set; } + public Udi? SettingsUdi { get; set; } } } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/BlockEditorPropertyEditor.cs b/src/Umbraco.Infrastructure/PropertyEditors/BlockEditorPropertyEditor.cs index 7eef04ada1..ff258d8437 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/BlockEditorPropertyEditor.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/BlockEditorPropertyEditor.cs @@ -114,7 +114,7 @@ namespace Umbraco.Cms.Core.PropertyEditors var val = property.GetValue(culture, segment); var valEditors = new Dictionary(); - BlockEditorData blockEditorData; + BlockEditorData? blockEditorData; try { blockEditorData = _blockEditorValues.DeserializeAndClean(val); diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockEditorConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockEditorConverter.cs index 9b4c6a6db4..575f7bd9d1 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockEditorConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockEditorConverter.cs @@ -23,7 +23,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters _publishedModelFactory = publishedModelFactory; } - public IPublishedElement ConvertToElement(BlockItemData data, PropertyCacheLevel referenceCacheLevel, bool preview) + public IPublishedElement? ConvertToElement(BlockItemData data, PropertyCacheLevel referenceCacheLevel, bool preview) { var publishedContentCache = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot().Content; diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs index 6916f2ea3f..421a258f7a 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs @@ -37,19 +37,22 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters => PropertyCacheLevel.Element; /// - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, + IPublishedPropertyType propertyType, object? source, bool preview) { return source?.ToString(); } /// - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, + PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { // NOTE: The intermediate object is just a json string, we don't actually convert from source -> intermediate since source is always just a json string - using (_proflog.DebugDuration($"ConvertPropertyToBlockList ({propertyType.DataType.Id})")) + using (_proflog.DebugDuration( + $"ConvertPropertyToBlockList ({propertyType.DataType.Id})")) { - var value = (string)inter; + var value = (string?)inter; // Short-circuit on empty values if (string.IsNullOrWhiteSpace(value)) return BlockListModel.Empty; @@ -57,18 +60,19 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters var converted = _blockListEditorDataConverter.Deserialize(value); if (converted.BlockValue.ContentData.Count == 0) return BlockListModel.Empty; - var blockListLayout = converted.Layout.ToObject>(); + var blockListLayout = converted.Layout?.ToObject>(); // Get configuration var configuration = propertyType.DataType.ConfigurationAs(); - var blockConfigMap = configuration.Blocks.ToDictionary(x => x.ContentElementTypeKey); - var validSettingsElementTypes = blockConfigMap.Values.Select(x => x.SettingsElementTypeKey).Where(x => x.HasValue).Distinct().ToList(); + var blockConfigMap = configuration?.Blocks?.ToDictionary(x => x.ContentElementTypeKey); + var validSettingsElementTypes = blockConfigMap?.Values.Select(x => x.SettingsElementTypeKey) + .Where(x => x.HasValue).Distinct().ToList(); // Convert the content data var contentPublishedElements = new Dictionary(); foreach (var data in converted.BlockValue.ContentData) { - if (!blockConfigMap.ContainsKey(data.ContentTypeKey)) continue; + if (!blockConfigMap?.ContainsKey(data.ContentTypeKey) ?? false) continue; var element = _blockConverter.ConvertToElement(data, referenceCacheLevel, preview); if (element == null) continue; @@ -83,7 +87,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters var settingsPublishedElements = new Dictionary(); foreach (var data in converted.BlockValue.SettingsData) { - if (!validSettingsElementTypes.Contains(data.ContentTypeKey)) continue; + if (!validSettingsElementTypes?.Contains(data.ContentTypeKey) ?? false) continue; var element = _blockConverter.ConvertToElement(data, referenceCacheLevel, preview); if (element == null) continue; @@ -92,46 +96,53 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters } var layout = new List(); - foreach (var layoutItem in blockListLayout) + if (blockListLayout is not null) { - // Get the content reference - var contentGuidUdi = (GuidUdi)layoutItem.ContentUdi; - if (!contentPublishedElements.TryGetValue(contentGuidUdi.Guid, out var contentData)) - continue; - - if (!blockConfigMap.TryGetValue(contentData.ContentType.Key, out var blockConfig)) - continue; - - // Get the setting reference - IPublishedElement settingsData = null; - var settingGuidUdi = layoutItem.SettingsUdi != null ? (GuidUdi)layoutItem.SettingsUdi : null; - if (settingGuidUdi != null) - settingsPublishedElements.TryGetValue(settingGuidUdi.Guid, out settingsData); - - // This can happen if they have a settings type, save content, remove the settings type, and display the front-end page before saving the content again - // We also ensure that the content types match, since maybe the settings type has been changed after this has been persisted - if (settingsData != null && (!blockConfig.SettingsElementTypeKey.HasValue || settingsData.ContentType.Key != blockConfig.SettingsElementTypeKey)) + foreach (var layoutItem in blockListLayout) { - settingsData = null; + // Get the content reference + var contentGuidUdi = (GuidUdi?)layoutItem.ContentUdi; + if (contentGuidUdi is null || !contentPublishedElements.TryGetValue(contentGuidUdi.Guid, out var contentData)) + continue; + + if (contentData is null || (!blockConfigMap?.TryGetValue(contentData.ContentType.Key, out var blockConfig) ?? true)) + continue; + + // Get the setting reference + IPublishedElement? settingsData = null; + var settingGuidUdi = layoutItem.SettingsUdi is not null ? (GuidUdi)layoutItem.SettingsUdi : null; + if (settingGuidUdi is not null) + settingsPublishedElements.TryGetValue(settingGuidUdi.Guid, out settingsData); + + // This can happen if they have a settings type, save content, remove the settings type, and display the front-end page before saving the content again + // We also ensure that the content types match, since maybe the settings type has been changed after this has been persisted + if (settingsData != null && (!blockConfig.SettingsElementTypeKey.HasValue || + settingsData.ContentType.Key != + blockConfig.SettingsElementTypeKey)) + { + settingsData = null; + } + + // Get settings type from configuration + var settingsType = blockConfig.SettingsElementTypeKey.HasValue + ? _blockConverter.GetModelType(blockConfig.SettingsElementTypeKey.Value) + : typeof(IPublishedElement); + + // TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow + var layoutType = typeof(BlockListItem<,>).MakeGenericType(contentData.GetType(), settingsType); + var layoutRef = (BlockListItem?)Activator.CreateInstance(layoutType, contentGuidUdi, contentData, + settingGuidUdi, settingsData); + + if (layoutRef is not null) + { + layout.Add(layoutRef); + } } - - // Get settings type from configuration - var settingsType = blockConfig.SettingsElementTypeKey.HasValue - ? _blockConverter.GetModelType(blockConfig.SettingsElementTypeKey.Value) - : typeof(IPublishedElement); - - // TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow - var layoutType = typeof(BlockListItem<,>).MakeGenericType(contentData.GetType(), settingsType); - var layoutRef = (BlockListItem)Activator.CreateInstance(layoutType, contentGuidUdi, contentData, settingGuidUdi, settingsData); - - layout.Add(layoutRef); } var model = new BlockListModel(layout); return model; } } - - } } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ColorPickerValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ColorPickerValueConverter.cs index e35da5b1aa..e377956850 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ColorPickerValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ColorPickerValueConverter.cs @@ -21,20 +21,20 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Element; - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { var useLabel = UseLabel(propertyType); if (source == null) return useLabel ? null : string.Empty; - var ssource = source.ToString(); + var ssource = source.ToString()!; if (ssource.DetectIsJson()) { try { var jo = JsonConvert.DeserializeObject(ssource); - if (useLabel) return new PickedColor(jo["value"].ToString(), jo["label"].ToString()); - return jo["value"].ToString(); + if (useLabel) return new PickedColor(jo!["value"]!.ToString(), jo["label"]!.ToString()); + return jo!["value"]!.ToString(); } catch { /* not json finally */ } } @@ -45,7 +45,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters private bool UseLabel(IPublishedPropertyType propertyType) { - return ConfigurationEditor.ConfigurationAs(propertyType.DataType.Configuration).UseLabel; + return ConfigurationEditor.ConfigurationAs(propertyType.DataType.Configuration)?.UseLabel ?? false; } public class PickedColor diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/FlexibleDropdownPropertyValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/FlexibleDropdownPropertyValueConverter.cs index 8a2980bd9c..9e3ccd0fe0 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/FlexibleDropdownPropertyValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/FlexibleDropdownPropertyValueConverter.cs @@ -16,20 +16,20 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters return propertyType.EditorAlias.Equals(Constants.PropertyEditors.Aliases.DropDownListFlexible); } - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { if(source == null) return Array.Empty(); - return JsonConvert.DeserializeObject(source.ToString()) ?? Array.Empty(); + return JsonConvert.DeserializeObject(source.ToString()!) ?? Array.Empty(); } - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { if (inter == null) return null; - var multiple = propertyType.DataType.ConfigurationAs().Multiple; + var multiple = propertyType.DataType.ConfigurationAs()!.Multiple; var selectedValues = (string[]) inter; if (selectedValues.Length > 0) { @@ -45,7 +45,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override Type GetPropertyValueType(IPublishedPropertyType propertyType) { - return propertyType.DataType.ConfigurationAs().Multiple + return propertyType.DataType.ConfigurationAs()!.Multiple ? typeof(IEnumerable) : typeof(string); } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/GridValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/GridValueConverter.cs index 10f2de5581..0276307ac1 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/GridValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/GridValueConverter.cs @@ -35,10 +35,10 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Element; - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { if (source == null) return null; - var sourceString = source.ToString(); + var sourceString = source.ToString()!; if (sourceString.DetectIsJson()) { @@ -49,7 +49,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters //so we have the grid json... we need to merge in the grid's configuration values with the values // we've saved in the database so that when the front end gets this value, it is up-to-date. - var sections = GetArray(obj, "sections"); + var sections = GetArray(obj!, "sections"); foreach (var section in sections.Cast()) { var rows = GetArray(section, "rows"); @@ -104,7 +104,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters private JArray GetArray(JObject obj, string propertyName) { - JToken token; + JToken? token; if (obj.TryGetValue(propertyName, out token)) { var asArray = token as JArray; diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValue.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValue.cs index 39f41d2d39..ca5647c356 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValue.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValue.cs @@ -28,7 +28,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters /// Gets or sets the value source image. /// [DataMember(Name = "src")] - public string? Src { get; set; } + public string? Src { get; set; } = string.Empty; /// /// Gets or sets the value focal point. @@ -43,16 +43,16 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public IEnumerable? Crops { get; set; } /// - public override string ToString() + public override string? ToString() => HasCrops() || HasFocalPoint() ? JsonConvert.SerializeObject(this, Formatting.None) : Src; /// - public string ToHtmlString() => Src; + public string? ToHtmlString() => Src; /// /// Gets a crop. /// - public ImageCropperCrop GetCrop(string alias) + public ImageCropperCrop? GetCrop(string alias) { if (Crops == null) return null; @@ -62,13 +62,13 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters : Crops.FirstOrDefault(x => x.Alias.InvariantEquals(alias)); } - public ImageUrlGenerationOptions GetCropBaseOptions(string url, ImageCropperCrop crop, bool preferFocalPoint) + public ImageUrlGenerationOptions GetCropBaseOptions(string? url, ImageCropperCrop? crop, bool preferFocalPoint) { - if ((preferFocalPoint && HasFocalPoint()) || (crop != null && crop.Coordinates == null && HasFocalPoint())) + if ((preferFocalPoint && HasFocalPoint()) || (crop is not null && crop.Coordinates is null && HasFocalPoint())) { - return new ImageUrlGenerationOptions(url) { FocalPoint = new ImageUrlGenerationOptions.FocalPointPosition(FocalPoint.Left, FocalPoint.Top) }; + return new ImageUrlGenerationOptions(url) { FocalPoint = new ImageUrlGenerationOptions.FocalPointPosition(FocalPoint!.Left, FocalPoint.Top) }; } - else if (crop != null && crop.Coordinates != null && preferFocalPoint == false) + else if (crop is not null && crop.Coordinates is not null && preferFocalPoint == false) { return new ImageUrlGenerationOptions(url) { Crop = new ImageUrlGenerationOptions.CropCoordinates(crop.Coordinates.X1, crop.Coordinates.Y1, crop.Coordinates.X2, crop.Coordinates.Y2) }; } @@ -81,17 +81,17 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters /// /// Gets the value image URL for a specified crop. /// - public string GetCropUrl(string alias, IImageUrlGenerator imageUrlGenerator, bool useCropDimensions = true, bool useFocalPoint = false, string cacheBusterValue = null) + public string? GetCropUrl(string alias, IImageUrlGenerator imageUrlGenerator, bool useCropDimensions = true, bool useFocalPoint = false, string? cacheBusterValue = null) { var crop = GetCrop(alias); // could not find a crop with the specified, non-empty, alias - if (crop == null && !string.IsNullOrWhiteSpace(alias)) + if (crop is null && !string.IsNullOrWhiteSpace(alias)) return null; var options = GetCropBaseOptions(null, crop, useFocalPoint || string.IsNullOrWhiteSpace(alias)); - if (crop != null && useCropDimensions) + if (crop is not null && useCropDimensions) { options.Width = crop.Width; options.Height = crop.Height; @@ -105,7 +105,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters /// /// Gets the value image URL for a specific width and height. /// - public string GetCropUrl(int width, int height, IImageUrlGenerator imageUrlGenerator, string cacheBusterValue = null) + public string? GetCropUrl(int width, int height, IImageUrlGenerator imageUrlGenerator, string? cacheBusterValue = null) { var options = GetCropBaseOptions(null, null, false); @@ -151,12 +151,12 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters foreach (var incomingCrop in incomingCrops) { var crop = crops.FirstOrDefault(x => x.Alias == incomingCrop.Alias); - if (crop == null) + if (crop is null) { // Add incoming crop crops.Add(incomingCrop); } - else if (crop.Coordinates == null) + else if (crop.Coordinates is null) { // Use incoming crop coordinates crop.Coordinates = incomingCrop.Coordinates; @@ -187,16 +187,19 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters { foreach (var crop in crops.Values().ToList()) { - if (crop.TryGetValue("coordinates", out var coordinates) == false || coordinates.HasValues == false) + if (crop?.TryGetValue("coordinates", out var coordinates) == false) { - // Remove crop without coordinates - crop.Remove(); - continue; + if (coordinates!.HasValues) + { + // Remove crop without coordinates + crop.Remove(); + continue; + } } // Width/height are already stored in the crop configuration - crop.Remove("width"); - crop.Remove("height"); + crop?.Remove("width"); + crop?.Remove("height"); } } @@ -218,14 +221,14 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters #region IEquatable /// - public bool Equals(ImageCropperValue other) + public bool Equals(ImageCropperValue? other) => ReferenceEquals(this, other) || Equals(this, other); /// - public override bool Equals(object obj) + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || obj is ImageCropperValue other && Equals(this, other); - private static bool Equals(ImageCropperValue left, ImageCropperValue right) + private static bool Equals(ImageCropperValue left, ImageCropperValue? right) => ReferenceEquals(left, right) // deals with both being null, too || !ReferenceEquals(left, null) && !ReferenceEquals(right, null) && string.Equals(left.Src, right.Src) @@ -269,14 +272,14 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters #region IEquatable /// - public bool Equals(ImageCropperFocalPoint other) + public bool Equals(ImageCropperFocalPoint? other) => ReferenceEquals(this, other) || Equals(this, other); /// - public override bool Equals(object obj) + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || obj is ImageCropperFocalPoint other && Equals(this, other); - private static bool Equals(ImageCropperFocalPoint left, ImageCropperFocalPoint right) + private static bool Equals(ImageCropperFocalPoint left, ImageCropperFocalPoint? right) => ReferenceEquals(left, right) // deals with both being null, too || !ReferenceEquals(left, null) && !ReferenceEquals(right, null) && left.Left == right.Left @@ -306,7 +309,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public class ImageCropperCrop : IEquatable { [DataMember(Name = "alias")] - public string Alias { get; set; } + public string Alias { get; set; } = string.Empty; [DataMember(Name = "width")] public int Width { get; set; } @@ -320,14 +323,14 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters #region IEquatable /// - public bool Equals(ImageCropperCrop other) + public bool Equals(ImageCropperCrop? other) => ReferenceEquals(this, other) || Equals(this, other); /// - public override bool Equals(object obj) + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || obj is ImageCropperCrop other && Equals(this, other); - private static bool Equals(ImageCropperCrop left, ImageCropperCrop right) + private static bool Equals(ImageCropperCrop left, ImageCropperCrop? right) => ReferenceEquals(left, right) // deals with both being null, too || !ReferenceEquals(left, null) && !ReferenceEquals(right, null) && string.Equals(left.Alias, right.Alias) @@ -377,14 +380,14 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters #region IEquatable /// - public bool Equals(ImageCropperCropCoordinates other) + public bool Equals(ImageCropperCropCoordinates? other) => ReferenceEquals(this, other) || Equals(this, other); /// - public override bool Equals(object obj) + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || obj is ImageCropperCropCoordinates other && Equals(this, other); - private static bool Equals(ImageCropperCropCoordinates left, ImageCropperCropCoordinates right) + private static bool Equals(ImageCropperCropCoordinates left, ImageCropperCropCoordinates? right) => ReferenceEquals(left, right) // deals with both being null, too || !ReferenceEquals(left, null) && !ReferenceEquals(right, null) && left.X1 == right.X1 diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs index 6b3b7e68cb..aeb0fcd265 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs @@ -42,12 +42,12 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters }; /// - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { if (source == null) return null; - var sourceString = source.ToString(); + var sourceString = source.ToString()!; - ImageCropperValue value; + ImageCropperValue? value; try { value = JsonConvert.DeserializeObject(sourceString, ImageCropperValueJsonSerializerSettings); @@ -59,7 +59,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters value = new ImageCropperValue { Src = sourceString }; } - value?.ApplyConfiguration(propertyType.DataType.ConfigurationAs()); + value?.ApplyConfiguration(propertyType.DataType.ConfigurationAs()!); return value; } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueTypeConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueTypeConverter.cs index e10725bd4b..8130b040b4 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueTypeConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueTypeConverter.cs @@ -17,16 +17,21 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters { private static readonly Type[] ConvertableTypes = { typeof(JObject) }; - public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) { + if (destinationType is null) + { + return false; + } + return ConvertableTypes.Any(x => TypeHelper.IsTypeAssignableFrom(x, destinationType)) || CanConvertFrom(context, destinationType); } - public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) { var cropperValue = value as ImageCropperValue; - if (cropperValue == null) + if (cropperValue is null) return null; return TypeHelper.IsTypeAssignableFrom(destinationType) diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/JsonValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/JsonValueConverter.cs index 9f78872ec1..1c6d7c2eb5 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/JsonValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/JsonValueConverter.cs @@ -54,10 +54,10 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Element; - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { if (source == null) return null; - var sourceString = source.ToString(); + var sourceString = source.ToString()!; if (sourceString.DetectIsJson()) { diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MarkdownEditorValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MarkdownEditorValueConverter.cs index 420a3156d0..77f141ed86 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MarkdownEditorValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MarkdownEditorValueConverter.cs @@ -31,10 +31,10 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Snapshot; - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { if (source == null) return null; - var sourceString = source.ToString(); + var sourceString = source.ToString()!; // ensures string is parsed for {localLink} and URLs are resolved correctly sourceString = _localLinkParser.EnsureInternalLinks(sourceString, preview); @@ -43,7 +43,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters return sourceString; } - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { // convert markup to HTML for frontend rendering. // source should come from ConvertSource and be a string (or null) already @@ -51,7 +51,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters return new HtmlEncodedString(inter == null ? string.Empty : mark.Transform((string)inter)); } - public override object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { // source should come from ConvertSource and be a string (or null) already return inter?.ToString() ?? string.Empty; diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs index cde2a4465d..3d96edd6df 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs @@ -32,7 +32,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override bool IsConverter(IPublishedPropertyType propertyType) => propertyType.EditorAlias.Equals(Core.Constants.PropertyEditors.Aliases.MediaPicker3); - public override bool? IsValue(object value, PropertyValueLevel level) + public override bool? IsValue(object? value, PropertyValueLevel level) { var isValue = base.IsValue(value, level); if (isValue != false && level == PropertyValueLevel.Source) @@ -51,7 +51,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Snapshot; - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { var isMultiple = IsMultipleDataType(propertyType.DataType); if (string.IsNullOrEmpty(inter?.ToString())) @@ -80,7 +80,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters // TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow var mediaWithCropsType = typeof(MediaWithCrops<>).MakeGenericType(mediaItem.GetType()); - var mediaWithCrops = (MediaWithCrops)Activator.CreateInstance(mediaWithCropsType, mediaItem, _publishedValueFallback, localCrops); + var mediaWithCrops = (MediaWithCrops)Activator.CreateInstance(mediaWithCropsType, mediaItem, _publishedValueFallback, localCrops)!; mediaItems.Add(mediaWithCrops); @@ -95,6 +95,6 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters return isMultiple ? mediaItems : mediaItems.FirstOrDefault(); } - private bool IsMultipleDataType(PublishedDataType dataType) => dataType.ConfigurationAs().Multiple; + private bool IsMultipleDataType(PublishedDataType dataType) => dataType.ConfigurationAs()?.Multiple ?? false; } } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MultiUrlPickerValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MultiUrlPickerValueConverter.cs index 3d8f15f6d0..9ea9e82836 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MultiUrlPickerValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MultiUrlPickerValueConverter.cs @@ -35,21 +35,21 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters public override bool IsConverter(IPublishedPropertyType propertyType) => Constants.PropertyEditors.Aliases.MultiUrlPicker.Equals(propertyType.EditorAlias); public override Type GetPropertyValueType(IPublishedPropertyType propertyType) => - propertyType.DataType.ConfigurationAs().MaxNumber == 1 ? + propertyType.DataType.ConfigurationAs()!.MaxNumber == 1 ? typeof(Link) : typeof(IEnumerable); public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Snapshot; - public override bool? IsValue(object value, PropertyValueLevel level) => value?.ToString() != "[]"; + public override bool? IsValue(object? value, PropertyValueLevel level) => value?.ToString() != "[]"; - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) => source?.ToString(); + public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) => source?.ToString()!; - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { using (_proflog.DebugDuration($"ConvertPropertyToLinks ({propertyType.DataType.Id})")) { - var maxNumber = propertyType.DataType.ConfigurationAs().MaxNumber; + var maxNumber = propertyType.DataType.ConfigurationAs()!.MaxNumber; if (inter == null) { @@ -57,14 +57,14 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters } var links = new List(); - var dtos = _jsonSerializer.Deserialize>(inter.ToString()); + var dtos = _jsonSerializer.Deserialize>(inter.ToString()!); var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); foreach (var dto in dtos) { var type = LinkType.External; var url = dto.Url; - if (dto.Udi != null) + if (dto.Udi is not null) { type = dto.Udi.EntityType == Constants.UdiEntityType.Media ? LinkType.Media diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs index 3406b1e6ff..545ea73bf9 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs @@ -36,8 +36,8 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters /// public override Type GetPropertyValueType(IPublishedPropertyType propertyType) { - var contentTypes = propertyType.DataType.ConfigurationAs().ContentTypes; - return contentTypes.Length == 1 + var contentTypes = propertyType.DataType.ConfigurationAs()?.ContentTypes; + return contentTypes?.Length == 1 ? typeof(IEnumerable<>).MakeGenericType(ModelType.For(contentTypes[0].Alias)) : typeof(IEnumerable); } @@ -47,27 +47,27 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters => PropertyCacheLevel.Element; /// - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { return source?.ToString(); } /// - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { using (_proflog.DebugDuration($"ConvertPropertyToNestedContent ({propertyType.DataType.Id})")) { var configuration = propertyType.DataType.ConfigurationAs(); - var contentTypes = configuration.ContentTypes; - var elements = contentTypes.Length == 1 - ? PublishedModelFactory.CreateModelList(contentTypes[0].Alias) + var contentTypes = configuration?.ContentTypes; + var elements = contentTypes?.Length == 1 + ? PublishedModelFactory.CreateModelList(contentTypes[0].Alias)! : new List(); - var value = (string)inter; + var value = (string?)inter; if (string.IsNullOrWhiteSpace(value)) return elements; var objects = JsonConvert.DeserializeObject>(value); - if (objects.Count == 0) return elements; + if (objects is null || objects.Count == 0) return elements; foreach (var sourceObject in objects) { diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs index 705ff516ef..19ced217f7 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs @@ -36,10 +36,10 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters /// public override Type GetPropertyValueType(IPublishedPropertyType propertyType) { - var contentTypes = propertyType.DataType.ConfigurationAs().ContentTypes; - return contentTypes.Length > 1 + var contentTypes = propertyType.DataType.ConfigurationAs()!.ContentTypes; + return contentTypes?.Length > 1 ? typeof(IPublishedElement) - : ModelType.For(contentTypes[0].Alias); + : ModelType.For(contentTypes?[0].Alias); } /// @@ -47,20 +47,20 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters => PropertyCacheLevel.Element; /// - public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) { return source?.ToString(); } /// - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { using (_proflog.DebugDuration($"ConvertPropertyToNestedContent ({propertyType.DataType.Id})")) { - var value = (string)inter; + var value = (string?)inter; if (string.IsNullOrWhiteSpace(value)) return null; - var objects = JsonConvert.DeserializeObject>(value); + var objects = JsonConvert.DeserializeObject>(value)!; if (objects.Count == 0) return null; if (objects.Count > 1) diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs index 3f14aa6cad..75253ceba4 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs @@ -33,7 +33,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters return false; var config = publishedProperty.DataType.ConfigurationAs(); - return config.MinItems == 1 && config.MaxItems == 1; + return config?.MinItems == 1 && config.MaxItems == 1; } public static bool IsNestedMany(IPublishedPropertyType publishedProperty) @@ -41,7 +41,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters return IsNested(publishedProperty) && !IsNestedSingle(publishedProperty); } - protected IPublishedElement ConvertToElement(JObject sourceObject, PropertyCacheLevel referenceCacheLevel, bool preview) + protected IPublishedElement? ConvertToElement(JObject sourceObject, PropertyCacheLevel referenceCacheLevel, bool preview) { var elementTypeAlias = sourceObject[NestedContentPropertyEditor.ContentTypeAliasPropertyKey]?.ToObject(); if (string.IsNullOrEmpty(elementTypeAlias)) @@ -54,7 +54,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters var propertyValues = sourceObject.ToObject>(); - if (!propertyValues.TryGetValue("key", out var keyo) + if (propertyValues is null || !propertyValues.TryGetValue("key", out var keyo) || !Guid.TryParse(keyo.ToString(), out var key)) key = Guid.Empty; diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs index 4c85277781..cae9ec7306 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs @@ -72,21 +72,21 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters } } - public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) { var converted = Convert(inter, preview); return new HtmlEncodedString(converted == null ? string.Empty : converted); } - private string Convert(object source, bool preview) + private string? Convert(object? source, bool preview) { if (source == null) { return null; } - var sourceString = source.ToString(); + var sourceString = source.ToString()!; // ensures string is parsed for {localLink} and URLs and media are resolved correctly sourceString = _linkParser.EnsureInternalLinks(sourceString, preview);