From f397ada759723e91673866da9c1339f4b3ce4699 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Thu, 18 Aug 2022 11:07:17 -0700 Subject: [PATCH] Allow invariant type/part definition lookup in ContentDefinitionManager (#12102) --- .../Controllers/AdminController.cs | 18 +++---- .../Builders/ContentPartDefinitionBuilder.cs | 6 +-- .../Builders/ContentTypeDefinitionBuilder.cs | 6 +-- .../ContentDefinitionManager.cs | 50 +++++++++---------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.ContentTypes/Controllers/AdminController.cs b/src/OrchardCore.Modules/OrchardCore.ContentTypes/Controllers/AdminController.cs index d073d2b9ac6..11b4ac35b2c 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentTypes/Controllers/AdminController.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentTypes/Controllers/AdminController.cs @@ -249,7 +249,7 @@ public async Task AddPartsTo(string id) { Type = typeViewModel, PartSelections = _contentDefinitionService.GetParts(metadataPartsOnly: false) - .Where(cpd => !typePartNames.Contains(cpd.Name) && cpd.PartDefinition != null ? cpd.PartDefinition.GetSettings().Attachable : false) + .Where(cpd => !typePartNames.Contains(cpd.Name, StringComparer.OrdinalIgnoreCase) && cpd.PartDefinition != null ? cpd.PartDefinition.GetSettings().Attachable : false) .Select(cpd => new PartSelectionViewModel { PartName = cpd.Name, PartDisplayName = cpd.DisplayName, PartDescription = cpd.Description }) .ToList() }; @@ -409,7 +409,7 @@ public async Task RemovePart(string id, string name) var typeViewModel = _contentDefinitionService.LoadType(id); - if (typeViewModel == null || !typeViewModel.TypeDefinition.Parts.Any(p => p.Name == name)) + if (typeViewModel == null || !typeViewModel.TypeDefinition.Parts.Any(p => String.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase))) { return NotFound(); } @@ -691,7 +691,7 @@ public async Task EditField(string id, string name, string returnU return NotFound(); } - var partFieldDefinition = partViewModel.PartDefinition.Fields.FirstOrDefault(x => x.Name == name); + var partFieldDefinition = partViewModel.PartDefinition.Fields.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); if (partFieldDefinition == null) { @@ -733,7 +733,7 @@ public async Task EditFieldPOST(string id, EditFieldViewModel view return NotFound(); } - var field = _contentDefinitionManager.LoadPartDefinition(id).Fields.FirstOrDefault(x => x.Name == viewModel.Name); + var field = _contentDefinitionManager.LoadPartDefinition(id).Fields.FirstOrDefault(x => String.Equals(x.Name, viewModel.Name, StringComparison.OrdinalIgnoreCase)); if (field == null) { @@ -773,7 +773,7 @@ public async Task EditFieldPOST(string id, EditFieldViewModel view _contentDefinitionService.AlterField(partViewModel, viewModel); // Refresh the local field variable in case it has been altered - field = _contentDefinitionManager.LoadPartDefinition(id).Fields.FirstOrDefault(x => x.Name == viewModel.Name); + field = _contentDefinitionManager.LoadPartDefinition(id).Fields.FirstOrDefault(x => String.Equals(x.Name, viewModel.Name, StringComparison.OrdinalIgnoreCase)); viewModel.Shape = await _contentDefinitionDisplayManager.UpdatePartFieldEditorAsync(field, _updateModelAccessor.ModelUpdater); @@ -821,7 +821,7 @@ public async Task RemoveFieldFromPOST(string id, string name) return NotFound(); } - var field = partViewModel.PartDefinition.Fields.FirstOrDefault(x => x.Name == name); + var field = partViewModel.PartDefinition.Fields.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); if (field == null) { @@ -858,7 +858,7 @@ public async Task EditTypePart(string id, string name) return NotFound(); } - var typePartDefinition = typeDefinition.Parts.FirstOrDefault(x => x.Name == name); + var typePartDefinition = typeDefinition.Parts.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); if (typePartDefinition == null) { @@ -900,7 +900,7 @@ public async Task EditTypePartPOST(string id, EditTypePartViewMode return NotFound(); } - var part = typeDefinition.Parts.FirstOrDefault(x => x.Name == viewModel.Name); + var part = typeDefinition.Parts.FirstOrDefault(x => String.Equals(x.Name, viewModel.Name, StringComparison.OrdinalIgnoreCase)); if (part == null) { @@ -938,7 +938,7 @@ public async Task EditTypePartPOST(string id, EditTypePartViewMode _contentDefinitionService.AlterTypePart(viewModel); // Refresh the local part variable in case it has been altered - part = _contentDefinitionManager.LoadTypeDefinition(id).Parts.FirstOrDefault(x => x.Name == viewModel.Name); + part = _contentDefinitionManager.LoadTypeDefinition(id).Parts.FirstOrDefault(x => String.Equals(x.Name, viewModel.Name, StringComparison.OrdinalIgnoreCase)); viewModel.Shape = await _contentDefinitionDisplayManager.UpdateTypePartEditorAsync(part, _updateModelAccessor.ModelUpdater); diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs index 46092459996..b07bd92e309 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs @@ -66,7 +66,7 @@ public ContentPartDefinitionBuilder Named(string name) public ContentPartDefinitionBuilder RemoveField(string fieldName) { - var existingField = _fields.SingleOrDefault(x => x.Name == fieldName); + var existingField = _fields.SingleOrDefault(x => String.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase)); if (existingField != null) { _fields.Remove(existingField); @@ -123,7 +123,7 @@ public ContentPartDefinitionBuilder WithField(string fieldName) public ContentPartDefinitionBuilder WithField(string fieldName, Action configuration) { - var existingField = _fields.FirstOrDefault(x => x.Name == fieldName); + var existingField = _fields.FirstOrDefault(x => String.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase)); if (existingField != null) { var toRemove = _fields.Where(x => x.Name == fieldName).ToArray(); @@ -144,7 +144,7 @@ public ContentPartDefinitionBuilder WithField(string fieldName, Action WithFieldAsync(string fieldName, Func configurationAsync) { - var existingField = _fields.FirstOrDefault(x => x.Name == fieldName); + var existingField = _fields.FirstOrDefault(x => String.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase)); if (existingField != null) { diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentTypeDefinitionBuilder.cs b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentTypeDefinitionBuilder.cs index 492d0310a56..76f85d79a7a 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentTypeDefinitionBuilder.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentTypeDefinitionBuilder.cs @@ -114,7 +114,7 @@ public ContentTypeDefinitionBuilder WithSettings(T settings) public ContentTypeDefinitionBuilder RemovePart(string partName) { - var existingPart = _parts.SingleOrDefault(x => x.Name == partName); + var existingPart = _parts.SingleOrDefault(x => String.Equals(x.Name, partName, StringComparison.OrdinalIgnoreCase)); if (existingPart != null) { _parts.Remove(existingPart); @@ -144,7 +144,7 @@ public ContentTypeDefinitionBuilder WithPart(string partName, Action configuration) { - var existingPart = _parts.FirstOrDefault(x => x.Name == name); + var existingPart = _parts.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); if (existingPart != null) { _parts.Remove(existingPart); @@ -173,7 +173,7 @@ public Task WithPartAsync(string partName, Func WithPartAsync(string name, ContentPartDefinition partDefinition, Func configurationAsync) { - var existingPart = _parts.FirstOrDefault(x => x.Name == name); + var existingPart = _parts.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); if (existingPart != null) { diff --git a/src/OrchardCore/OrchardCore.ContentManagement/ContentDefinitionManager.cs b/src/OrchardCore/OrchardCore.ContentManagement/ContentDefinitionManager.cs index 7a41d834c51..5d5503eab79 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement/ContentDefinitionManager.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement/ContentDefinitionManager.cs @@ -22,8 +22,8 @@ public class ContentDefinitionManager : IContentDefinitionManager private readonly ConcurrentDictionary _cachedTypeDefinitions; private readonly ConcurrentDictionary _cachedPartDefinitions; - private readonly Dictionary _scopedTypeDefinitions = new Dictionary(); - private readonly Dictionary _scopedPartDefinitions = new Dictionary(); + private readonly Dictionary _scopedTypeDefinitions = new(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary _scopedPartDefinitions = new(StringComparer.OrdinalIgnoreCase); public ContentDefinitionManager( IContentDefinitionStore contentDefinitionStore, @@ -32,15 +32,15 @@ public ContentDefinitionManager( _contentDefinitionStore = contentDefinitionStore; _memoryCache = memoryCache; - _cachedTypeDefinitions = _memoryCache.GetOrCreate("TypeDefinitions", entry => new ConcurrentDictionary()); - _cachedPartDefinitions = _memoryCache.GetOrCreate("PartDefinitions", entry => new ConcurrentDictionary()); + _cachedTypeDefinitions = _memoryCache.GetOrCreate("TypeDefinitions", entry => new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase)); + _cachedPartDefinitions = _memoryCache.GetOrCreate("PartDefinitions", entry => new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase)); } public async Task GetIdentifierAsync() => (await _contentDefinitionStore.GetContentDefinitionAsync()).Identifier; public ContentTypeDefinition LoadTypeDefinition(string name) { - if (string.IsNullOrEmpty(name)) + if (String.IsNullOrEmpty(name)) { throw new ArgumentException("Argument cannot be null or empty", nameof(name)); } @@ -49,7 +49,7 @@ public ContentTypeDefinition LoadTypeDefinition(string name) { var contentTypeDefinitionRecord = LoadContentDefinitionRecord() .ContentTypeDefinitionRecords - .FirstOrDefault(x => x.Name == name); + .FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); _scopedTypeDefinitions[name] = typeDefinition = Build(contentTypeDefinitionRecord, LoadContentDefinitionRecord().ContentPartDefinitionRecords); }; @@ -59,7 +59,7 @@ public ContentTypeDefinition LoadTypeDefinition(string name) public ContentTypeDefinition GetTypeDefinition(string name) { - if (string.IsNullOrEmpty(name)) + if (String.IsNullOrEmpty(name)) { throw new ArgumentException("Argument cannot be null or empty", nameof(name)); } @@ -71,7 +71,7 @@ public ContentTypeDefinition GetTypeDefinition(string name) { var contentTypeDefinitionRecord = document .ContentTypeDefinitionRecords - .FirstOrDefault(x => x.Name == name); + .FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); return Build(contentTypeDefinitionRecord, GetContentDefinitionRecord().ContentPartDefinitionRecords); }); @@ -83,7 +83,7 @@ public ContentPartDefinition LoadPartDefinition(string name) { _scopedPartDefinitions[name] = partDefinition = Build(LoadContentDefinitionRecord() .ContentPartDefinitionRecords - .FirstOrDefault(x => x.Name == name)); + .FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase))); }; return partDefinition; @@ -98,7 +98,7 @@ public ContentPartDefinition GetPartDefinition(string name) { return Build(document .ContentPartDefinitionRecords - .FirstOrDefault(x => x.Name == name)); + .FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase))); }); } @@ -136,7 +136,7 @@ public void StorePartDefinition(ContentPartDefinition contentPartDefinition) public void DeleteTypeDefinition(string name) { - var record = LoadContentDefinitionRecord().ContentTypeDefinitionRecords.FirstOrDefault(x => x.Name == name); + var record = LoadContentDefinitionRecord().ContentTypeDefinitionRecords.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); // deletes the content type record associated if (record != null) @@ -149,7 +149,7 @@ public void DeleteTypeDefinition(string name) public void DeletePartDefinition(string name) { // remove parts from current types - var typesWithPart = LoadTypeDefinitions().Where(typeDefinition => typeDefinition.Parts.Any(part => part.PartDefinition.Name == name)); + var typesWithPart = LoadTypeDefinitions().Where(typeDefinition => typeDefinition.Parts.Any(part => String.Equals(part.PartDefinition.Name, name, StringComparison.OrdinalIgnoreCase))); foreach (var typeDefinition in typesWithPart) { @@ -157,7 +157,7 @@ public void DeletePartDefinition(string name) } // delete part - var record = LoadContentDefinitionRecord().ContentPartDefinitionRecords.FirstOrDefault(x => x.Name == name); + var record = LoadContentDefinitionRecord().ContentPartDefinitionRecords.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); if (record != null) { @@ -168,7 +168,7 @@ public void DeletePartDefinition(string name) private ContentTypeDefinitionRecord Acquire(ContentTypeDefinition contentTypeDefinition) { - var result = LoadContentDefinitionRecord().ContentTypeDefinitionRecords.FirstOrDefault(x => x.Name == contentTypeDefinition.Name); + var result = LoadContentDefinitionRecord().ContentTypeDefinitionRecords.FirstOrDefault(x => String.Equals(x.Name, contentTypeDefinition.Name, StringComparison.OrdinalIgnoreCase)); if (result == null) { result = new ContentTypeDefinitionRecord { Name = contentTypeDefinition.Name, DisplayName = contentTypeDefinition.DisplayName }; @@ -179,7 +179,7 @@ private ContentTypeDefinitionRecord Acquire(ContentTypeDefinition contentTypeDef private ContentPartDefinitionRecord Acquire(ContentPartDefinition contentPartDefinition) { - var result = LoadContentDefinitionRecord().ContentPartDefinitionRecords.FirstOrDefault(x => x.Name == contentPartDefinition.Name); + var result = LoadContentDefinitionRecord().ContentPartDefinitionRecords.FirstOrDefault(x => String.Equals(x.Name, contentPartDefinition.Name, StringComparison.OrdinalIgnoreCase)); if (result == null) { result = new ContentPartDefinitionRecord { Name = contentPartDefinition.Name, }; @@ -188,13 +188,13 @@ private ContentPartDefinitionRecord Acquire(ContentPartDefinition contentPartDef return result; } - private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord record) + private static void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord record) { record.DisplayName = model.DisplayName; record.Settings = model.Settings; var toRemove = record.ContentTypePartDefinitionRecords - .Where(typePartDefinitionRecord => !model.Parts.Any(part => typePartDefinitionRecord.Name == part.Name)) + .Where(typePartDefinitionRecord => !model.Parts.Any(part => String.Equals(typePartDefinitionRecord.Name, part.Name, StringComparison.OrdinalIgnoreCase))) .ToList(); foreach (var remove in toRemove) @@ -204,7 +204,7 @@ private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord reco foreach (var part in model.Parts) { - var typePartRecord = record.ContentTypePartDefinitionRecords.FirstOrDefault(r => r.Name == part.Name); + var typePartRecord = record.ContentTypePartDefinitionRecords.FirstOrDefault(r => String.Equals(r.Name, part.Name, StringComparison.OrdinalIgnoreCase)); if (typePartRecord == null) { typePartRecord = new ContentTypePartDefinitionRecord @@ -220,17 +220,17 @@ private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord reco } } - private void Apply(ContentTypePartDefinition model, ContentTypePartDefinitionRecord record) + private static void Apply(ContentTypePartDefinition model, ContentTypePartDefinitionRecord record) { record.Settings = model.Settings; } - private void Apply(ContentPartDefinition model, ContentPartDefinitionRecord record) + private static void Apply(ContentPartDefinition model, ContentPartDefinitionRecord record) { record.Settings = model.Settings; var toRemove = record.ContentPartFieldDefinitionRecords - .Where(partFieldDefinitionRecord => !model.Fields.Any(partField => partFieldDefinitionRecord.Name == partField.Name)) + .Where(partFieldDefinitionRecord => !model.Fields.Any(partField => String.Equals(partFieldDefinitionRecord.Name, partField.Name, StringComparison.OrdinalIgnoreCase))) .ToList(); foreach (var remove in toRemove) @@ -241,7 +241,7 @@ private void Apply(ContentPartDefinition model, ContentPartDefinitionRecord reco foreach (var field in model.Fields) { var fieldName = field.Name; - var partFieldRecord = record.ContentPartFieldDefinitionRecords.FirstOrDefault(r => r.Name == fieldName); + var partFieldRecord = record.ContentPartFieldDefinitionRecords.FirstOrDefault(r => String.Equals(r.Name, fieldName, StringComparison.OrdinalIgnoreCase)); if (partFieldRecord == null) { partFieldRecord = new ContentPartFieldDefinitionRecord @@ -255,7 +255,7 @@ private void Apply(ContentPartDefinition model, ContentPartDefinitionRecord reco } } - private void Apply(ContentPartFieldDefinition model, ContentPartFieldDefinitionRecord record) + private static void Apply(ContentPartFieldDefinition model, ContentPartFieldDefinitionRecord record) { record.Settings = model.Settings; } @@ -270,7 +270,7 @@ private ContentTypeDefinition Build(ContentTypeDefinitionRecord source, IList Build(tp, partDefinitionRecords.FirstOrDefault(p => p.Name == tp.PartName))), + source.ContentTypePartDefinitionRecords.Select(tp => Build(tp, partDefinitionRecords.FirstOrDefault(p => String.Equals(p.Name, tp.PartName, StringComparison.OrdinalIgnoreCase)))), source.Settings); return contentTypeDefinition; @@ -301,7 +301,7 @@ private ContentPartFieldDefinition Build(ContentPartFieldDefinitionRecord source ); } - private ContentFieldDefinition Build(ContentFieldDefinitionRecord source) + private static ContentFieldDefinition Build(ContentFieldDefinitionRecord source) { return source == null ? null : new ContentFieldDefinition(source.Name); }