Skip to content

Commit e18c434

Browse files
author
Warren Buckley
authored
Merge pull request #34 from umbraco-community/feature/nullable-types
Enable & fix up any nullable type issues
2 parents 2c0bb3f + cb6843c commit e18c434

13 files changed

+69
-67
lines changed

Our.Umbraco.TagHelpers/ActiveClassTagHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ActiveClassTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
3535
/// on an <a> tag when the page is active/selected
3636
/// </summary>
3737
[HtmlAttributeName(tagHelperAttributeName)]
38-
public string ActiveClassName { get; set; }
38+
public string? ActiveClassName { get; set; }
3939

4040

4141
/// <summary>
@@ -62,7 +62,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
6262

6363
// Try & parse href as URI, as it could be relative or absolute
6464
// or contain a quersystring we only want the path part
65-
if (Uri.TryCreate(href, UriKind.Absolute, out Uri link) || Uri.TryCreate(ctx.PublishedRequest.Uri, href, out link))
65+
if (Uri.TryCreate(href, UriKind.Absolute, out Uri? link) || Uri.TryCreate(ctx.PublishedRequest.Uri, href, out link))
6666
{
6767
// Get the node based of the value in the HREF
6868
var nodeOfLink = ctx.Content.GetByRoute(link.AbsolutePath);

Our.Umbraco.TagHelpers/Composing/PackageManifestComposer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TagHelperManifestFilter : IManifestFilter
1717
{
1818
public void Filter(List<PackageManifest> manifests)
1919
{
20-
var version = typeof(TagHelperManifestFilter).Assembly.GetName().Version.ToString();
20+
var version = typeof(TagHelperManifestFilter).Assembly.GetName()?.Version?.ToString() ?? "Unknown";
2121

2222
manifests.Add(new PackageManifest
2323
{

Our.Umbraco.TagHelpers/DictionaryTagHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public DictionaryTagHelper(ILocalizationService localizationService)
2323
/// The dictionary key to translate
2424
/// </summary>
2525
[HtmlAttributeName("key")]
26-
public string Key { get; set; }
26+
public string? Key { get; set; }
2727

2828
/// <summary>
2929
/// An optional attribute to set a fallback language to use
3030
/// If the current language does not contain a translation for the key
3131
/// </summary>
3232
[HtmlAttributeName("fallback-lang")]
33-
public string FallbackLang { get; set; }
33+
public string? FallbackLang { get; set; }
3434

3535
public override void Process(TagHelperContext context, TagHelperOutput output)
3636
{

Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public InlineSvgTagHelper(MediaFileManager mediaFileManager, IWebHostEnvironment
3030
/// NOTE: You cannot use this in conjuction with the media-item attribute
3131
/// </summary>
3232
[HtmlAttributeName("src")]
33-
public string FileSource { get; set; }
33+
public string? FileSource { get; set; }
3434

3535
/// <summary>
3636
/// An IPublishedContent Umbraco Media Item that has an .svg file extension
3737
/// NOTE: You cannot use this in conjuction with the src attribute
3838
/// </summary>
3939
[HtmlAttributeName("media-item")]
40-
public IPublishedContent MediaItem { get; set; }
40+
public IPublishedContent? MediaItem { get; set; }
4141

4242
public override void Process(TagHelperContext context, TagHelperOutput output)
4343
{

Our.Umbraco.TagHelpers/LanguageSwitcherTagHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
5252
var allDomains = umbracoContext.Domains.GetAll(false);
5353
var allDomainsFiltered = allDomains?.GroupBy(d => d.Culture).Select(g => g.First()).ToList();
5454

55-
if (allDomainsFiltered.Count() > 0)
55+
if (allDomainsFiltered?.Count() > 0)
5656
{
5757
// List of languages/model to use with
5858
var data = new Dictionary<string, object>();
@@ -99,11 +99,11 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
9999

100100
public class LanguageSwitcherLang
101101
{
102-
public string Culture { get; set; }
102+
public string? Culture { get; set; }
103103

104-
public string Name { get; set; }
104+
public string? Name { get; set; }
105105

106-
public string Url { get; set; }
106+
public string? Url { get; set; }
107107

108108
public bool IsCurrentLang { get; set; }
109109
}

Our.Umbraco.TagHelpers/MacroTagHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ public class MacroTagHelper : TagHelper
2121
/// The alias of the Macro to execute
2222
/// </summary>
2323
[HtmlAttributeName("alias")]
24-
public string MacroAlias { get; set; }
24+
public string? MacroAlias { get; set; }
2525

2626
/// <summary>
2727
/// An optional attribute to set the context of the Content that the Macro will use
2828
/// Without it set it will use the current page
2929
/// </summary>
3030
[HtmlAttributeName("content")]
31-
public IPublishedContent ContentNode { get; set; }
31+
public IPublishedContent? ContentNode { get; set; }
3232

3333
/// <summary>
3434
/// An optional list of attributes that will map to Macro Parameters
3535
/// bind:myMacroParam="" bind:startNodeId="" etc...
3636
/// </summary>
3737
[HtmlAttributeName(DictionaryAttributePrefix = "bind:")]
38-
public IDictionary<string, string> MacroParameters { get; set; } = new Dictionary<string, string>();
38+
public IDictionary<string, string>? MacroParameters { get; set; } = new Dictionary<string, string>();
3939

4040
public MacroTagHelper(IUmbracoComponentRenderer umbracoComponentRenderer, IUmbracoContextAccessor umbracoContextAccessor)
4141
{
@@ -59,7 +59,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
5959
// TagHelpers dont seem to like HTMLAttributes as <string, object> dictionary
6060
// Hence us trying to convert it here
6161
IDictionary<string, object> macroParams = new Dictionary<string, object>();
62-
if (MacroParameters.Any())
62+
if (MacroParameters != null && MacroParameters.Any())
6363
{
6464
macroParams = MacroParameters.ToDictionary(p => p.Key, p => (object)p.Value);
6565
}

Our.Umbraco.TagHelpers/MemberTagHelper.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public class MemberTagHelper : TagHelper
2323
/// * = All authenticated users
2424
/// </summary>
2525
[HtmlAttributeName("our-member-exclude")]
26-
public string ExcludeRoles { get; set; }
26+
public string? ExcludeRoles { get; set; }
2727

2828
/// <summary>
2929
/// A comma separated list of Member Groups to include
3030
/// ? = All anonymous users
3131
/// * = All authenticated users
3232
/// </summary>
3333
[HtmlAttributeName("our-member-include")]
34-
public string IncludeRoles { get; set; }
34+
public string? IncludeRoles { get; set; }
3535

3636
public MemberTagHelper(IMemberManager memberManager)
3737
{
@@ -62,29 +62,30 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
6262
}
6363
}
6464

65-
private bool IsUserInRole(string roleString, List<string> currentMemberRoles)
65+
private bool IsUserInRole(string? roleString, List<string> currentMemberRoles)
6666
{
6767
// roles is a CSV of member groups they need to have access to
68-
var roles = roleString.Split(',').Select(x => x.Trim());
69-
foreach (var role in roles)
70-
{
71-
// Role ? == all anonymous users (User not logged in)
72-
if (role == "?" && _memberManager.IsLoggedIn() == false)
68+
var roles = roleString?.Split(',').Select(x => x.Trim());
69+
if (roles != null)
70+
foreach (var role in roles)
7371
{
74-
return true;
75-
}
72+
// Role ? == all anonymous users (User not logged in)
73+
if (role == "?" && _memberManager.IsLoggedIn() == false)
74+
{
75+
return true;
76+
}
7677

77-
// Role * == all authenticated users
78-
if (role == "*" && _memberManager.IsLoggedIn())
79-
{
80-
return true;
81-
}
78+
// Role * == all authenticated users
79+
if (role == "*" && _memberManager.IsLoggedIn())
80+
{
81+
return true;
82+
}
8283

83-
if (currentMemberRoles.InvariantContains(role))
84-
{
85-
return true;
84+
if (currentMemberRoles.InvariantContains(role))
85+
{
86+
return true;
87+
}
8688
}
87-
}
8889

8990
return false;
9091
}

Our.Umbraco.TagHelpers/Our.Umbraco.TagHelpers.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
4+
<TargetFrameworks>net5.0;net6.0;</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup>
@@ -29,6 +29,7 @@
2929
<PackageIcon>TagHelperLogo.png</PackageIcon>
3030
<PackageTags>Umbraco;TagHelper;TagHelpers;UmbracoCMS</PackageTags>
3131
<PackageReadmeFile>README.md</PackageReadmeFile>
32+
<Nullable>enable</Nullable>
3233
</PropertyGroup>
3334

3435
<ItemGroup>

Our.Umbraco.TagHelpers/OurFallbackTagHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class OurFallbackTagHelper : TagHelper
1717
/// Would just be Header
1818
/// </summary>
1919
[HtmlAttributeName("property")]
20-
public ModelExpression ModelProperty { get; set; }
20+
public ModelExpression? ModelProperty { get; set; }
2121

2222
[HtmlAttributeName("mode")]
2323
public Fallback Mode { get; set; }
@@ -26,17 +26,17 @@ public class OurFallbackTagHelper : TagHelper
2626
/// If using the fallback mode as Language then you can specify the culture to fallback to
2727
/// </summary>
2828
[HtmlAttributeName("culture")]
29-
public string CultureCode { get; set; } = null;
29+
public string? CultureCode { get; set; } = null;
3030

3131
[HtmlAttributeNotBound]
3232
[ViewContext]
33-
public ViewContext ViewContext { get; set; }
33+
public ViewContext? ViewContext { get; set; }
3434

3535
public override void Process(TagHelperContext context, TagHelperOutput output)
3636
{
3737
// Check to see that the Model Property is NOT a complex type or a collection of things
38-
var isComplexType = ModelProperty.ModelExplorer.Metadata.IsComplexType;
39-
var isEnumerableType = ModelProperty.ModelExplorer.Metadata.IsEnumerableType;
38+
var isComplexType = ModelProperty != null && ModelProperty.ModelExplorer.Metadata.IsComplexType;
39+
var isEnumerableType = ModelProperty != null && ModelProperty.ModelExplorer.Metadata.IsEnumerableType;
4040

4141
// Only can support simpler things such as strings (as if collection)
4242
// We have no way of knowing how they want to iterate and display that HTML etc
@@ -47,7 +47,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
4747
}
4848

4949
// Attempt to fetch/cast the Model as IPublishedContent
50-
var contentNode = ViewContext.ViewData.Model as IPublishedContent;
50+
var contentNode = ViewContext?.ViewData.Model as IPublishedContent;
5151
if(contentNode == null)
5252
{
5353
output.SuppressOutput();
@@ -57,7 +57,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
5757
output.TagName = ""; // Remove the outer <our-fallback>
5858

5959
// Get the Model property with fallback mode
60-
var result = contentNode.Value(ModelProperty.Name, fallback: Mode, culture: CultureCode);
60+
var result = contentNode.Value(ModelProperty?.Name, fallback: Mode, culture: CultureCode);
6161

6262
// If we have a value that can be
6363
if (string.IsNullOrWhiteSpace($"{result}") == false)

Our.Umbraco.TagHelpers/Services/BackofficeUserAccessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public ClaimsIdentity BackofficeUser
3636
return new ClaimsIdentity();
3737

3838
CookieAuthenticationOptions cookieOptions = _cookieOptionsSnapshot.Get(global::Umbraco.Cms.Core.Constants.Security.BackOfficeAuthenticationType);
39-
string backOfficeCookie = httpContext.Request.Cookies[cookieOptions.Cookie.Name!];
39+
string? backOfficeCookie = httpContext.Request.Cookies[cookieOptions.Cookie.Name!];
4040

4141
if (string.IsNullOrEmpty(backOfficeCookie))
4242
return new ClaimsIdentity();
4343

44-
AuthenticationTicket unprotected = cookieOptions.TicketDataFormat.Unprotect(backOfficeCookie!);
44+
AuthenticationTicket? unprotected = cookieOptions.TicketDataFormat.Unprotect(backOfficeCookie!);
4545
ClaimsIdentity backOfficeIdentity = unprotected!.Principal.GetUmbracoIdentity();
4646

4747
return backOfficeIdentity;

0 commit comments

Comments
 (0)