Skip to content

Commit fab87ae

Browse files
committed
Replaced [NotNull, DisallowNull] with null-forgiving operator
The Roslyn code analyzers will throw a `CS8616` if a non-nullable property will not be initialized after the constructor has executed. This is a problem for models that are expected to be initialized by e.g. data binding, mapping, or deserialization. We used to work around this using the `[NotNull, DisallowNull]` attributes on a nullable property, which I liked as it was explicit about what it was doing. Unfortunately, as of .NET 5.0.4 (SDK 5.0.201), that now throws a `CS8616` as well. Given that, we need to instead use the less obvious null-forgiving operator instead with these cases. Given this, I've replaced the cases where we were using `[NotNull, DisallowNull]` with `= default!;` on a non-nullable type instead.
1 parent ef9dc49 commit fab87ae

5 files changed

Lines changed: 12 additions & 27 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public FileListAttributeViewModel(
6262
/// While the <i>relative</i> file path can be retrieved from <see cref="FileListAttributeDescriptorViewModel.Path"
6363
/// />, that doesn't include the base path of the web application. The <see cref="AbsolutePath"/> addresses this.
6464
/// </remarks>
65-
[Required, NotNull, DisallowNull]
66-
public string? AbsolutePath { get; set; }
65+
[Required]
66+
public string AbsolutePath { get; set; } = default!;
6767

6868
} // Class
6969
} // Namespace

OnTopic.Editor.AspNetCore/Models/AttributeBindingModel.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,23 @@ public AttributeBindingModel(string editorType) {
4343
/// <summary>
4444
/// The unique name associated with the specified attribute.
4545
/// </summary>
46-
[NotNull, DisallowNull]
47-
public string? Key {
48-
get;
49-
init;
50-
}
46+
public string Key { get; init; } = default!;
5147

5248
/*==========================================================================================================================
5349
| EDITOR TYPE
5450
\-------------------------------------------------------------------------------------------------------------------------*/
5551
/// <summary>
5652
/// The editor type associated with the attribute.
5753
/// </summary>
58-
[NotNull, DisallowNull]
59-
public string? EditorType {
60-
get;
61-
init;
62-
}
54+
public string EditorType { get; init; } = default!;
6355

6456
/*==========================================================================================================================
6557
| VALUE
6658
\-------------------------------------------------------------------------------------------------------------------------*/
6759
/// <summary>
6860
/// The value associated with the attribute.
6961
/// </summary>
70-
[NotNull, DisallowNull]
71-
public string? Value {
72-
get;
73-
init;
74-
}
62+
public string Value { get; init; } = default!;
7563

7664
/*==========================================================================================================================
7765
| GET VALUE

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public abstract class ClientResource {
2222
/// <summary>
2323
/// Gets or sets the URL associated with the client-side resource.
2424
/// </summary>
25-
[NotNull, DisallowNull]
26-
public Uri? Url { get; set; }
25+
public Uri Url { get; set; } = default!;
2726

2827
}
2928
}

OnTopic.Editor.AspNetCore/Models/EditorViewModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public record EditorViewModel {
2323
/// The <see cref="EditingTopicViewModel"/> representing the core properties of the currently selected <see
2424
/// cref="Topic"/>.
2525
/// </summary>
26-
[NotNull, DisallowNull]
27-
public EditingTopicViewModel? Topic { get; init; }
26+
public EditingTopicViewModel? Topic { get; init; } = default!;
2827

2928
/*==========================================================================================================================
3029
| CONTENT TYPE DESCRIPTOR
@@ -33,8 +32,7 @@ public record EditorViewModel {
3332
/// The <see cref="ContentTypeDescriptorViewModel"/> representing the core properties of the <see cref="Topic"/>'s <see
3433
/// cref="ContentTypeDescriptor"/>.
3534
/// </summary>
36-
[NotNull, DisallowNull]
37-
public ContentTypeDescriptorViewModel? ContentTypeDescriptor { get; init; }
35+
public ContentTypeDescriptorViewModel ContentTypeDescriptor { get; init; } = default!;
3836

3937
/*==========================================================================================================================
4038
| IS MODAL?

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ public record AttributeDescriptorViewModel: CoreTopicViewModel {
6464
/// The <see cref="EditorType"/> corresponds to the <see cref="AttributeDescriptor"/> subtype name, such as <c>
6565
/// BooleanAttributeDescriptor</c>. This can be used by the editor to determine the appropriate view component to display.
6666
/// </remarks>
67-
[Required, NotNull, DisallowNull]
68-
public string? EditorType { get; init; }
67+
[Required]
68+
public string EditorType { get; init; } = default!;
6969

7070
/*==========================================================================================================================
7171
| PROPERTY: DISPLAY GROUP
7272
\-------------------------------------------------------------------------------------------------------------------------*/
7373
/// <summary>
7474
/// Determines what group of attributes to associate the current attribute with.
7575
/// </summary>
76-
[Required, NotNull, DisallowNull]
77-
public string? DisplayGroup { get; init; }
76+
[Required]
77+
public string DisplayGroup { get; init; } = default!;
7878

7979
/*==========================================================================================================================
8080
| PROPERTY: IS REQUIRED?

0 commit comments

Comments
 (0)