Skip to content

Commit 4e0687d

Browse files
committed
Fix: Handle nullable fallback for optional parameter parsing
Ensure the fallback value is properly checked for null before usage. This prevents potential issues when the initializer value is not defined, improving robustness in parameter parsing.
1 parent 9ed8697 commit 4e0687d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/CodeOfChaos.CliArgsParser.Generators/ParameterDto.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ public string GetWithPropertyDictionary() {
5454

5555
if (PropertyType == "bool") return $"parameterDictionary.GetOptionalParameterByPossibleNames<{PropertyType}>({Name}, {ShortName})";
5656

57-
string fallback = Symbol.DeclaringSyntaxReferences
57+
string? fallback = Symbol.DeclaringSyntaxReferences
5858
.Select(r => r.GetSyntax())
5959
.OfType<PropertyDeclarationSyntax>()
60-
.FirstOrDefault()?.Initializer?.Value.ToString() ?? "default";
61-
62-
return $"parameterDictionary.GetOptionalParameterByPossibleNames<{PropertyType}>({Name}, {ShortName}) ?? {fallback}";
60+
.FirstOrDefault()?.Initializer?.Value.ToString();
61+
62+
// ReSharper disable once ConvertIfStatementToReturnStatement
63+
if (fallback is null) return $"parameterDictionary.GetOptionalParameterByPossibleNames<{PropertyType}>({Name}, {ShortName})";
64+
return $"parameterDictionary.GetOptionalParameterByPossibleNames<{PropertyType}?>({Name}, {ShortName}) ?? {fallback}";
6365
}
6466
}

0 commit comments

Comments
 (0)