Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 77 additions & 5 deletions src/Refitter.Core/ParameterExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Copilot AI Nov 9, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EscapeString method should handle additional escape sequences for completeness. Consider adding cases for:

  • Form feed (\f\\f)
  • Vertical tab (\v\\v)
  • Backspace (\b\\b)

While these are less common in API parameters, handling them ensures robustness when dealing with various string defaults.

Copilot uses AI. Check for mistakes.
Comment on lines +171 to +211

Copilot AI Nov 9, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EscapeString method does not handle the null character (\0) which can appear in string defaults. If a string contains \0, it should be escaped as \\0 to be valid in C# string literals. Consider adding a case for '\0' that appends "\\0".

Copilot uses AI. Check for mistakes.

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",

Copilot AI Nov 9, 2025

Copy link

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 long and ulong type suffixes. In C#, large integer values that exceed int.MaxValue (2,147,483,647) must have an L suffix to be treated as long literals. Without the suffix, values like long? value = 3000000000 will cause a compilation error. Add cases for:

  • "long" or "Int64" → append L
  • "ulong" or "UInt64" → append UL

Additionally, consider appending U for uint types to be explicit, though this is optional for values within int range.

Suggested change
"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 uses AI. Check for mistakes.
"double" or "Double" => FormatDoubleLiteral(numericString),
"long" or "Int64" => $"{numericString}L",
"ulong" or "UInt64" => $"{numericString}UL",
"uint" or "UInt32" => $"{numericString}U",
_ => numericString
};
}

Comment on lines +222 to +230

Copilot AI Nov 9, 2025

Copy link

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.

Suggested change
"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";
}

Copilot uses AI. Check for mistakes.
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";
}

private static bool IsNumericType(string type)
{
return type is "int" or "Int32" or "long" or "Int64" or "short" or "Int16"
Expand Down
Loading
Loading