Skip to content

Commit 36674c4

Browse files
authored
Escape strings when creating descriptions (#39)
* Escape strings when creating descriptions Descriptions are just splatted into the generated code wholesale, not fixing up any characters that are invalid C#, such as newlines, backslashes, and the like. These are now appropriately escaped for use in a C# string without errors. * Remove unnecessary using
1 parent 49371f1 commit 36674c4

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/libs/CSharpToJsonSchema.Generators/Sources.Method.Tools.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void AddAllTools()
4545
tool = new global::CSharpToJsonSchema.Tool
4646
{{
4747
Name = ""{method.Name}"",
48-
Description = ""{method.Description}"",
48+
Description = ""{GetDescriptionStringAsValidCSharp(method.Description)}"",
4949
Strict = {(method.IsStrict ? "true" : "false")},
5050
Parameters = global::CSharpToJsonSchema.SchemaBuilder.ConvertToSchema(global::{@interface.Namespace}.{extensionsClassName}JsonSerializerContext.Default.{method.Name}Args,{"\""}{GetDictionaryString(method)}{"\""}),
5151
}};
@@ -108,4 +108,4 @@ private static string GetDelegateInputsTypes(MethodData first, bool addReturnTyp
108108
f.Add(first.ReturnType.ToDisplayString());
109109
return string.Join(", ", f);
110110
}
111-
}
111+
}

src/libs/CSharpToJsonSchema.Generators/Sources.Tools.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using CSharpToJsonSchema.Generators.Models;
44
using H.Generators;
55
using H.Generators.Extensions;
6+
using Microsoft.CodeAnalysis.CSharp;
67

78
namespace CSharpToJsonSchema.Generators;
89

@@ -42,7 +43,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth =
4243
.Select(static x => $"\"{x.Name}\""))} }},
4344
{indent} }}";
4445
}
45-
46+
4647
if (parameter.EnumValues.Count != 0)
4748
{
4849
return $@"new {name}
@@ -52,7 +53,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth =
5253
{indent} Enum = new string[] {{ {string.Join(", ", parameter.EnumValues.Select(static x => $"\"{x}\""))} }},
5354
{indent} }}";
5455
}
55-
56+
5657
return $@"new {name}
5758
{indent} {{
5859
{indent} Type = ""{parameter.SchemaType}"",{(parameter.Format != null ? $@"
@@ -80,7 +81,7 @@ public static partial class {extensionsClassName}
8081
new global::CSharpToJsonSchema.Tool
8182
{{
8283
Name = ""{method.Name}"",
83-
Description = ""{method.Description}"",
84+
Description = ""{GetDescriptionStringAsValidCSharp(method.Description)}"",
8485
Strict = {(method.IsStrict ? "true" : "false")},
8586
Parameters = global::CSharpToJsonSchema.SchemaBuilder.ConvertToSchema(global::{@interface.Namespace}.{extensionsClassName}JsonSerializerContext.Default.{method.Name}Args,{"\""}{GetDictionaryString(method)}{"\""}),
8687
}},
@@ -91,19 +92,24 @@ public static partial class {extensionsClassName}
9192
}}";
9293
}
9394

94-
95+
9596

9697
private static string GetDictionaryString(MethodData data)
9798
{
9899
StringBuilder sb = new StringBuilder();
99100

100101
var methodDescriptions = new Dictionary<string, string>();
101-
methodDescriptions.Add("MainFunction_Desc", data.Description);
102-
sb.Append("{");
102+
methodDescriptions.Add("MainFunction_Desc", GetDescriptionStringAsValidCSharp(data.Description));
103+
sb.Append('{');
103104
var lst = methodDescriptions.Select(s => $"\"{s.Key.ToCamelCase()}\":\"{s.Value}\"");
104105
sb.Append(string.Join(", ", lst));
105-
sb.Append("}");
106-
107-
return sb.ToString().Replace("\"","\\\"");
106+
sb.Append('}');
107+
108+
return sb.ToString().Replace("\"", "\\\"");
109+
}
110+
111+
public static string GetDescriptionStringAsValidCSharp(string description)
112+
{
113+
return SymbolDisplay.FormatLiteral(description, quote: false);
108114
}
109115
}

src/tests/CSharpToJsonSchema.IntegrationTests/VariousTypesTools.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ public bool GetCurrentWeather3(
3030

3131
[Description("Gets the value")]
3232
public Task<int> GetValueAsync(CancellationToken cancellationToken = default);
33+
34+
[Description("Gets the value\nWith multiple lines")]
35+
public Task<int> GetValueAsync2(CancellationToken cancellationToken = default);
36+
37+
[Description("""
38+
Gets the value
39+
With multiple lines
40+
""")]
41+
public Task<int> GetValueAsync3(CancellationToken cancellationToken = default);
3342
}
3443

3544
public class VariousTypesService : IVariousTypesTools
@@ -65,4 +74,14 @@ public Task<int> GetValueAsync(CancellationToken cancellationToken = default)
6574
{
6675
throw new NotImplementedException();
6776
}
68-
}
77+
78+
public Task<int> GetValueAsync2(CancellationToken cancellationToken = default)
79+
{
80+
throw new NotImplementedException();
81+
}
82+
83+
public Task<int> GetValueAsync3(CancellationToken cancellationToken = default)
84+
{
85+
throw new NotImplementedException();
86+
}
87+
}

0 commit comments

Comments
 (0)