From 57f32100cbc378c8a4a047c02c267947fa26752e Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle Date: Mon, 21 Feb 2022 12:49:17 +0100 Subject: [PATCH] Fix errors --- .../ServiceCollectionExtensions.cs | 16 +++++++++++++++- .../ContentEditing/PropertyTypeValidation.cs | 2 +- src/Umbraco.Core/Models/IPropertyType.cs | 2 +- .../Mapping/ContentPropertyDisplayMapper.cs | 2 +- .../Models/Mapping/ContentTypeMapDefinition.cs | 2 +- src/Umbraco.Core/Models/PropertyType.cs | 4 ++-- .../Services/PropertyValidationService.cs | 4 ++-- .../Factories/PropertyGroupFactory.cs | 2 +- 8 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs b/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs index 5cb9a7137f..6c806ce0db 100644 --- a/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs @@ -7,6 +7,20 @@ namespace Umbraco.Extensions { public static class ServiceCollectionExtensions { + /// + /// Adds a service of type with an implementation type of to the specified . + /// + /// + /// Removes all previous registrations for the type . + /// + public static void AddUnique( + this IServiceCollection services) + where TService : class + where TImplementing : class, TService + { + AddUnique(services, ServiceLifetime.Singleton); + } + /// /// Adds a service of type with an implementation type of to the specified . /// @@ -15,7 +29,7 @@ namespace Umbraco.Extensions /// public static void AddUnique( this IServiceCollection services, - ServiceLifetime lifetime = ServiceLifetime.Singleton) + ServiceLifetime lifetime) where TService : class where TImplementing : class, TService { diff --git a/src/Umbraco.Core/Models/ContentEditing/PropertyTypeValidation.cs b/src/Umbraco.Core/Models/ContentEditing/PropertyTypeValidation.cs index 7c35fd156a..5db1ab8139 100644 --- a/src/Umbraco.Core/Models/ContentEditing/PropertyTypeValidation.cs +++ b/src/Umbraco.Core/Models/ContentEditing/PropertyTypeValidation.cs @@ -9,7 +9,7 @@ namespace Umbraco.Cms.Core.Models.ContentEditing public class PropertyTypeValidation { [DataMember(Name = "mandatory")] - public bool? Mandatory { get; set; } + public bool Mandatory { get; set; } [DataMember(Name = "mandatoryMessage")] public string? MandatoryMessage { get; set; } diff --git a/src/Umbraco.Core/Models/IPropertyType.cs b/src/Umbraco.Core/Models/IPropertyType.cs index 2e70c64cd9..be7eb5708e 100644 --- a/src/Umbraco.Core/Models/IPropertyType.cs +++ b/src/Umbraco.Core/Models/IPropertyType.cs @@ -46,7 +46,7 @@ namespace Umbraco.Cms.Core.Models /// /// Gets of sets a value indicating whether a value for this property type is required. /// - bool? Mandatory { get; set; } + bool Mandatory { get; set; } /// /// Gets or sets a value indicating whether the label of this property type should be displayed on top. diff --git a/src/Umbraco.Core/Models/Mapping/ContentPropertyDisplayMapper.cs b/src/Umbraco.Core/Models/Mapping/ContentPropertyDisplayMapper.cs index 90a608302c..8064925305 100644 --- a/src/Umbraco.Core/Models/Mapping/ContentPropertyDisplayMapper.cs +++ b/src/Umbraco.Core/Models/Mapping/ContentPropertyDisplayMapper.cs @@ -48,7 +48,7 @@ namespace Umbraco.Cms.Core.Models.Mapping dest.LabelOnTop = originalProp.PropertyType?.LabelOnTop; //add the validation information - dest.Validation.Mandatory = originalProp.PropertyType?.Mandatory; + dest.Validation.Mandatory = originalProp.PropertyType?.Mandatory ?? false; dest.Validation.MandatoryMessage = originalProp.PropertyType?.MandatoryMessage; dest.Validation.Pattern = originalProp.PropertyType?.ValidationRegExp; dest.Validation.PatternMessage = originalProp.PropertyType?.ValidationRegExpMessage; diff --git a/src/Umbraco.Core/Models/Mapping/ContentTypeMapDefinition.cs b/src/Umbraco.Core/Models/Mapping/ContentTypeMapDefinition.cs index f150523e69..0ded9ad81d 100644 --- a/src/Umbraco.Core/Models/Mapping/ContentTypeMapDefinition.cs +++ b/src/Umbraco.Core/Models/Mapping/ContentTypeMapDefinition.cs @@ -317,7 +317,7 @@ namespace Umbraco.Cms.Core.Models.Mapping target.Name = source.Label; target.DataTypeId = source.DataTypeId; target.DataTypeKey = source.DataTypeKey; - target.Mandatory = source.Validation?.Mandatory; + target.Mandatory = source.Validation?.Mandatory ?? false; target.MandatoryMessage = source.Validation?.MandatoryMessage; target.ValidationRegExp = source.Validation?.Pattern; target.ValidationRegExpMessage = source.Validation?.PatternMessage; diff --git a/src/Umbraco.Core/Models/PropertyType.cs b/src/Umbraco.Core/Models/PropertyType.cs index 7db1d0d2b3..12fe07f1c8 100644 --- a/src/Umbraco.Core/Models/PropertyType.cs +++ b/src/Umbraco.Core/Models/PropertyType.cs @@ -25,7 +25,7 @@ namespace Umbraco.Cms.Core.Models private Lazy? _propertyGroupId; private string _propertyEditorAlias; private ValueStorageType _valueStorageType; - private bool? _mandatory; + private bool _mandatory; private string? _mandatoryMessage; private int _sortOrder; private string? _validationRegExp; @@ -178,7 +178,7 @@ namespace Umbraco.Cms.Core.Models /// [DataMember] - public bool? Mandatory + public bool Mandatory { get => _mandatory; set => SetPropertyValueAndDetectChanges(value, ref _mandatory, nameof(Mandatory)); diff --git a/src/Umbraco.Core/Services/PropertyValidationService.cs b/src/Umbraco.Core/Services/PropertyValidationService.cs index 093e765948..7aea02ca57 100644 --- a/src/Umbraco.Core/Services/PropertyValidationService.cs +++ b/src/Umbraco.Core/Services/PropertyValidationService.cs @@ -40,7 +40,7 @@ namespace Umbraco.Cms.Core.Services var editor = _propertyEditors[propertyType.PropertyEditorAlias]; if (editor == null) throw new InvalidOperationException("No property editor found by alias " + propertyType.PropertyEditorAlias); - return ValidatePropertyValue(editor, dataType, postedValue, propertyType.Mandatory ?? false, propertyType.ValidationRegExp, propertyType.MandatoryMessage, propertyType.ValidationRegExpMessage); + return ValidatePropertyValue(editor, dataType, postedValue, propertyType.Mandatory, propertyType.ValidationRegExp, propertyType.MandatoryMessage, propertyType.ValidationRegExpMessage); } /// @@ -192,7 +192,7 @@ namespace Umbraco.Cms.Core.Services } var configuration = _dataTypeService.GetDataType(propertyType.DataTypeId).Configuration; var valueEditor = editor.GetValueEditor(configuration); - return !valueEditor.Validate(value, propertyType.Mandatory ?? false, propertyType.ValidationRegExp).Any(); + return !valueEditor.Validate(value, propertyType.Mandatory, propertyType.ValidationRegExp).Any(); } } } diff --git a/src/Umbraco.Infrastructure/Persistence/Factories/PropertyGroupFactory.cs b/src/Umbraco.Infrastructure/Persistence/Factories/PropertyGroupFactory.cs index 2e73ff037c..10b6aadea3 100644 --- a/src/Umbraco.Infrastructure/Persistence/Factories/PropertyGroupFactory.cs +++ b/src/Umbraco.Infrastructure/Persistence/Factories/PropertyGroupFactory.cs @@ -131,7 +131,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Factories ContentTypeId = contentTypeId, DataTypeId = propertyType.DataTypeId, Description = propertyType.Description, - Mandatory = propertyType.Mandatory ?? false, + Mandatory = propertyType.Mandatory, MandatoryMessage = propertyType.MandatoryMessage, Name = propertyType.Name, SortOrder = propertyType.SortOrder,