Skip to content

Commit e0a8616

Browse files
authored
Merge pull request #9470 from dotnet/copilot/fix-null-reference-exception
Fix NullReferenceException in LocalizationModelDeserializer for malformed keys
2 parents a5fc0ea + 2db5f0b commit e0a8616

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,4 @@ Microsoft.TemplateEngine.VC.db
258258

259259
#Rider
260260
.idea/
261+
.nuget/

src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/LocalizationModelDeserializer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ private static IReadOnlyDictionary<string, PostActionLocalizationModel> LoadPost
131131

132132
foreach (var postActionParts in strings.GroupBy(p => p.NameParts.FirstOrDefault()))
133133
{
134+
if (string.IsNullOrEmpty(postActionParts.Key))
135+
{
136+
// Post action with no ID. Ignore.
137+
continue;
138+
}
139+
134140
string postActionId = postActionParts.Key;
135141
string? description = postActionParts.SingleOrDefault(p => p.NameParts.Skip(1).FirstOrDefault() == "description").LocalizedString;
136142
var instructions = LoadManualInstructionModels(postActionParts
@@ -162,6 +168,12 @@ private static IReadOnlyDictionary<string, string> LoadManualInstructionModels(I
162168

163169
foreach (var instructionParts in strings.GroupBy(p => p.NameParts.FirstOrDefault()))
164170
{
171+
if (string.IsNullOrEmpty(instructionParts.Key))
172+
{
173+
// Instruction with no ID. Ignore.
174+
continue;
175+
}
176+
165177
string id = instructionParts.Key;
166178
string? text = instructionParts.SingleOrDefault(p => p.NameParts.Skip(1).FirstOrDefault() == "text").LocalizedString;
167179
results[id] = text;

test/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests/TemplateConfigTests/LocalizationTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public void CanReadDescription(string fileContent, bool errorExpected, string? e
109109
[InlineData(/*lang=json,strict*/ """{ "symbols/someSymbol/displayName": "localizedSymbol" }""", false, "someSymbol", "localizedSymbol", "(null)")]
110110
[InlineData(/*lang=json,strict*/ """{ "symbols/someSymbol/description": "localizedSymbolDescription" }""", false, "someSymbol", "(null)", "localizedSymbolDescription")]
111111
[InlineData(/*lang=json*/ """{ description: ""}""", false, null, null, null)]
112+
// Test case for NullReferenceException fix: malformed symbol key with only "symbols/" prefix
113+
[InlineData(/*lang=json,strict*/ """{ "symbols/": "test" }""", false, null, null, null)]
112114
public void CanReadNonChoiceSymbol(
113115
string fileContent,
114116
bool errorExpected,
@@ -244,6 +246,10 @@ public void CanReadChoiceSymbol(
244246
[InlineData(/*lang=json*/ """{ description: ""}""", false, null, null, null)]
245247
[InlineData(/*lang=json,strict*/ """{ "postActions/pa0/description": "localizedDescription" }""", false, "pa0", "localizedDescription", "(null)")]
246248
[InlineData(/*lang=json,strict*/ """{ "postActions/pa0/manualInstructions/first/text": "localizedDescription" }""", false, "pa0", "(null)", "first*localizedDescription")]
249+
// Test case for NullReferenceException fix: malformed postAction key with only "postActions/" prefix
250+
[InlineData(/*lang=json,strict*/ """{ "postActions/": "test" }""", false, null, null, null)]
251+
// Test case for NullReferenceException fix: malformed manualInstruction key with missing instruction id
252+
[InlineData(/*lang=json,strict*/ """{ "postActions/pa0/manualInstructions//text": "test" }""", false, "pa0", "(null)", "(null)")]
247253
public void CanReadPostAction(
248254
string fileContent,
249255
bool errorExpected,

0 commit comments

Comments
 (0)