Skip to content

Escape strings when creating descriptions #39

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

Merged
merged 2 commits into from
May 8, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static void AddAllTools()
tool = new global::CSharpToJsonSchema.Tool
{{
Name = ""{method.Name}"",
Description = ""{method.Description}"",
Description = ""{GetDescriptionStringAsValidCSharp(method.Description)}"",
Strict = {(method.IsStrict ? "true" : "false")},
Parameters = global::CSharpToJsonSchema.SchemaBuilder.ConvertToSchema(global::{@interface.Namespace}.{extensionsClassName}JsonSerializerContext.Default.{method.Name}Args,{"\""}{GetDictionaryString(method)}{"\""}),
}};
Expand Down Expand Up @@ -108,4 +108,4 @@ private static string GetDelegateInputsTypes(MethodData first, bool addReturnTyp
f.Add(first.ReturnType.ToDisplayString());
return string.Join(", ", f);
}
}
}
24 changes: 15 additions & 9 deletions src/libs/CSharpToJsonSchema.Generators/Sources.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CSharpToJsonSchema.Generators.Models;
using H.Generators;
using H.Generators.Extensions;
using Microsoft.CodeAnalysis.CSharp;

namespace CSharpToJsonSchema.Generators;

Expand Down Expand Up @@ -42,7 +43,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth =
.Select(static x => $"\"{x.Name}\""))} }},
{indent} }}";
}

if (parameter.EnumValues.Count != 0)
{
return $@"new {name}
Expand All @@ -52,7 +53,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth =
{indent} Enum = new string[] {{ {string.Join(", ", parameter.EnumValues.Select(static x => $"\"{x}\""))} }},
{indent} }}";
}

return $@"new {name}
{indent} {{
{indent} Type = ""{parameter.SchemaType}"",{(parameter.Format != null ? $@"
Expand Down Expand Up @@ -80,7 +81,7 @@ public static partial class {extensionsClassName}
new global::CSharpToJsonSchema.Tool
{{
Name = ""{method.Name}"",
Description = ""{method.Description}"",
Description = ""{GetDescriptionStringAsValidCSharp(method.Description)}"",
Strict = {(method.IsStrict ? "true" : "false")},
Parameters = global::CSharpToJsonSchema.SchemaBuilder.ConvertToSchema(global::{@interface.Namespace}.{extensionsClassName}JsonSerializerContext.Default.{method.Name}Args,{"\""}{GetDictionaryString(method)}{"\""}),
}},
Expand All @@ -91,19 +92,24 @@ public static partial class {extensionsClassName}
}}";
}



private static string GetDictionaryString(MethodData data)
{
StringBuilder sb = new StringBuilder();

var methodDescriptions = new Dictionary<string, string>();
methodDescriptions.Add("MainFunction_Desc", data.Description);
sb.Append("{");
methodDescriptions.Add("MainFunction_Desc", GetDescriptionStringAsValidCSharp(data.Description));
sb.Append('{');
var lst = methodDescriptions.Select(s => $"\"{s.Key.ToCamelCase()}\":\"{s.Value}\"");
sb.Append(string.Join(", ", lst));
sb.Append("}");

return sb.ToString().Replace("\"","\\\"");
sb.Append('}');

return sb.ToString().Replace("\"", "\\\"");
}

public static string GetDescriptionStringAsValidCSharp(string description)
{
return SymbolDisplay.FormatLiteral(description, quote: false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public bool GetCurrentWeather3(

[Description("Gets the value")]
public Task<int> GetValueAsync(CancellationToken cancellationToken = default);

[Description("Gets the value\nWith multiple lines")]
public Task<int> GetValueAsync2(CancellationToken cancellationToken = default);

[Description("""
Gets the value
With multiple lines
""")]
public Task<int> GetValueAsync3(CancellationToken cancellationToken = default);
}

public class VariousTypesService : IVariousTypesTools
Expand Down Expand Up @@ -65,4 +74,14 @@ public Task<int> GetValueAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}

public Task<int> GetValueAsync2(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public Task<int> GetValueAsync3(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
Loading