From cc8cb96140218a289850071120f956ce4420a529 Mon Sep 17 00:00:00 2001 From: Stefano Chiodino Date: Thu, 15 Nov 2018 07:23:09 +0000 Subject: [PATCH] The change to nullable expands to even more neighbors... --- .../Published/NestedContentTests.cs | 2 +- .../PublishedContentDataTableTests.cs | 2 +- .../PublishedContent/SolidPublishedSnapshot.cs | 2 +- .../TestHelpers/Stubs/TestPublishedContent.cs | 2 +- src/Umbraco.Web/Models/PublishedContentBase.cs | 2 +- .../DataSource/BTree.ContentDataSerializer.cs | 7 +++++-- .../NuCache/DataSource/ContentData.cs | 2 +- .../PublishedCache/NuCache/PublishedContent.cs | 2 +- .../PublishedCache/PublishedMember.cs | 2 +- .../DictionaryPublishedContent.cs | 2 +- .../XmlPublishedCache/XmlPublishedContent.cs | 2 +- src/Umbraco.Web/PublishedContentExtensions.cs | 9 +++++++-- src/Umbraco.Web/Routing/PublishedRouter.cs | 9 ++++++--- src/Umbraco.Web/Templates/TemplateRenderer.cs | 16 +++++++++------- 14 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/Umbraco.Tests/Published/NestedContentTests.cs b/src/Umbraco.Tests/Published/NestedContentTests.cs index 920fa2acd5..702187dd9e 100644 --- a/src/Umbraco.Tests/Published/NestedContentTests.cs +++ b/src/Umbraco.Tests/Published/NestedContentTests.cs @@ -270,7 +270,7 @@ namespace Umbraco.Tests.Published // ReSharper disable UnassignedGetOnlyAutoProperty public override int Id { get; } - public override int TemplateId { get; } + public override int? TemplateId { get; } public override int SortOrder { get; } public override string Name { get; } public override PublishedCultureInfo GetCulture(string culture = ".") => throw new NotSupportedException(); diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs index a640423515..9d47d77e6e 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs @@ -201,7 +201,7 @@ namespace Umbraco.Tests.PublishedContent public IPublishedContent Parent { get; set; } public int Id { get; set; } public Guid Key { get; set; } - public int TemplateId { get; set; } + public int? TemplateId { get; set; } public int SortOrder { get; set; } public string Name { get; set; } public PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); diff --git a/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs b/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs index efd1c6ae8b..d62a98ab15 100644 --- a/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs +++ b/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs @@ -173,7 +173,7 @@ namespace Umbraco.Tests.PublishedContent public int Id { get; set; } public Guid Key { get; set; } - public int TemplateId { get; set; } + public int? TemplateId { get; set; } public int SortOrder { get; set; } public string Name { get; set; } public PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs index 0faf1537b3..0fb1aba576 100644 --- a/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs @@ -15,7 +15,7 @@ namespace Umbraco.Tests.TestHelpers.Stubs } public int Id { get; } - public int TemplateId { get; set; } + public int? TemplateId { get; set; } public int SortOrder { get; set; } public string Name { get; set; } public IVariationContextAccessor VariationContextAccessor { get; set; } diff --git a/src/Umbraco.Web/Models/PublishedContentBase.cs b/src/Umbraco.Web/Models/PublishedContentBase.cs index 1b8128b4c0..b85ec0a0d1 100644 --- a/src/Umbraco.Web/Models/PublishedContentBase.cs +++ b/src/Umbraco.Web/Models/PublishedContentBase.cs @@ -51,7 +51,7 @@ namespace Umbraco.Web.Models public abstract string Path { get; } /// - public abstract int TemplateId { get; } + public abstract int? TemplateId { get; } /// public abstract int CreatorId { get; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/BTree.ContentDataSerializer.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/BTree.ContentDataSerializer.cs index 7c793b69bd..80633efe2e 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/BTree.ContentDataSerializer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/BTree.ContentDataSerializer.cs @@ -30,9 +30,12 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource PrimitiveSerializer.Int32.WriteTo(value.VersionId, stream); PrimitiveSerializer.DateTime.WriteTo(value.VersionDate, stream); PrimitiveSerializer.Int32.WriteTo(value.WriterId, stream); - PrimitiveSerializer.Int32.WriteTo(value.TemplateId, stream); + if (value.TemplateId.HasValue) + { + PrimitiveSerializer.Int32.WriteTo(value.TemplateId.Value, stream); + } PropertiesSerializer.WriteTo(value.Properties, stream); CultureVariationsSerializer.WriteTo(value.CultureInfos, stream); } } -} \ No newline at end of file +} diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/ContentData.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/ContentData.cs index 4721a1f4ca..520cc99011 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/ContentData.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/ContentData.cs @@ -10,7 +10,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource public int VersionId { get; set; } public DateTime VersionDate { get; set; } public int WriterId { get; set; } - public int TemplateId { get; set; } + public int? TemplateId { get; set; } public bool Published { get; set; } public IDictionary Properties { get; set; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs index a4610e82db..5d4e007212 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs @@ -215,7 +215,7 @@ namespace Umbraco.Web.PublishedCache.NuCache public override string Path => _contentNode.Path; /// - public override int TemplateId => _contentData.TemplateId; + public override int? TemplateId => _contentData.TemplateId; /// public override int CreatorId => _contentNode.CreatorId; diff --git a/src/Umbraco.Web/PublishedCache/PublishedMember.cs b/src/Umbraco.Web/PublishedCache/PublishedMember.cs index 44ce2328b7..de9831ffeb 100644 --- a/src/Umbraco.Web/PublishedCache/PublishedMember.cs +++ b/src/Umbraco.Web/PublishedCache/PublishedMember.cs @@ -129,7 +129,7 @@ namespace Umbraco.Web.PublishedCache public override Guid Key => _member.Key; - public override int TemplateId => throw new NotSupportedException(); + public override int? TemplateId => throw new NotSupportedException(); public override int SortOrder => 0; diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs index 49e0d1e9d2..201607593c 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs @@ -148,7 +148,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache public override Guid Key => _key; - public override int TemplateId => 0; + public override int? TemplateId => null; public override int SortOrder => _sortOrder; diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs index 3c143a6066..1643e6219d 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs @@ -109,7 +109,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache } } - public override int TemplateId + public override int? TemplateId { get { diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index f0ddf62074..79c7e27d98 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -132,10 +132,15 @@ namespace Umbraco.Web /// Returns the current template Alias /// /// - /// + /// Empty string if none is set. public static string GetTemplateAlias(this IPublishedContent content) { - var template = Current.Services.FileService.GetTemplate(content.TemplateId); + if(content.TemplateId.HasValue == false) + { + return string.Empty; + } + + var template = Current.Services.FileService.GetTemplate(content.TemplateId.Value); return template == null ? string.Empty : template.Alias; } diff --git a/src/Umbraco.Web/Routing/PublishedRouter.cs b/src/Umbraco.Web/Routing/PublishedRouter.cs index 06c23406ab..9b1a614bb5 100644 --- a/src/Umbraco.Web/Routing/PublishedRouter.cs +++ b/src/Umbraco.Web/Routing/PublishedRouter.cs @@ -755,9 +755,9 @@ namespace Umbraco.Web.Routing } } - private ITemplate GetTemplateModel(int templateId) + private ITemplate GetTemplateModel(int? templateId) { - if (templateId <= 0) + if (templateId.HasValue == false) { _logger.Debug("GetTemplateModel: No template."); return null; @@ -765,7 +765,10 @@ namespace Umbraco.Web.Routing _logger.Debug("GetTemplateModel: Get template id={TemplateId}", templateId); - var template = _services.FileService.GetTemplate(templateId); + if (templateId == null) + throw new InvalidOperationException("The template is not set, the page cannot render."); + + var template = _services.FileService.GetTemplate(templateId.Value); if (template == null) throw new InvalidOperationException("The template with Id " + templateId + " does not exist, the page cannot render."); _logger.Debug("GetTemplateModel: Got template id={TemplateId} alias={TemplateAlias}", template.Id, template.Alias); diff --git a/src/Umbraco.Web/Templates/TemplateRenderer.cs b/src/Umbraco.Web/Templates/TemplateRenderer.cs index e0b5c8c301..a94a489c5b 100644 --- a/src/Umbraco.Web/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web/Templates/TemplateRenderer.cs @@ -36,7 +36,7 @@ namespace Umbraco.Web.Templates { if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext)); PageId = pageId; - AltTemplate = altTemplateId; + AltTemplateId = altTemplateId; _umbracoContext = umbracoContext; } @@ -52,7 +52,7 @@ namespace Umbraco.Web.Templates /// /// Gets/sets the alt template to render if there is one /// - public int? AltTemplate { get; } + public int? AltTemplateId { get; } public void Render(StringWriter writer) { @@ -90,20 +90,22 @@ namespace Umbraco.Web.Templates //set the doc that was found by id contentRequest.PublishedContent = doc; //set the template, either based on the AltTemplate found or the standard template of the doc - contentRequest.TemplateModel = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false - ? _fileService.GetTemplate(doc.TemplateId) - : _fileService.GetTemplate(AltTemplate.Value); + var templateId = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplateId.HasValue == false + ? doc.TemplateId + : AltTemplateId.Value; + if (templateId.HasValue) + contentRequest.TemplateModel = _fileService.GetTemplate(templateId.Value); //if there is not template then exit if (contentRequest.HasTemplate == false) { - if (AltTemplate.HasValue == false) + if (AltTemplateId.HasValue == false) { writer.Write("", doc.TemplateId); } else { - writer.Write("", AltTemplate); + writer.Write("", AltTemplateId); } return; }