Skip to content

Commit 76958ca

Browse files
committed
fix: Fixed issue with Task result type.
1 parent b07ba9e commit 76958ca

File tree

6 files changed

+240
-10
lines changed

6 files changed

+240
-10
lines changed

src/libs/CSharpToJsonSchema.Generators/Conversion/ToModels.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static InterfaceData PrepareData(
2929
Name: x.Name,
3030
Description: GetDescription(x),
3131
IsAsync: x.IsAsync || x.ReturnType.Name == "Task",
32-
IsVoid: x.ReturnsVoid,
32+
IsVoid: x.ReturnsVoid || x.ReturnType.MetadataName == "Task",
3333
IsStrict: isStrict,
3434
Parameters: new OpenApiSchema(
3535
Name: x.Name,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using H.Generators;
1+
using H.Generators;
22
using H.Generators.Extensions;
33
using CSharpToJsonSchema.Generators.Core;
44

@@ -26,7 +26,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth =
2626
{indent} Items = {GenerateOpenApiSchema(parameter.ArrayItem.First(), depth: depth + 1)},
2727
{indent} }}";
2828
}
29-
if (parameter.SchemaType == "object")
29+
if (parameter.Properties.Count != 0)
3030
{
3131
return $@"new {name}
3232
{indent} {{

src/tests/CSharpToJsonSchema.IntegrationTests/VariousTypesTools.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// ReSharper disable RedundantUsingDirective
22
using System;
3+
using System.Threading;
4+
using System.Threading.Tasks;
35
using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
46

57
namespace CSharpToJsonSchema.IntegrationTests;
@@ -16,6 +18,18 @@ public bool GetCurrentWeather(
1618
bool parameter5,
1719
DateTime dateTime,
1820
DateOnly date);
21+
22+
[Description("Sets the value")]
23+
public void SetValue(int value);
24+
25+
[Description("Gets the value")]
26+
public int GetValue();
27+
28+
[Description("Sets the value")]
29+
public Task SetValueAsync(int value, CancellationToken cancellationToken = default);
30+
31+
[Description("Gets the value")]
32+
public Task<int> GetValueAsync(CancellationToken cancellationToken = default);
1933
}
2034

2135
public class VariousTypesService : IVariousTypesTools
@@ -31,4 +45,24 @@ public bool GetCurrentWeather(
3145
{
3246
return true;
3347
}
48+
49+
public void SetValue(int value)
50+
{
51+
throw new NotImplementedException();
52+
}
53+
54+
public int GetValue()
55+
{
56+
throw new NotImplementedException();
57+
}
58+
59+
public Task SetValueAsync(int value, CancellationToken cancellationToken = default)
60+
{
61+
throw new NotImplementedException();
62+
}
63+
64+
public Task<int> GetValueAsync(CancellationToken cancellationToken = default)
65+
{
66+
throw new NotImplementedException();
67+
}
3468
}

src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs

Lines changed: 134 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ public class GetCurrentWeatherArgs
1616
public global::System.DateOnly Date { get; set; }
1717
}
1818

19+
public class SetValueArgs
20+
{
21+
public int Value { get; set; }
22+
}
23+
24+
public class GetValueArgs
25+
{
26+
27+
}
28+
29+
public class SetValueAsyncArgs
30+
{
31+
public int Value { get; set; }
32+
}
33+
34+
public class GetValueAsyncArgs
35+
{
36+
37+
}
38+
1939
public static global::System.Collections.Generic.IReadOnlyDictionary<string, global::System.Func<string, global::System.Threading.CancellationToken, global::System.Threading.Tasks.Task<string>>> AsCalls(this IVariousTypesTools service)
2040
{
2141
return new global::System.Collections.Generic.Dictionary<string, global::System.Func<string, global::System.Threading.CancellationToken, global::System.Threading.Tasks.Task<string>>>
@@ -24,9 +44,27 @@ public class GetCurrentWeatherArgs
2444
{
2545
return global::System.Threading.Tasks.Task.FromResult(service.CallGetCurrentWeather(json));
2646
},
27-
28-
29-
47+
48+
["GetValue"] = (json, _) =>
49+
{
50+
return global::System.Threading.Tasks.Task.FromResult(service.CallGetValue(json));
51+
},
52+
["SetValue"] = (json, _) =>
53+
{
54+
service.CallSetValue(json);
55+
56+
return global::System.Threading.Tasks.Task.FromResult(string.Empty);
57+
},
58+
["GetValueAsync"] = async (json, cancellationToken) =>
59+
{
60+
return await service.CallGetValueAsync(json, cancellationToken);
61+
},
62+
["SetValueAsync"] = async (json, cancellationToken) =>
63+
{
64+
await service.CallSetValueAsync(json, cancellationToken);
65+
66+
return string.Empty;
67+
},
3068
};
3169
}
3270

@@ -43,6 +81,58 @@ public static VariousTypesToolsExtensions.GetCurrentWeatherArgs AsGetCurrentWeat
4381
throw new global::System.InvalidOperationException("Could not deserialize JSON.");
4482
}
4583

84+
public static VariousTypesToolsExtensions.SetValueArgs AsSetValueArgs(
85+
this IVariousTypesTools functions,
86+
string json)
87+
{
88+
return
89+
global::System.Text.Json.JsonSerializer.Deserialize<VariousTypesToolsExtensions.SetValueArgs>(json, new global::System.Text.Json.JsonSerializerOptions
90+
{
91+
PropertyNamingPolicy = global::System.Text.Json.JsonNamingPolicy.CamelCase,
92+
Converters = {{ new global::System.Text.Json.Serialization.JsonStringEnumConverter(global::System.Text.Json.JsonNamingPolicy.CamelCase) }}
93+
}) ??
94+
throw new global::System.InvalidOperationException("Could not deserialize JSON.");
95+
}
96+
97+
public static VariousTypesToolsExtensions.GetValueArgs AsGetValueArgs(
98+
this IVariousTypesTools functions,
99+
string json)
100+
{
101+
return
102+
global::System.Text.Json.JsonSerializer.Deserialize<VariousTypesToolsExtensions.GetValueArgs>(json, new global::System.Text.Json.JsonSerializerOptions
103+
{
104+
PropertyNamingPolicy = global::System.Text.Json.JsonNamingPolicy.CamelCase,
105+
Converters = {{ new global::System.Text.Json.Serialization.JsonStringEnumConverter(global::System.Text.Json.JsonNamingPolicy.CamelCase) }}
106+
}) ??
107+
throw new global::System.InvalidOperationException("Could not deserialize JSON.");
108+
}
109+
110+
public static VariousTypesToolsExtensions.SetValueAsyncArgs AsSetValueAsyncArgs(
111+
this IVariousTypesTools functions,
112+
string json)
113+
{
114+
return
115+
global::System.Text.Json.JsonSerializer.Deserialize<VariousTypesToolsExtensions.SetValueAsyncArgs>(json, new global::System.Text.Json.JsonSerializerOptions
116+
{
117+
PropertyNamingPolicy = global::System.Text.Json.JsonNamingPolicy.CamelCase,
118+
Converters = {{ new global::System.Text.Json.Serialization.JsonStringEnumConverter(global::System.Text.Json.JsonNamingPolicy.CamelCase) }}
119+
}) ??
120+
throw new global::System.InvalidOperationException("Could not deserialize JSON.");
121+
}
122+
123+
public static VariousTypesToolsExtensions.GetValueAsyncArgs AsGetValueAsyncArgs(
124+
this IVariousTypesTools functions,
125+
string json)
126+
{
127+
return
128+
global::System.Text.Json.JsonSerializer.Deserialize<VariousTypesToolsExtensions.GetValueAsyncArgs>(json, new global::System.Text.Json.JsonSerializerOptions
129+
{
130+
PropertyNamingPolicy = global::System.Text.Json.JsonNamingPolicy.CamelCase,
131+
Converters = {{ new global::System.Text.Json.Serialization.JsonStringEnumConverter(global::System.Text.Json.JsonNamingPolicy.CamelCase) }}
132+
}) ??
133+
throw new global::System.InvalidOperationException("Could not deserialize JSON.");
134+
}
135+
46136
public static string CallGetCurrentWeather(this IVariousTypesTools functions, string json)
47137
{
48138
var args = functions.AsGetCurrentWeatherArgs(json);
@@ -55,11 +145,49 @@ public static string CallGetCurrentWeather(this IVariousTypesTools functions, st
55145
});
56146
}
57147

58-
148+
public static string CallGetValue(this IVariousTypesTools functions, string json)
149+
{
150+
var args = functions.AsGetValueArgs(json);
151+
var jsonResult = functions.GetValue();
152+
153+
return global::System.Text.Json.JsonSerializer.Serialize(jsonResult, new global::System.Text.Json.JsonSerializerOptions
154+
{
155+
PropertyNamingPolicy = global::System.Text.Json.JsonNamingPolicy.CamelCase,
156+
Converters = { new global::System.Text.Json.Serialization.JsonStringEnumConverter(global::System.Text.Json.JsonNamingPolicy.CamelCase) },
157+
});
158+
}
159+
160+
public static void CallSetValue(this IVariousTypesTools functions, string json)
161+
{
162+
var args = functions.AsSetValueArgs(json);
163+
functions.SetValue(args.Value);
164+
}
59165

60-
166+
public static async global::System.Threading.Tasks.Task<string> CallGetValueAsync(
167+
this IVariousTypesTools functions,
168+
string json,
169+
global::System.Threading.CancellationToken cancellationToken = default)
170+
{
171+
var args = functions.AsGetValueAsyncArgs(json);
172+
var jsonResult = await functions.GetValueAsync(cancellationToken);
61173

62-
174+
return global::System.Text.Json.JsonSerializer.Serialize(jsonResult, new global::System.Text.Json.JsonSerializerOptions
175+
{
176+
PropertyNamingPolicy = global::System.Text.Json.JsonNamingPolicy.CamelCase,
177+
Converters = { new global::System.Text.Json.Serialization.JsonStringEnumConverter(global::System.Text.Json.JsonNamingPolicy.CamelCase) },
178+
});
179+
}
180+
181+
public static async global::System.Threading.Tasks.Task<string> CallSetValueAsync(
182+
this IVariousTypesTools functions,
183+
string json,
184+
global::System.Threading.CancellationToken cancellationToken = default)
185+
{
186+
var args = functions.AsSetValueAsyncArgs(json);
187+
await functions.SetValueAsync(args.Value, cancellationToken);
188+
189+
return string.Empty;
190+
}
63191

64192
public static async global::System.Threading.Tasks.Task<string> CallAsync(
65193
this IVariousTypesTools service,

src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,74 @@ public static partial class VariousTypesToolsExtensions
6666
Required = new string[] { "parameter1", "parameter2", "parameter3", "parameter4", "parameter5", "dateTime", "date" },
6767
},
6868
},
69+
70+
new global::CSharpToJsonSchema.Tool
71+
{
72+
Name = "SetValue",
73+
Description = "Sets the value",
74+
Strict = false,
75+
Parameters = new global::CSharpToJsonSchema.OpenApiSchema
76+
{
77+
Type = "object",
78+
Description = "Sets the value",
79+
Properties = new global::System.Collections.Generic.Dictionary<string, global::CSharpToJsonSchema.OpenApiSchema>
80+
{
81+
["value"] = new global::CSharpToJsonSchema.OpenApiSchema
82+
{
83+
Type = "integer",
84+
Format = "int32",
85+
Description = "",
86+
}
87+
},
88+
Required = new string[] { "value" },
89+
},
90+
},
91+
92+
new global::CSharpToJsonSchema.Tool
93+
{
94+
Name = "GetValue",
95+
Description = "Gets the value",
96+
Strict = false,
97+
Parameters = new global::CSharpToJsonSchema.OpenApiSchema
98+
{
99+
Type = "object",
100+
Description = "Gets the value",
101+
},
102+
},
103+
104+
new global::CSharpToJsonSchema.Tool
105+
{
106+
Name = "SetValueAsync",
107+
Description = "Sets the value",
108+
Strict = false,
109+
Parameters = new global::CSharpToJsonSchema.OpenApiSchema
110+
{
111+
Type = "object",
112+
Description = "Sets the value",
113+
Properties = new global::System.Collections.Generic.Dictionary<string, global::CSharpToJsonSchema.OpenApiSchema>
114+
{
115+
["value"] = new global::CSharpToJsonSchema.OpenApiSchema
116+
{
117+
Type = "integer",
118+
Format = "int32",
119+
Description = "",
120+
}
121+
},
122+
Required = new string[] { "value" },
123+
},
124+
},
125+
126+
new global::CSharpToJsonSchema.Tool
127+
{
128+
Name = "GetValueAsync",
129+
Description = "Gets the value",
130+
Strict = false,
131+
Parameters = new global::CSharpToJsonSchema.OpenApiSchema
132+
{
133+
Type = "object",
134+
Description = "Gets the value",
135+
},
136+
},
69137
};
70138
}
71139
}

src/tests/CSharpToJsonSchema.SnapshotTests/TestHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ await Task.WhenAll(
3838
.UseTextForParameters("Diagnostics"),
3939
verifier
4040
.Verify(driver)
41-
//.AutoVerify()
41+
.AutoVerify()
4242
.UseDirectory("Snapshots"));
4343
}
4444
}

0 commit comments

Comments
 (0)