Skip to content

Commit 07b8eae

Browse files
committed
fix: Fixed some issues with Source Generator.
1 parent 0b5cbf3 commit 07b8eae

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

src/libs/OpenAI.Generators.Core/Conversion/ToModels.cs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,44 @@ public static InterfaceData PrepareData(
1212
{
1313
interfaceSymbol = interfaceSymbol ?? throw new ArgumentNullException(nameof(interfaceSymbol));
1414
attributeData = attributeData ?? throw new ArgumentNullException(nameof(attributeData));
15-
15+
16+
var isStrict = attributeData.NamedArguments.FirstOrDefault(x => x.Key == "Strict").Value.Value is bool strict &&
17+
strict;
1618
var methods = interfaceSymbol
1719
.GetMembers()
1820
.OfType<IMethodSymbol>()
1921
.Where(static x => x.MethodKind == MethodKind.Ordinary)
20-
.Select(x => new MethodData(
21-
Name: x.Name,
22-
Description: GetDescription(x),
23-
IsAsync: x.IsAsync || x.ReturnType.Name == "Task",
24-
IsVoid: x.ReturnsVoid,
25-
IsStrict: attributeData.NamedArguments.FirstOrDefault(x => x.Key == "Strict").Value.Value is bool strict && strict,
26-
Parameters: new OpenApiSchema(
22+
.Select(x =>
23+
{
24+
var parameters = x.Parameters
25+
.Where(static x => x.Type.MetadataName != "CancellationToken")
26+
.ToArray();
27+
28+
return new MethodData(
2729
Name: x.Name,
2830
Description: GetDescription(x),
29-
Type: "object",
30-
SchemaType: "object",
31-
Properties:
32-
x.Parameters
33-
.Where(static x => x.Type.MetadataName != "CancellationToken")
34-
.Select(static x => ToParameterData(
35-
typeSymbol: x.Type,
36-
name: x.Name,
37-
description: GetDescription(x),
38-
isRequired: !x.IsOptional))
39-
.ToArray(),
40-
EnumValues: Array.Empty<string>(),
41-
IsNullable: false,
42-
IsRequired: true,
43-
Format: null,
44-
ArrayItem: Array.Empty<OpenApiSchema>(),
45-
DefaultValue: string.Empty)))
31+
IsAsync: x.IsAsync || x.ReturnType.Name == "Task",
32+
IsVoid: x.ReturnsVoid,
33+
IsStrict: isStrict,
34+
Parameters: new OpenApiSchema(
35+
Name: x.Name,
36+
Description: GetDescription(x),
37+
Type: "object",
38+
SchemaType: "object",
39+
Properties: parameters
40+
.Select(y => ToParameterData(
41+
typeSymbol: y.Type,
42+
name: y.Name,
43+
description: GetDescription(y),
44+
isRequired: isStrict || !y.IsOptional))
45+
.ToArray(),
46+
EnumValues: Array.Empty<string>(),
47+
IsNullable: false,
48+
IsRequired: true,
49+
Format: null,
50+
ArrayItem: Array.Empty<OpenApiSchema>(),
51+
DefaultValue: string.Empty));
52+
})
4653
.ToArray();
4754

4855
return new InterfaceData(

src/libs/OpenAI.Generators/Sources.Calls.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public class {method.Name}Args
104104
global::System.Threading.CancellationToken cancellationToken = default)
105105
{{
106106
var args = functions.As{method.Name}Args(json);
107-
var jsonResult = await functions.{method.Name}({string.Join(", ", method.Parameters.Properties.Select(static parameter => $@"args.{parameter.Name.ToPropertyName()}"))}, cancellationToken);
107+
var jsonResult = await functions.{method.Name}({string.Join(", ", method.Parameters.Properties
108+
.Select(static parameter => $@"args.{parameter.Name.ToPropertyName()}").Append("cancellationToken"))});
108109
109110
return global::System.Text.Json.JsonSerializer.Serialize(jsonResult, new global::System.Text.Json.JsonSerializerOptions
110111
{{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.Properties.Count != 0)
29+
if (parameter.SchemaType == "object")
3030
{
3131
return $@"new {name}
3232
{indent} {{
@@ -63,7 +63,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth =
6363
public static string GenerateClientImplementation(InterfaceData @interface)
6464
{
6565
var extensionsClassName = @interface.Name.Substring(startIndex: 1) + "Extensions";
66-
66+
6767
return @$"
6868
#nullable enable
6969

0 commit comments

Comments
 (0)