Skip to content

Commit 696860f

Browse files
committed
Merge branch 'maintenance/code-analysis' into develop
Addressed a number of warnings from code analysis that have been introduced either as part of the OnTopic Editor 5.1.0 effort, or due to improved Roslyn code analyzers. Notably, this includes changing how we handle non-nullable properties that will be initialized by either data binding or the `ITopicMappingService`; the previous approach of using the `[NotNull]` and `[DisallowNull]` attributes is no longer supported; instead, we must use the null-forgiving operator (`!`). I preferred the previous approach, but regardless this addresses the issue.
2 parents ebcc2b3 + 95770f2 commit 696860f

9 files changed

Lines changed: 32 additions & 38 deletions

File tree

OnTopic.Editor.AspNetCore.Attributes/FileListAttribute/FileListAttributeViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
\=============================================================================================================================*/
66
using System.Collections.ObjectModel;
77
using System.ComponentModel.DataAnnotations;
8-
using System.Diagnostics.CodeAnalysis;
98
using Microsoft.AspNetCore.Mvc.Rendering;
109
using OnTopic.Editor.AspNetCore.Models;
1110
using OnTopic.Editor.AspNetCore.Models.Metadata;
@@ -62,8 +61,8 @@ public FileListAttributeViewModel(
6261
/// While the <i>relative</i> file path can be retrieved from <see cref="FileListAttributeDescriptorViewModel.Path"
6362
/// />, that doesn't include the base path of the web application. The <see cref="AbsolutePath"/> addresses this.
6463
/// </remarks>
65-
[Required, NotNull, DisallowNull]
66-
public string? AbsolutePath { get; set; }
64+
[Required]
65+
public string AbsolutePath { get; set; } = default!;
6766

6867
} // Class
6968
} // Namespace

OnTopic.Editor.AspNetCore.Attributes/ReflexiveAttribute/ReflexiveViewComponent.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| Client Ignia, LLC
44
| Project Topics Library
55
\=============================================================================================================================*/
6+
using System;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Mvc;
89
using OnTopic.Editor.AspNetCore.Models;
@@ -65,7 +66,15 @@ string htmlFieldPrefix
6566
| Establish snapshot of previously saved attribute descriptor
6667
\-----------------------------------------------------------------------------------------------------------------------*/
6768
var topic = _topicRepository.Load(currentTopic.UniqueKey);
68-
var reflexiveViewModel = (AttributeDescriptorViewModel?)await _topicMappingService.MapAsync(topic).ConfigureAwait(false);
69+
var reflexiveViewModel = (AttributeDescriptorViewModel?)null;
70+
71+
if (topic?.ContentType.EndsWith("AttributeDescriptor", StringComparison.OrdinalIgnoreCase)?? false) {
72+
reflexiveViewModel = (AttributeDescriptorViewModel?)await _topicMappingService.MapAsync(topic).ConfigureAwait(false);
73+
}
74+
75+
if (reflexiveViewModel is null) {
76+
reflexiveViewModel = new();
77+
}
6978

7079
/*------------------------------------------------------------------------------------------------------------------------
7180
| Establish hybrid view model

OnTopic.Editor.AspNetCore.Attributes/TopicListAttribute/TopicListViewComponent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public IViewComponentResult Invoke(
9595
_ => baseTopic
9696
};
9797
}
98-
else {
99-
rootTopic = _topicRepository.Load(attribute.RootTopic?.UniqueKey);
98+
else if (attribute.RootTopic is not null) {
99+
rootTopic = _topicRepository.Load(attribute.RootTopic.UniqueKey);
100100
}
101101

102102
if (rootTopic is not null && !String.IsNullOrEmpty(attribute.RelativeTopicPath)) {

OnTopic.Editor.AspNetCore.Attributes/Views/Editor/Components/Reflexive/Default.cshtml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
@{
55

6-
var excludedModelTypes = new ModelType[] { ModelType.Reflexive, ModelType.Relationship, ModelType.NestedTopic };
6+
var excludedModelTypes = new ModelType[] { ModelType.Reflexive, ModelType.Relationship, ModelType.NestedTopic, ModelType.None };
77
var modelType = Model.AttributeDescriptor.ModelType;
88
var isSupported = !excludedModelTypes.Contains(modelType);
99

@@ -21,6 +21,11 @@
2121
)
2222
</text>
2323
}
24+
else if (modelType is ModelType.None) {
25+
<div class="alert alert-warning" role="alert">
26+
<strong>Note:</strong> This attribute can only be set once the current topic has been saved.
27+
</div>
28+
}
2429
else {
2530
<div class="alert alert-warning" role="alert">
2631
<strong>Note:</strong> This attribute cannot be displayed on topics implementing the @Model.CurrentTopic.ContentType content type.

OnTopic.Editor.AspNetCore/Controllers/EditorController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ private ContentTypeDescriptor GetContentType(string contentType) => TopicReposit
122122
/// <param name="modelType"></param>
123123
/// <returns></returns>
124124
private ModelType GetModelType(ModelType modelType) {
125-
if (modelType == ModelType.Reflexive && CurrentTopic is AttributeDescriptor) {
126-
return ((AttributeDescriptor)CurrentTopic).ModelType;
125+
if (modelType == ModelType.Reflexive && CurrentTopic is AttributeDescriptor attributeDescriptor) {
126+
return attributeDescriptor.ModelType;
127127
}
128128
return modelType;
129129
}

OnTopic.Editor.AspNetCore/Models/AttributeBindingModel.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7-
using System.Diagnostics.CodeAnalysis;
87

98
namespace OnTopic.Editor.AspNetCore.Models {
109

@@ -43,35 +42,23 @@ public AttributeBindingModel(string editorType) {
4342
/// <summary>
4443
/// The unique name associated with the specified attribute.
4544
/// </summary>
46-
[NotNull, DisallowNull]
47-
public string? Key {
48-
get;
49-
init;
50-
}
45+
public string Key { get; init; } = default!;
5146

5247
/*==========================================================================================================================
5348
| EDITOR TYPE
5449
\-------------------------------------------------------------------------------------------------------------------------*/
5550
/// <summary>
5651
/// The editor type associated with the attribute.
5752
/// </summary>
58-
[NotNull, DisallowNull]
59-
public string? EditorType {
60-
get;
61-
init;
62-
}
53+
public string EditorType { get; init; } = default!;
6354

6455
/*==========================================================================================================================
6556
| VALUE
6657
\-------------------------------------------------------------------------------------------------------------------------*/
6758
/// <summary>
6859
/// The value associated with the attribute.
6960
/// </summary>
70-
[NotNull, DisallowNull]
71-
public string? Value {
72-
get;
73-
init;
74-
}
61+
public string Value { get; init; } = default!;
7562

7663
/*==========================================================================================================================
7764
| GET VALUE

OnTopic.Editor.AspNetCore/Models/ClientResources/ClientResource.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7-
using System.Diagnostics.CodeAnalysis;
87

98
namespace OnTopic.Editor.AspNetCore.Models.ClientResources {
109

@@ -22,8 +21,7 @@ public abstract class ClientResource {
2221
/// <summary>
2322
/// Gets or sets the URL associated with the client-side resource.
2423
/// </summary>
25-
[NotNull, DisallowNull]
26-
public Uri? Url { get; set; }
24+
public Uri Url { get; set; } = default!;
2725

2826
}
2927
}

OnTopic.Editor.AspNetCore/Models/EditorViewModel.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
| Client Ignia, LLC
44
| Project Topics Library
55
\=============================================================================================================================*/
6-
using System.Diagnostics.CodeAnalysis;
76
using OnTopic.Editor.AspNetCore.Models.Metadata;
87

98
namespace OnTopic.Editor.AspNetCore.Models {
@@ -23,8 +22,7 @@ public record EditorViewModel {
2322
/// The <see cref="EditingTopicViewModel"/> representing the core properties of the currently selected <see
2423
/// cref="Topic"/>.
2524
/// </summary>
26-
[NotNull, DisallowNull]
27-
public EditingTopicViewModel? Topic { get; init; }
25+
public EditingTopicViewModel Topic { get; init; } = default!;
2826

2927
/*==========================================================================================================================
3028
| CONTENT TYPE DESCRIPTOR
@@ -33,8 +31,7 @@ public record EditorViewModel {
3331
/// The <see cref="ContentTypeDescriptorViewModel"/> representing the core properties of the <see cref="Topic"/>'s <see
3432
/// cref="ContentTypeDescriptor"/>.
3533
/// </summary>
36-
[NotNull, DisallowNull]
37-
public ContentTypeDescriptorViewModel? ContentTypeDescriptor { get; init; }
34+
public ContentTypeDescriptorViewModel ContentTypeDescriptor { get; init; } = default!;
3835

3936
/*==========================================================================================================================
4037
| IS MODAL?

OnTopic.Editor.AspNetCore/Models/Metadata/AttributeDescriptorViewModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.ComponentModel.DataAnnotations;
8-
using System.Diagnostics.CodeAnalysis;
98
using OnTopic.Editor.AspNetCore.Models.ClientResources;
109
using OnTopic.Internal.Diagnostics;
1110
using OnTopic.Metadata;
@@ -64,17 +63,17 @@ public record AttributeDescriptorViewModel: CoreTopicViewModel {
6463
/// The <see cref="EditorType"/> corresponds to the <see cref="AttributeDescriptor"/> subtype name, such as <c>
6564
/// BooleanAttributeDescriptor</c>. This can be used by the editor to determine the appropriate view component to display.
6665
/// </remarks>
67-
[Required, NotNull, DisallowNull]
68-
public string? EditorType { get; init; }
66+
[Required]
67+
public string EditorType { get; init; } = default!;
6968

7069
/*==========================================================================================================================
7170
| PROPERTY: DISPLAY GROUP
7271
\-------------------------------------------------------------------------------------------------------------------------*/
7372
/// <summary>
7473
/// Determines what group of attributes to associate the current attribute with.
7574
/// </summary>
76-
[Required, NotNull, DisallowNull]
77-
public string? DisplayGroup { get; init; }
75+
[Required]
76+
public string DisplayGroup { get; init; } = default!;
7877

7978
/*==========================================================================================================================
8079
| PROPERTY: IS REQUIRED?

0 commit comments

Comments
 (0)