diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticDescriptor.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticDescriptor.cs index 5ae3e9bb433a0916e8fea54ee3ff34d4f145e978..5d794fc4453f7015672fa0a6354115c6281a5f1c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticDescriptor.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorDiagnosticDescriptor.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -35,7 +35,16 @@ namespace Microsoft.AspNetCore.Razor.Language public RazorDiagnosticSeverity Severity { get; } - public string GetMessageFormat() => _messageFormat(); + public string GetMessageFormat() + { + var message = _messageFormat(); + if (string.IsNullOrEmpty(message)) + { + return Resources.FormatRazorDiagnosticDescriptor_DefaultError(Id); + } + + return message; + } public override bool Equals(object obj) { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Resources.resx b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Resources.resx index 6d541a509f83a86cbb029e5a5a4d63e9dffcdcca..947c5d2c47d04e130ad531f63bbd53cab5679280 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Resources.resx +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Resources.resx @@ -574,4 +574,7 @@ <data name="Component_EditorRequiredParameterNotSpecified" xml:space="preserve"> <value>Component '{0}' expects a value for the parameter '{1}', but a value may not have been provided.</value> </data> -</root> + <data name="RazorDiagnosticDescriptor_DefaultError" xml:space="preserve"> + <value>Encountered diagnostic '{0}'.</value> + </data> +</root> \ No newline at end of file diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticDescriptorTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticDescriptorTest.cs index ad8f49d41088e642b8a4eefbd3d2c8dd102d6e0c..e2f9eccbd57ef7ec4d00d217d97d0e2c6bb694eb 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticDescriptorTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticDescriptorTest.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Xunit; @@ -74,5 +74,17 @@ namespace Microsoft.AspNetCore.Razor.Language // Assert Assert.False(result); } + + [Fact] + public void RazorDiagnosticDescriptor_NullMessage() + { + // Arrange & Act + var descriptor = new RazorDiagnosticDescriptor("RZ0001", () => null, RazorDiagnosticSeverity.Error); + + // Assert + Assert.Equal("RZ0001", descriptor.Id); + Assert.Equal(RazorDiagnosticSeverity.Error, descriptor.Severity); + Assert.Equal("Encountered diagnostic 'RZ0001'.", descriptor.GetMessageFormat()); + } } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticTest.cs index 77c988ff00aaf79869aa3d636234284bcb8b62ae..6b6897050d9e8986dbd304a27be4e082b60a7412 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorDiagnosticTest.cs @@ -1,6 +1,7 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Globalization; using Microsoft.AspNetCore.Razor.Language.Legacy; using Xunit; @@ -41,5 +42,20 @@ namespace Microsoft.AspNetCore.Razor.Language Assert.Equal(RazorDiagnosticSeverity.Error, defaultDiagnostic.Severity); Assert.Equal(span, diagnostic.Span); } + + [Fact] + public void GetMessage_WithNullDescriptorFormat_ReturnsDefaultErrorString() + { + // Arrange + var descriptor = new RazorDiagnosticDescriptor("RZ0001", () => null, RazorDiagnosticSeverity.Error); + var span = new SourceSpan("test.cs", 15, 1, 8, 5); + + // Act + var diagnostic = RazorDiagnostic.Create(descriptor, span, "Hello", "World"); + var message = diagnostic.GetMessage(CultureInfo.InvariantCulture); + + // Assert + Assert.Equal("Encountered diagnostic 'RZ0001'.", message); + } } }