Skip to content

Commit 46a569b

Browse files
author
Gunpal Jain
committed
fix: schema generation
1 parent 1ebee3f commit 46a569b

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/libs/CSharpToJsonSchema/OpenApiSchemaJsonContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace CSharpToJsonSchema;
55
[JsonSerializable(typeof(OpenApiSchema))]
66
[JsonSerializable(typeof(IDictionary<string, OpenApiSchema>))]
77
[JsonSerializable(typeof(IDictionary<string, string>))]
8+
[JsonSerializable(typeof(Tool))]
9+
[JsonSerializable(typeof(List<Tool>))]
10+
[JsonSourceGenerationOptions(NumberHandling = JsonNumberHandling.Strict, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
811
public partial class OpenApiSchemaJsonContext:JsonSerializerContext
912
{
1013

src/libs/CSharpToJsonSchema/SchemaSubsetHelper.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@ public static OpenApiSchema ConvertToSchema(JsonTypeInfo type, string descriptio
117117
{
118118
TransformSchemaNode = (a, b) =>
119119
{
120+
if (a.TypeInfo.Type.IsEnum)
121+
{
122+
b["type"] = "string";
123+
}
120124
if (a.PropertyInfo == null)
121125
return b;
122126
var propName = ToCamelCase(a.PropertyInfo.Name);
123127
if (dics.ContainsKey(propName))
124128
{
125129
b["description"] = dics[propName];
126130
}
131+
127132
return b;
128133
},
129134
}));
@@ -134,12 +139,14 @@ public static OpenApiSchema ConvertToSchema(JsonTypeInfo type, string descriptio
134139
required.Add(re.Key);
135140
}
136141

142+
137143
var mainDescription =x.Description ?? dics["mainFunction_Desc"];
138144
return new OpenApiSchema()
139145
{
140146
Description = mainDescription,
141147
Properties = x.Properties,
142-
Required = required
148+
Required = required,
149+
Type = "object"
143150
};
144151
}
145152

@@ -157,7 +164,9 @@ public static string ConvertToSchema(Type type, JsonSerializerOptions? jsonOptio
157164
{
158165
var node = jsonOptions.GetJsonSchemaAsNode(type);
159166
var x = ConvertToCompatibleSchemaSubset(node);
167+
168+
var x2= JsonSerializer.Serialize(x.Properties, OpenApiSchemaJsonContext.Default.IDictionaryStringOpenApiSchema);
160169

161-
return JsonSerializer.Serialize(x.Properties, OpenApiSchemaJsonContext.Default.IDictionaryStringOpenApiSchema);
170+
return x2;
162171
}
163172
}

src/tests/CSharpToJsonSchema.AotTests/JsonSerializationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void ShouldCreateToolWithComplexStudentClass()
127127
{
128128
var service = new StudenRecordService();
129129
var tools = service.AsTools();
130+
var serialize = JsonSerializer.Serialize(tools, OpenApiSchemaJsonContext.Default.ListTool);
130131
}
131132

132133
}

0 commit comments

Comments
 (0)