-
-
Notifications
You must be signed in to change notification settings - Fork 64
Fix string escaping, null safety, and numeric literals in optional parameter default values #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
01f7e14
797b138
e17d732
7402111
c7ca8f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -145,9 +145,11 @@ private static string GetDefaultValueForParameter(string parameterString, IColle | |||||||||||||||||||||||||||||||||||||||||
| var variableName = parts[parts.Length - 1].TrimEnd(';', ','); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| var parameterModel = parameterModels.FirstOrDefault(p => p.VariableName == variableName); | ||||||||||||||||||||||||||||||||||||||||||
| return parameterModel?.Schema?.Default != null | ||||||||||||||||||||||||||||||||||||||||||
| ? FormatDefaultValue(parameterModel.Schema.Default, parameterModel.Type) | ||||||||||||||||||||||||||||||||||||||||||
| : "default"; | ||||||||||||||||||||||||||||||||||||||||||
| if (parameterModel?.Schema?.Default != null && !string.IsNullOrEmpty(parameterModel.Type)) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| return FormatDefaultValue(parameterModel.Schema.Default, parameterModel.Type); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return "default"; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private static string FormatDefaultValue(object? defaultValue, string parameterType) | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -160,12 +162,82 @@ private static string FormatDefaultValue(object? defaultValue, string parameterT | |||||||||||||||||||||||||||||||||||||||||
| return type switch | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| "bool" => defaultValue.ToString()?.ToLowerInvariant() ?? "default", | ||||||||||||||||||||||||||||||||||||||||||
| "string" => $"\"{defaultValue}\"", | ||||||||||||||||||||||||||||||||||||||||||
| _ when IsNumericType(type) => defaultValue.ToString() ?? "default", | ||||||||||||||||||||||||||||||||||||||||||
| "string" => $"\"{EscapeString(defaultValue.ToString() ?? string.Empty)}\"", | ||||||||||||||||||||||||||||||||||||||||||
| _ when IsNumericType(type) => FormatNumericValue(defaultValue, type), | ||||||||||||||||||||||||||||||||||||||||||
| _ => "default" | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private static string EscapeString(string value) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| var sb = new StringBuilder(value.Length + 10); | ||||||||||||||||||||||||||||||||||||||||||
| foreach (var c in value) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| switch (c) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| case '\\': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\\\"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '"': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\\""); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\n': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\n"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\r': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\r"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\t': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\t"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\f': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\f"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\v': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\v"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\b': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\b"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| case '\0': | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\\0"); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||
| sb.Append(c); | ||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return sb.ToString(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+171
to
+211
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private static string FormatNumericValue(object defaultValue, string type) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| var numericString = defaultValue is IFormattable formattable | ||||||||||||||||||||||||||||||||||||||||||
| ? formattable.ToString(null, CultureInfo.InvariantCulture) | ||||||||||||||||||||||||||||||||||||||||||
| : (defaultValue.ToString() ?? "default"); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return type switch | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| "float" or "Single" => $"{numericString}f", | ||||||||||||||||||||||||||||||||||||||||||
| "decimal" or "Decimal" => $"{numericString}m", | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| "decimal" or "Decimal" => $"{numericString}m", | |
| "decimal" or "Decimal" => $"{numericString}m", | |
| "long" or "Int64" => $"{numericString}L", | |
| "ulong" or "UInt64" => $"{numericString}UL", | |
| "uint" or "UInt32" => $"{numericString}U", |
Copilot
AI
Nov 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FormatNumericValue method doesn't handle the double type suffix. While doubles don't require a suffix in most cases, if the numeric value is an integer (e.g., 1, 10), it will be treated as int by default in C# unless explicitly cast. For double types with integer values, you should append .0 to ensure the literal is treated as a double. For example, double? value = 10 should be double? value = 10.0 or double? value = 10d.
| "decimal" or "Decimal" => $"{numericString}m", | |
| _ => numericString | |
| }; | |
| } | |
| "decimal" or "Decimal" => $"{numericString}m", | |
| "double" or "Double" => FormatDoubleLiteral(numericString), | |
| _ => numericString | |
| }; | |
| } | |
| // Ensures that double literals are formatted with .0 if integer, or as-is if already floating-point | |
| private static string FormatDoubleLiteral(string numericString) | |
| { | |
| // If the string already contains a decimal point or exponent, return as-is | |
| if (numericString.Contains('.') || numericString.Contains('e') || numericString.Contains('E')) | |
| return numericString; | |
| // Otherwise, append .0 to make it a double literal | |
| return numericString + ".0"; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
EscapeStringmethod should handle additional escape sequences for completeness. Consider adding cases for:\f→\\f)\v→\\v)\b→\\b)While these are less common in API parameters, handling them ensures robustness when dealing with various string defaults.