-
-
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 3 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,56 @@ 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 * 2); | ||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||||
| 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"; | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| using FluentAssertions; | ||
| using Refitter.Core; | ||
| using Refitter.Tests.Build; | ||
| using Refitter.Tests.TestUtilities; | ||
| using Xunit; | ||
|
|
||
| namespace Refitter.Tests.Examples; | ||
|
|
||
| public class OptionalParametersWithDefaultValuesEdgeCasesTests | ||
| { | ||
| [Fact] | ||
| public async Task String_Default_Values_Should_Be_Escaped() | ||
| { | ||
| const string openApiSpec = | ||
| """ | ||
| { | ||
| "openapi": "3.0.1", | ||
| "info": { | ||
| "title": "Test API", | ||
| "version": "v1" | ||
| }, | ||
| "paths": { | ||
| "/api/test": { | ||
| "get": { | ||
| "operationId": "TestEscaping", | ||
| "parameters": [ | ||
| { | ||
| "name": "quotedString", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "string", | ||
| "default": "value with \"quotes\"" | ||
| } | ||
| }, | ||
| { | ||
| "name": "backslashString", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "string", | ||
| "default": "path\\to\\file" | ||
| } | ||
| }, | ||
| { | ||
| "name": "newlineString", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "string", | ||
| "default": "line1\nline2" | ||
| } | ||
| } | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "Success" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| string generatedCode = await GenerateCode(openApiSpec); | ||
|
|
||
| // Verify strings are properly escaped | ||
| generatedCode.Should().Contain("string? quotedString = \"value with \\\"quotes\\\"\""); | ||
| generatedCode.Should().Contain("string? backslashString = \"path\\\\to\\\\file\""); | ||
| generatedCode.Should().Contain("string? newlineString = \"line1\\nline2\""); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Float_Default_Values_Should_Have_Type_Suffix() | ||
| { | ||
| const string openApiSpec = | ||
| """ | ||
| { | ||
| "openapi": "3.0.1", | ||
| "info": { | ||
| "title": "Test API", | ||
| "version": "v1" | ||
| }, | ||
| "paths": { | ||
| "/api/test": { | ||
| "get": { | ||
| "operationId": "TestFloat", | ||
| "parameters": [ | ||
| { | ||
| "name": "floatValue", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "number", | ||
| "format": "float", | ||
| "default": 1.5 | ||
| } | ||
| } | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "Success" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| string generatedCode = await GenerateCode(openApiSpec); | ||
|
|
||
| // Verify float has 'f' suffix | ||
| generatedCode.Should().Contain("float? floatValue = 1.5f"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Decimal_Default_Values_Should_Have_Type_Suffix() | ||
| { | ||
| const string openApiSpec = | ||
| """ | ||
| { | ||
| "openapi": "3.0.1", | ||
| "info": { | ||
| "title": "Test API", | ||
| "version": "v1" | ||
| }, | ||
| "paths": { | ||
| "/api/test": { | ||
| "get": { | ||
| "operationId": "TestDecimal", | ||
| "parameters": [ | ||
| { | ||
| "name": "decimalValue", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "number", | ||
| "format": "decimal", | ||
| "default": 99.99 | ||
| } | ||
| } | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "Success" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| string generatedCode = await GenerateCode(openApiSpec); | ||
|
|
||
| // Verify decimal has 'm' suffix | ||
| generatedCode.Should().Contain("decimal? decimalValue = 99.99m"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Generated_Code_With_Escaped_Strings_Should_Build() | ||
| { | ||
| const string openApiSpec = | ||
| """ | ||
| { | ||
| "openapi": "3.0.1", | ||
| "info": { | ||
| "title": "Test API", | ||
| "version": "v1" | ||
| }, | ||
| "paths": { | ||
| "/api/test": { | ||
| "get": { | ||
| "operationId": "TestBuild", | ||
| "parameters": [ | ||
| { | ||
| "name": "escapedString", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "string", | ||
| "default": "value with \"quotes\" and \\backslashes\\" | ||
| } | ||
| } | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "Success" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| string generatedCode = await GenerateCode(openApiSpec); | ||
| BuildHelper.BuildCSharp(generatedCode).Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Generated_Code_With_Float_Decimal_Should_Build() | ||
| { | ||
| const string openApiSpec = | ||
| """ | ||
| { | ||
| "openapi": "3.0.1", | ||
| "info": { | ||
| "title": "Test API", | ||
| "version": "v1" | ||
| }, | ||
| "paths": { | ||
| "/api/test": { | ||
| "get": { | ||
| "operationId": "TestBuild", | ||
| "parameters": [ | ||
| { | ||
| "name": "floatValue", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "number", | ||
| "format": "float", | ||
| "default": 1.5 | ||
| } | ||
| }, | ||
| { | ||
| "name": "decimalValue", | ||
| "in": "query", | ||
| "required": false, | ||
| "schema": { | ||
| "type": "number", | ||
| "format": "decimal", | ||
| "default": 99.99 | ||
| } | ||
| } | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "Success" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| string generatedCode = await GenerateCode(openApiSpec); | ||
| BuildHelper.BuildCSharp(generatedCode).Should().BeTrue(); | ||
| } | ||
|
|
||
| private static async Task<string> GenerateCode(string openApiSpec) | ||
| { | ||
| var swaggerFile = await SwaggerFileHelper.CreateSwaggerFile(openApiSpec); | ||
| var settings = new RefitGeneratorSettings | ||
| { | ||
| OpenApiPath = swaggerFile, | ||
| OptionalParameters = true | ||
| }; | ||
|
|
||
| var sut = await RefitGenerator.CreateAsync(settings); | ||
| var code = sut.Generate(); | ||
| return code; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.