Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 3 additions & 0 deletions src/libs/CSharpToJsonSchema/OpenApiSchemaJsonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace CSharpToJsonSchema;
[JsonSerializable(typeof(OpenApiSchema))]
[JsonSerializable(typeof(IDictionary<string, OpenApiSchema>))]
[JsonSerializable(typeof(IDictionary<string, string>))]
[JsonSerializable(typeof(Tool))]
[JsonSerializable(typeof(List<Tool>))]
Comment on lines +8 to +9
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Missing import for Tool class

The code adds JSON serialization for Tool and List<Tool> types, but there's no import statement for the Tool class. This could cause compilation errors.

1
using System.Text.Json.Serialization;
2
+using System.Collections.Generic;
+// Add the appropriate namespace for the Tool class
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[JsonSerializable(typeof(Tool))]
[JsonSerializable(typeof(List<Tool>))]
using System.Text.Json.Serialization;
using System.Collections.Generic;
// Add the appropriate namespace for the Tool class
[JsonSerializable(typeof(Tool))]
[JsonSerializable(typeof(List<Tool>))]

[JsonSourceGenerationOptions(NumberHandling = JsonNumberHandling.Strict, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
public partial class OpenApiSchemaJsonContext:JsonSerializerContext
{

Expand Down
46 changes: 28 additions & 18 deletions src/libs/CSharpToJsonSchema/SchemaSubsetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,49 @@ public static OpenApiSchema ConvertToSchema<T>(JsonSerializerOptions? jsonOption
public static OpenApiSchema ConvertToSchema(JsonTypeInfo type, string descriptionString)
{
var typeInfo = type;

var dics = JsonSerializer.Deserialize(descriptionString,OpenApiSchemaJsonContext.Default.IDictionaryStringString);

var dics = JsonSerializer.Deserialize(descriptionString,
OpenApiSchemaJsonContext.Default.IDictionaryStringString);
List<string> required = new List<string>();
var x = ConvertToCompatibleSchemaSubset(typeInfo.GetJsonSchemaAsNode(exporterOptions:new JsonSchemaExporterOptions()
{
TransformSchemaNode = (a, b) =>
var x = ConvertToCompatibleSchemaSubset(typeInfo.GetJsonSchemaAsNode(
exporterOptions: new JsonSchemaExporterOptions()
{
if (a.PropertyInfo == null)
return b;
var propName = ToCamelCase(a.PropertyInfo.Name);
if (dics.ContainsKey(propName))
TransformSchemaNode = (a, b) =>
{
b["description"] = dics[propName];
}
return b;
},
}));
if (a.TypeInfo.Type.IsEnum)
{
b["type"] = "string";
}

if (a.PropertyInfo == null)
return b;
var propName = ToCamelCase(a.PropertyInfo.Name);
if (dics.ContainsKey(propName))
{
b["description"] = dics[propName];
}

return b;
},
}));



foreach (var re in x.Properties)
{
required.Add(re.Key);
}

var mainDescription =x.Description ?? dics["mainFunction_Desc"];

var mainDescription = x.Description ?? dics["mainFunction_Desc"];
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Missing error handling for dictionary key lookup

The code assumes that the 'dics' dictionary contains a "mainFunction_Desc" key, but doesn't handle the case where this key might be missing. This could lead to a KeyNotFoundException at runtime.

-var mainDescription = x.Description ?? dics["mainFunction_Desc"];
+var mainDescription = x.Description ?? (dics.TryGetValue("mainFunction_Desc", out var desc) ? desc : "");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var mainDescription = x.Description ?? dics["mainFunction_Desc"];
var mainDescription = x.Description ?? (dics.TryGetValue("mainFunction_Desc", out var desc) ? desc : "");

return new OpenApiSchema()
{
Description = mainDescription,
Properties = x.Properties,
Required = required
Required = required,
Type = "object"
};
}

public static string ToCamelCase(string str)
{
if (!string.IsNullOrEmpty(str) && str.Length > 1)
Expand Down
Loading