Skip to content

Commit c7328bc

Browse files
ronaldbarendsekjac
andauthored
v14: Return all data types from DataTypeService.GetAllAsync() (#16230)
* Return all data types from GetAllAsync * Remove unnecessary async/awaits * Ensure we don't accidentally fetch all data types when we mean to fetch none. --------- Co-authored-by: kjac <[email protected]>
1 parent 78d93a1 commit c7328bc

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

src/Umbraco.Cms.Api.Management/Controllers/DataType/Tree/DataTypeTreeControllerBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ protected override Ordering ItemOrdering
4040

4141
protected override DataTypeTreeItemResponseModel[] MapTreeItemViewModels(Guid? parentId, IEntitySlim[] entities)
4242
{
43-
var dataTypes = _dataTypeService
44-
.GetAllAsync(entities.Select(entity => entity.Key).ToArray()).GetAwaiter().GetResult()
45-
.ToDictionary(contentType => contentType.Id);
43+
Dictionary<int, IDataType> dataTypes = entities.Any()
44+
? _dataTypeService
45+
.GetAllAsync(entities.Select(entity => entity.Key).ToArray()).GetAwaiter().GetResult()
46+
.ToDictionary(contentType => contentType.Id)
47+
: new Dictionary<int, IDataType>();
4648

4749
return entities.Select(entity =>
4850
{

src/Umbraco.Core/Services/ContentTypeEditing/ContentTypeEditingServiceBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,12 @@ private Guid[] GetDataTypeKeys(ContentTypeEditingModelBase<TPropertyTypeModel, T
658658
=> model.Properties.Select(property => property.DataTypeKey).Distinct().ToArray();
659659

660660
private async Task<IDataType[]> GetDataTypesAsync(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model)
661-
=> (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray();
661+
{
662+
Guid[] dataTypeKeys = GetDataTypeKeys(model);
663+
return dataTypeKeys.Any()
664+
? (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray()
665+
: Array.Empty<IDataType>();
666+
}
662667

663668
private int? GetParentId(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, Guid? containerKey)
664669
{

src/Umbraco.Core/Services/DataTypeService.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.ComponentModel.DataAnnotations;
2-
using System.Reflection;
32
using Microsoft.Extensions.DependencyInjection;
43
using Microsoft.Extensions.Logging;
54
using Umbraco.Cms.Core.DependencyInjection;
@@ -14,7 +13,6 @@
1413
using Umbraco.Cms.Core.Scoping;
1514
using Umbraco.Cms.Core.Services.OperationStatus;
1615
using Umbraco.Extensions;
17-
using DataType = Umbraco.Cms.Core.Models.DataType;
1816

1917
namespace Umbraco.Cms.Core.Services.Implement
2018
{
@@ -224,27 +222,29 @@ public IEnumerable<EntityContainer> GetContainers(int[] containerIds)
224222
=> GetAsync(name).GetAwaiter().GetResult();
225223

226224
/// <inheritdoc />
227-
public async Task<IDataType?> GetAsync(string name)
225+
public Task<IDataType?> GetAsync(string name)
228226
{
229227
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
230228
IDataType? dataType = _dataTypeRepository.Get(Query<IDataType>().Where(x => x.Name == name))?.FirstOrDefault();
231229
ConvertMissingEditorOfDataTypeToLabel(dataType);
232-
return await Task.FromResult(dataType);
230+
231+
return Task.FromResult(dataType);
233232
}
234233

235234
/// <inheritdoc />
236235
public Task<IEnumerable<IDataType>> GetAllAsync(params Guid[] keys)
237236
{
238-
// Nothing requested, return nothing
239-
if (keys.Any() is false)
237+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
238+
239+
IQuery<IDataType> query = Query<IDataType>();
240+
if (keys.Length > 0)
240241
{
241-
return Task.FromResult(Enumerable.Empty<IDataType>());
242+
query = query.Where(x => keys.Contains(x.Key));
242243
}
243244

244-
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
245-
246-
IDataType[] dataTypes = _dataTypeRepository.Get(Query<IDataType>().Where(x => keys.Contains(x.Key))).ToArray();
245+
IDataType[] dataTypes = _dataTypeRepository.Get(query).ToArray();
247246
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
247+
248248
return Task.FromResult<IEnumerable<IDataType>>(dataTypes);
249249
}
250250

@@ -288,6 +288,7 @@ public Task<PagedModel<IDataType>> FilterAsync(string? name = null, string? edit
288288
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
289289
IDataType? dataType = _dataTypeRepository.Get(id);
290290
ConvertMissingEditorOfDataTypeToLabel(dataType);
291+
291292
return dataType;
292293
}
293294

@@ -301,12 +302,13 @@ public Task<PagedModel<IDataType>> FilterAsync(string? name = null, string? edit
301302
=> GetAsync(id).GetAwaiter().GetResult();
302303

303304
/// <inheritdoc />
304-
public async Task<IDataType?> GetAsync(Guid id)
305+
public Task<IDataType?> GetAsync(Guid id)
305306
{
306307
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
307308
IDataType? dataType = GetDataTypeFromRepository(id);
308309
ConvertMissingEditorOfDataTypeToLabel(dataType);
309-
return await Task.FromResult(dataType);
310+
311+
return Task.FromResult(dataType);
310312
}
311313

312314
/// <summary>
@@ -319,23 +321,25 @@ public IEnumerable<IDataType> GetByEditorAlias(string propertyEditorAlias)
319321
=> GetByEditorAliasAsync(propertyEditorAlias).GetAwaiter().GetResult();
320322

321323
/// <inheritdoc />
322-
public async Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string propertyEditorAlias)
324+
public Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string propertyEditorAlias)
323325
{
324326
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
325327
IQuery<IDataType> query = Query<IDataType>().Where(x => x.EditorAlias == propertyEditorAlias);
326328
IEnumerable<IDataType> dataTypes = _dataTypeRepository.Get(query).ToArray();
327329
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
328-
return await Task.FromResult(dataTypes);
330+
331+
return Task.FromResult(dataTypes);
329332
}
330333

331334
/// <inheritdoc />
332-
public async Task<IEnumerable<IDataType>> GetByEditorUiAlias(string editorUiAlias)
335+
public Task<IEnumerable<IDataType>> GetByEditorUiAlias(string editorUiAlias)
333336
{
334337
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
335338
IQuery<IDataType> query = Query<IDataType>().Where(x => x.EditorUiAlias == editorUiAlias);
336339
IEnumerable<IDataType> dataTypes = _dataTypeRepository.Get(query).ToArray();
337340
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
338-
return await Task.FromResult(dataTypes);
341+
342+
return Task.FromResult(dataTypes);
339343
}
340344

341345
/// <summary>
@@ -347,8 +351,8 @@ public IEnumerable<IDataType> GetAll(params int[] ids)
347351
{
348352
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
349353
IEnumerable<IDataType> dataTypes = _dataTypeRepository.GetMany(ids).ToArray();
350-
351354
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
355+
352356
return dataTypes;
353357
}
354358

@@ -366,8 +370,7 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable<IDataType> dat
366370
{
367371
// Any data types that don't have an associated editor are created of a specific type.
368372
// We convert them to labels to make clear to the user why the data type cannot be used.
369-
IEnumerable<IDataType> dataTypesWithMissingEditors = dataTypes
370-
.Where(x => x.Editor is MissingPropertyEditor);
373+
IEnumerable<IDataType> dataTypesWithMissingEditors = dataTypes.Where(x => x.Editor is MissingPropertyEditor);
371374
foreach (IDataType dataType in dataTypesWithMissingEditors)
372375
{
373376
dataType.Editor = new LabelPropertyEditor(_dataValueEditorFactory, _ioHelper);
@@ -398,7 +401,7 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable<IDataType> dat
398401
DataTypeOperationStatus.Success => OperationResult.Attempt.Succeed(MoveOperationStatusType.Success, evtMsgs),
399402
DataTypeOperationStatus.CancelledByNotification => OperationResult.Attempt.Fail(MoveOperationStatusType.FailedCancelledByEvent, evtMsgs),
400403
DataTypeOperationStatus.ParentNotFound => OperationResult.Attempt.Fail(MoveOperationStatusType.FailedParentNotFound, evtMsgs),
401-
_ => OperationResult.Attempt.Fail<MoveOperationStatusType>(MoveOperationStatusType.FailedNotAllowedByPath, evtMsgs, new InvalidOperationException($"Invalid operation status: {result.Status}")),
404+
_ => OperationResult.Attempt.Fail<MoveOperationStatusType>(MoveOperationStatusType.FailedNotAllowedByPath, evtMsgs, new InvalidOperationException($"Invalid operation status: {result.Status}")),
402405
};
403406
}
404407

@@ -448,9 +451,7 @@ public async Task<Attempt<IDataType, DataTypeOperationStatus>> MoveAsync(IDataTy
448451

449452
[Obsolete("Use the method which specifies the userId parameter")]
450453
public Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId)
451-
{
452-
return Copy(copying, containerId, Constants.Security.SuperUserId);
453-
}
454+
=> Copy(copying, containerId, Constants.Security.SuperUserId);
454455

455456
public Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId, int userId = Constants.Security.SuperUserId)
456457
{
@@ -671,7 +672,6 @@ public void Delete(IDataType dataType, int userId = Constants.Security.SuperUser
671672

672673
scope.Notifications.Publish(new DataTypeDeletedNotification(dataType, eventMessages).WithStateFrom(deletingDataTypeNotification));
673674

674-
675675
var currentUserId = await _userIdKeyResolver.GetAsync(userKey);
676676
Audit(AuditType.Delete, currentUserId, dataType.Id);
677677

0 commit comments

Comments
 (0)