Skip to content

Commit bb8be35

Browse files
committed
fix: avoid typed tool returns in samples
1 parent 9b9659c commit bb8be35

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

samples/api-test-generator.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,33 +72,43 @@
7272
{
7373
// Try to parse as JSON
7474
var doc = JsonDocument.Parse(spec);
75-
var endpoints = new List<object>();
75+
var endpoints = new List<(string Path, string Method, string Summary, string OperationId)>();
7676

7777
if (doc.RootElement.TryGetProperty("paths", out var paths))
7878
{
7979
foreach (var path in paths.EnumerateObject())
8080
{
8181
foreach (var method in path.Value.EnumerateObject())
8282
{
83-
var endpoint = new
84-
{
85-
path = path.Name,
86-
method = method.Name.ToUpper(),
87-
summary = method.Value.TryGetProperty("summary", out var s) ? s.GetString() : "",
88-
operationId = method.Value.TryGetProperty("operationId", out var o) ? o.GetString() : ""
89-
};
90-
endpoints.Add(endpoint);
83+
endpoints.Add(
84+
(
85+
path.Name,
86+
method.Name.ToUpperInvariant(),
87+
method.Value.TryGetProperty("summary", out var s) ? (s.GetString() ?? "") : "",
88+
method.Value.TryGetProperty("operationId", out var o) ? (o.GetString() ?? "") : ""
89+
)
90+
);
9191
if (endpoints.Count > 50) break; // Limit
9292
}
9393
if (endpoints.Count > 50) break;
9494
}
9595
}
96-
97-
return new ParseEndpointsResult(true, $"Found {endpoints.Count} endpoints", endpoints, endpoints.Count);
96+
97+
var sb = new System.Text.StringBuilder();
98+
sb.AppendLine($"success: true");
99+
sb.AppendLine($"count: {endpoints.Count}");
100+
sb.AppendLine("endpoints:");
101+
foreach (var ep in endpoints)
102+
{
103+
var summary = string.IsNullOrWhiteSpace(ep.Summary) ? "" : $" — {ep.Summary}";
104+
var opId = string.IsNullOrWhiteSpace(ep.OperationId) ? "" : $" (operationId: {ep.OperationId})";
105+
sb.AppendLine($"- {ep.Method} {ep.Path}{summary}{opId}");
106+
}
107+
return sb.ToString();
98108
}
99109
catch (Exception ex)
100110
{
101-
return new ParseEndpointsResult(false, ex.Message, new List<object>(), 0);
111+
return $"success: false\nmessage: {ex.Message}\n";
102112
}
103113
},
104114
"parse_endpoints",
@@ -121,8 +131,11 @@
121131
if (spec.Contains("basic", StringComparison.OrdinalIgnoreCase))
122132
authTypes.Add("Basic Auth");
123133
}
124-
125-
return new AuthAnalysisResult(authTypes);
134+
135+
if (authTypes.Count == 0)
136+
return "authenticationTypes: (none detected)\n";
137+
138+
return "authenticationTypes:\n" + string.Join("\n", authTypes.Select(a => $"- {a}")) + "\n";
126139
},
127140
"analyze_auth",
128141
"Analyze authentication requirements"
@@ -147,8 +160,13 @@
147160
testCases.Add($"Test {method} {path} - Missing Required Fields");
148161
testCases.Add($"Test {method} {path} - Invalid Data Types");
149162
}
150-
151-
return new TestCaseResult($"{method} {path}", testCases);
163+
164+
var sb = new System.Text.StringBuilder();
165+
sb.AppendLine($"endpoint: {method} {path}");
166+
sb.AppendLine("testCases:");
167+
foreach (var tc in testCases)
168+
sb.AppendLine($"- {tc}");
169+
return sb.ToString();
152170
},
153171
"generate_test_cases",
154172
"Generate test case scenarios for an endpoint"
@@ -252,7 +270,3 @@ 2. Analyze authentication requirements
252270
Console.WriteLine($"💡 Tip: Review and customize the generated tests before use");
253271

254272
return 0;
255-
256-
record ParseEndpointsResult(bool success, string message, List<object> endpoints, int count);
257-
record AuthAnalysisResult(List<string> authenticationTypes);
258-
record TestCaseResult(string endpoint, List<string> testCases);

samples/test-data-generator.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
{
5252
// This tool helps AI understand what kind of data to generate
5353
// The actual generation is done by the AI model
54-
return new FieldDescriptionResult(fieldName, dataType, index);
54+
return $"field: {fieldName}\ntype: {dataType}\nindex: {index}\n";
5555
},
5656
"generate_record",
5757
"Generate a single data record field"
@@ -64,11 +64,11 @@
6464
{
6565
var doc = JsonDocument.Parse(jsonData);
6666
var recordCount = doc.RootElement.GetArrayLength();
67-
return new ValidateDataResult(true, recordCount, "");
67+
return $"valid: true\nrecordCount: {recordCount}\n";
6868
}
6969
catch (Exception ex)
7070
{
71-
return new ValidateDataResult(false, 0, ex.Message);
71+
return $"valid: false\nrecordCount: 0\nerror: {ex.Message}\n";
7272
}
7373
},
7474
"validate_data",
@@ -244,6 +244,3 @@ 4. Ensure data consistency (e.g., emails match names)
244244
Console.WriteLine($"\n💡 Tip: Use different schemas: user, product, order, customer, employee");
245245

246246
return 0;
247-
248-
record FieldDescriptionResult(string field, string type, int index);
249-
record ValidateDataResult(bool valid, int recordCount, string error);

0 commit comments

Comments
 (0)