Skip to content

Commit

Permalink
Added support for nullable reference types
Browse files Browse the repository at this point in the history
See #163
  • Loading branch information
abjerner committed Feb 6, 2023
1 parent 1917a5c commit 51505d2
Show file tree
Hide file tree
Showing 40 changed files with 202 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public RedirectsContentAppFactory(RedirectsBackOfficeHelper backOfficeHelper) {
_backOfficeHelper = backOfficeHelper;
}

public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) {
public ContentApp? GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) {
return _backOfficeHelper.GetContentAppFor(source, userGroups);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public ActionResult GetRootNodes() {
[HttpPost]
public ActionResult AddRedirect([FromBody] JObject m) {

AddRedirectOptions model = m.ToObject<AddRedirectOptions>();
AddRedirectOptions? model = m.ToObject<AddRedirectOptions>();

try {

// Some input validation
if (model == null) throw new RedirectsException("Failed parsing request body.");
if (string.IsNullOrWhiteSpace(model.OriginalUrl)) throw new RedirectsException(_backOffice.Localize("errorNoUrl"));
if (string.IsNullOrWhiteSpace(model.Destination?.Url)) throw new RedirectsException(_backOffice.Localize("errorNoDestination"));
if (string.IsNullOrWhiteSpace(model.Destination.Url)) throw new RedirectsException(_backOffice.Localize("errorNoDestination"));

// Add the redirect
IRedirect redirect = _redirects.AddRedirect(model);
Expand All @@ -105,7 +105,7 @@ public ActionResult EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions
try {

// Get a reference to the redirect
IRedirect redirect = _redirects.GetRedirectByKey(redirectId);
IRedirect? redirect = _redirects.GetRedirectByKey(redirectId);
if (redirect == null) throw new RedirectNotFoundException();

// Some input validation
Expand Down Expand Up @@ -163,7 +163,7 @@ public ActionResult EditRedirect(Guid rootNodeKey, Guid redirectId, string url,
try {

// Get a reference to the redirect
IRedirect redirect = _redirects.GetRedirectByKey(redirectId);
IRedirect? redirect = _redirects.GetRedirectByKey(redirectId);
if (redirect == null) throw new RedirectNotFoundException();

// Some input validation
Expand All @@ -185,7 +185,7 @@ public ActionResult EditRedirect(Guid rootNodeKey, Guid redirectId, string url,
Id = linkId,
Key = linkKey,
Type = type,
Name = redirect.Destination?.Name
Name = redirect.Destination.Name
};

// Split the URL and query string
Expand Down Expand Up @@ -228,7 +228,7 @@ public ActionResult DeleteRedirect(Guid redirectId) {
try {

// Get a reference to the redirect
IRedirect redirect = _redirects.GetRedirectByKey(redirectId);
IRedirect? redirect = _redirects.GetRedirectByKey(redirectId);
if (redirect == null) throw new RedirectNotFoundException();

// Delete the redirect
Expand Down Expand Up @@ -258,7 +258,7 @@ public ActionResult DeleteRedirect(Guid redirectId) {
/// <param name="rootNodeKey">The root node key that the returned redirects should match. <c>null</c> means all redirects. <see cref="Guid.Empty"/> means all global redirects.</param>
/// <returns>A list of redirects.</returns>
[HttpGet]
public ActionResult GetRedirects(int page = 1, int limit = 20, string type = null, string text = null, Guid? rootNodeKey = null) {
public ActionResult GetRedirects(int page = 1, int limit = 20, string? type = null, string? text = null, Guid? rootNodeKey = null) {

try {

Expand Down Expand Up @@ -300,15 +300,15 @@ public ActionResult GetRedirectsForNode(string type, Guid key) {
case "content":

// Get a reference to the content item
IContent content1 = _contentService.GetById(key);
IContent? content1 = _contentService.GetById(key);

// Trigger an exception if the content item couldn't be found
if (content1 == null) throw new RedirectsException(HttpStatusCode.NotFound, _backOffice.Localize("errorContentNoRedirects"));

// Look up the content via the content cahce
IPublishedContent content2 = null;
if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext umbraco)) {
content2 = umbraco.Content.GetById(key);
IPublishedContent? content2 = null;
if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? umbraco)) {
content2 = umbraco.Content?.GetById(key);
}

// Initialize a new model instance
Expand All @@ -319,15 +319,15 @@ public ActionResult GetRedirectsForNode(string type, Guid key) {
case "media":

// Get a reference to the media item
IMedia media1 = _mediaService.GetById(key);
IMedia? media1 = _mediaService.GetById(key);

// Trigger an exception if the media item couldn't be found
if (media1 == null) throw new RedirectsException(HttpStatusCode.NotFound, _backOffice.Localize("errorContentNoRedirects"));

// Look up the media via the content cahce
IPublishedContent media2 = null;
IPublishedContent? media2 = null;
if (_umbracoContextAccessor.TryGetUmbracoContext(out umbraco)) {
media2 = umbraco.Content.GetById(key);
media2 = umbraco.Content?.GetById(key);
}

// Initialize a new model instance
Expand Down
16 changes: 8 additions & 8 deletions src/Skybrud.Umbraco.Redirects/Extensions/RedirectsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class RedirectsExtensions {
/// <param name="redirect">The redirect.</param>
/// <param name="redirectsService">A reference to the current redirects service.</param>
/// <returns>The destination URL.</returns>
public static string GetDestinationUrl(this OutboundRedirect redirect, IRedirectsService redirectsService) {
public static string? GetDestinationUrl(this OutboundRedirect redirect, IRedirectsService redirectsService) {
return redirectsService.GetDestinationUrl(redirect);
}

Expand All @@ -36,7 +36,7 @@ public static string GetDestinationUrl(this OutboundRedirect redirect, IRedirect
/// <param name="uri">The inbound URL.</param>
/// <param name="redirectsService">A reference to the current redirects service.</param>
/// <returns>The destination URL.</returns>
public static string GetDestinationUrl(this OutboundRedirect redirect, Uri uri, IRedirectsService redirectsService) {
public static string? GetDestinationUrl(this OutboundRedirect redirect, Uri uri, IRedirectsService redirectsService) {
return redirectsService.GetDestinationUrl(redirect, uri);
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public static T SetDestination<T>(this T redirect, IPublishedContent content) wh
return SetDestination(redirect, new RedirectDestination {
Id = content.Id,
Key = content.Key,
Name = content.Name,
Name = content.Name!,
Url = content.Url(),
Query = string.Empty,
Fragment = string.Empty,
Expand All @@ -101,7 +101,7 @@ public static T SetDestination<T>(this T redirect, IPublishedContent content) wh
return SetDestination(redirect, new RedirectDestination {
Id = content.Id,
Key = content.Key,
Name = content.Name,
Name = content.Name!,
Url = content.Url(),
Query = string.Empty,
Fragment = string.Empty,
Expand Down Expand Up @@ -153,7 +153,7 @@ public static IOutboundRedirect GetOutboundRedirect(this IPublishedContent conte
/// <returns>An instance of <see cref="IOutboundRedirect"/>.</returns>
public static IOutboundRedirect GetOutboundRedirect(this IPublishedContent content, string propertyAlias) {
if (string.IsNullOrWhiteSpace(propertyAlias)) throw new ArgumentNullException(nameof(propertyAlias));
IOutboundRedirect redirect = content.Value(propertyAlias) as IOutboundRedirect;
IOutboundRedirect? redirect = content.Value(propertyAlias) as IOutboundRedirect;
return redirect ?? new OutboundRedirect();
}

Expand Down Expand Up @@ -188,7 +188,7 @@ public static IOutboundRedirect GetOutboundRedirect(this IPublishedContent conte
/// </summary>
/// <param name="content">The content item holding the outbound rediderect.</param>
/// <returns>An instance of <see cref="IOutboundRedirect"/> if successful; otherwise, <see langword="null"/>.</returns>
public static IOutboundRedirect GetOutboundRedirectOrDefault(this IPublishedContent content) {
public static IOutboundRedirect? GetOutboundRedirectOrDefault(this IPublishedContent content) {
return GetOutboundRedirectOrDefault(content, OutboundPropertyAliases);
}

Expand All @@ -201,7 +201,7 @@ public static IOutboundRedirect GetOutboundRedirectOrDefault(this IPublishedCont
/// <param name="content">The content item holding the outbound rediderect.</param>
/// <param name="propertyAlias">The alias of the property.</param>
/// <returns>An instance of <see cref="IOutboundRedirect"/> if successful; otherwise, <see langword="null"/>.</returns>
public static IOutboundRedirect GetOutboundRedirectOrDefault(this IPublishedContent content, string propertyAlias) {
public static IOutboundRedirect? GetOutboundRedirectOrDefault(this IPublishedContent content, string propertyAlias) {
if (string.IsNullOrWhiteSpace(propertyAlias)) throw new ArgumentNullException(nameof(propertyAlias));
return content.Value(propertyAlias) as IOutboundRedirect;
}
Expand All @@ -215,7 +215,7 @@ public static IOutboundRedirect GetOutboundRedirectOrDefault(this IPublishedCont
/// <param name="content">The content item holding the outbound rediderect.</param>
/// <param name="propertyAliases">The aliases of the properties.</param>
/// <returns>An instance of <see cref="IOutboundRedirect"/> if successful; otherwise, <see langword="null"/>.</returns>
public static IOutboundRedirect GetOutboundRedirectOrDefault(this IPublishedContent content, params string[] propertyAliases) {
public static IOutboundRedirect? GetOutboundRedirectOrDefault(this IPublishedContent content, params string[] propertyAliases) {

if (propertyAliases == null) throw new ArgumentNullException(nameof(propertyAliases));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public RedirectsModelsFactory(IUmbracoContextAccessor umbracoContextAccessor) {
/// </summary>
/// <param name="json">A <see cref="JObject"/> instance representing the outbound redirect.</param>
/// <returns>An instance of <see cref="OutboundRedirect"/>.</returns>
public virtual IOutboundRedirect CreateOutboundRedirect(JObject json) {
public virtual IOutboundRedirect? CreateOutboundRedirect(JObject? json) {

if (json == null) return null;

Expand All @@ -56,19 +56,19 @@ public virtual IOutboundRedirect CreateOutboundRedirect(JObject json) {
bool forward = json.GetBoolean("forward");

// Parse the destination
RedirectDestination destination = json.GetObject("destination", RedirectDestination.Parse);
RedirectDestination? destination = json.GetObject("destination", RedirectDestination.Parse);
if (destination is not { IsValid: true }) return null;

// Look up the current URL for content and media
switch (destination.Type) {

case RedirectDestinationType.Content:
IPublishedContent content = UmbracoContext?.Content?.GetById(destination.Key);
IPublishedContent? content = UmbracoContext.Content?.GetById(destination.Key);
if (content != null) destination.Url = content.Url();
break;

case RedirectDestinationType.Media:
IPublishedContent media = UmbracoContext?.Media?.GetById(destination.Key);
IPublishedContent? media = UmbracoContext.Media?.GetById(destination.Key);
if (media != null) destination.Url = media.Url();
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ public class OutboundRedirectReferenceFactory : IDataValueReferenceFactory, IDat

public IDataValueReference GetDataValueReference() => this;

public IEnumerable<UmbracoEntityReference> GetReferences(object value) {
public IEnumerable<UmbracoEntityReference> GetReferences(object? value) {

List<UmbracoEntityReference> references = new List<UmbracoEntityReference>();
if (value is not string json) return references;

IRedirectDestination destination = OutboundRedirect.Deserialize(json)?.Destination;
if (destination == null) return references;
IRedirectDestination destination = OutboundRedirect.Deserialize(json).Destination;

switch (destination.Type) {

Expand All @@ -39,7 +38,7 @@ public IEnumerable<UmbracoEntityReference> GetReferences(object value) {

}

public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(OutboundRedirectEditor.EditorAlias);
public bool IsForEditor(IDataEditor? dataEditor) => dataEditor is not null && dataEditor.Alias.InvariantEquals(OutboundRedirectEditor.EditorAlias);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RedirectsBackOfficeHelper {
/// <summary>
/// Gets a reference to the current backoffice user.
/// </summary>
public IUser CurrentUser => Dependencies.BackOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
public IUser? CurrentUser => Dependencies.BackOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;

/// <summary>
/// Gets a reference to the redirects settings.
Expand Down Expand Up @@ -195,12 +195,12 @@ private RedirectModel Map(IRedirect redirect, Dictionary<Guid, RedirectRootNodeM

string backOfficeBaseUrl = Dependencies.GlobalSettings.GetBackOfficePath(Dependencies.HostingEnvironment);

RedirectRootNodeModel rootNode = null;
RedirectRootNodeModel? rootNode = null;
if (redirect.RootKey != Guid.Empty) {

if (!rootNodeLookup.TryGetValue(redirect.RootKey, out rootNode)) {

if (!contentLookup.TryGetValue(redirect.RootKey, out IContent content)) {
if (!contentLookup.TryGetValue(redirect.RootKey, out IContent? content)) {
content = Dependencies.ContentService.GetById(redirect.RootKey);
if (content != null) contentLookup.Add(content.Key, content);
}
Expand All @@ -215,7 +215,7 @@ private RedirectModel Map(IRedirect redirect, Dictionary<Guid, RedirectRootNodeM
RedirectDestinationModel destination;
if (redirect.Destination.Type == RedirectDestinationType.Content) {

if (!contentLookup.TryGetValue(redirect.Destination.Key, out IContent content)) {
if (!contentLookup.TryGetValue(redirect.Destination.Key, out IContent? content)) {
content = Dependencies.ContentService.GetById(redirect.Destination.Key);
if (content != null) contentLookup.Add(content.Key, content);
}
Expand All @@ -226,7 +226,7 @@ private RedirectModel Map(IRedirect redirect, Dictionary<Guid, RedirectRootNodeM

} else if (redirect.Destination.Type == RedirectDestinationType.Media) {

if (!mediaLookup.TryGetValue(redirect.Destination.Key, out IMedia media)) {
if (!mediaLookup.TryGetValue(redirect.Destination.Key, out IMedia? media)) {
media = Dependencies.MediaService.GetById(redirect.Destination.Key);
if (media != null) mediaLookup.Add(media.Key, media);
}
Expand All @@ -251,7 +251,7 @@ private RedirectModel Map(IRedirect redirect, Dictionary<Guid, RedirectRootNodeM
/// <param name="source">The source - eg. an instance <see cref="IContent"/>.</param>
/// <param name="userGroups">The user groups of the current user.</param>
/// <returns>An instance of <see cref="ContentApp"/>, or <c>null</c> if no content app should be shown.</returns>
public virtual ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) {
public virtual ContentApp? GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) {

// Return null if the content app is disabled via appsettings.json
if (!Settings.ContentApp.Enabled) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public async Task InvokeAsync(HttpContext context) {

// Get the destination URL from the arguments (in case a value has been set
// from an notification handler)
string destinationUrl = preLookup.DestinationUrl;
string? destinationUrl = preLookup.DestinationUrl;

// Declare a variable for the redirect (either from the pre lookup or a lookup via the service)
IRedirect redirect = preLookup.Redirect ?? _redirectsService.GetRedirectByRequest(context.Request);
IRedirect? redirect = preLookup.Redirect ?? _redirectsService.GetRedirectByRequest(context.Request);

// Return if we neither have a redirect or a destination URL
if (redirect == null && string.IsNullOrWhiteSpace(destinationUrl)) return Task.CompletedTask;
Expand All @@ -72,7 +72,7 @@ public async Task InvokeAsync(HttpContext context) {
RedirectType redirectType = preLookup.RedirectType ?? redirect?.Type ?? RedirectType.Temporary;

// Calculate the destination URL
destinationUrl ??= _redirectsService.GetDestinationUrl(redirect, uri);
if (redirect is not null) destinationUrl ??= _redirectsService.GetDestinationUrl(redirect, uri);

// Invoke the post lookup event
RedirectPostLookupNotification postLookup = new(context, redirect, redirectType, destinationUrl);
Expand Down
Loading

0 comments on commit 51505d2

Please sign in to comment.