-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcsharp-codegen.test.ts
More file actions
255 lines (250 loc) · 11.7 KB
/
Copy pathcsharp-codegen.test.ts
File metadata and controls
255 lines (250 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import type { JSONSchema7 } from "json-schema";
import { describe, expect, it } from "vitest";
import { generateRpcCode } from "../../scripts/codegen/csharp.ts";
import type { ApiSchema } from "../../scripts/codegen/utils.ts";
describe("C# RPC codegen", () => {
it.each([
["anyOf", false],
["anyOf", true],
["oneOf", false],
["oneOf", true],
] as const)(
"preserves the %s hierarchy when adding a variant (nullable: %s)",
(keyword, nullable) => {
const jsonSchemaVariant: JSONSchema7 = {
type: "object",
properties: {
type: { type: "string", const: "json_schema" },
jsonSchema: { $ref: "#/definitions/JsonSchemaResponseFormat" },
},
required: ["type", "jsonSchema"],
};
const nullVariants: JSONSchema7[] = nullable ? [{ type: "null" }] : [];
const responseFormat: JSONSchema7 = {
title: "ResponseFormat",
description: "A provider-native output format.",
[keyword]: [jsonSchemaVariant, ...nullVariants],
};
const schema: ApiSchema = {
session: {
send: {
rpcMethod: "session.send",
params: {
type: "object",
title: "SendRequest",
properties: {
responseFormat: { $ref: "#/definitions/ResponseFormat" },
requiredFormat: { $ref: "#/definitions/ResponseFormat" },
},
required: ["requiredFormat"],
},
},
},
definitions: {
ResponseFormat: responseFormat,
JsonSchemaResponseFormat: {
type: "object",
properties: {
name: { type: "string" },
schema: { "x-opaque-json": true } as JSONSchema7,
strict: { type: "boolean" },
},
required: ["name", "schema"],
},
},
};
const code = generateRpcCode(schema);
const futureCode = generateRpcCode({
...schema,
definitions: {
...schema.definitions,
ResponseFormat: {
...responseFormat,
[keyword]: [
jsonSchemaVariant,
{
type: "object",
properties: { type: { type: "string", const: "text" } },
required: ["type"],
},
...nullVariants,
],
},
},
});
for (const generated of [code, futureCode]) {
expect(generated).toContain("public partial class ResponseFormat\n");
expect(generated).toContain("A provider-native output format.");
expect(generated).toContain(
'[JsonPolymorphic(\n TypeDiscriminatorPropertyName = "type",'
);
expect(generated).toContain(
'[JsonDerivedType(typeof(ResponseFormatJsonSchema), "json_schema")]'
);
expect(generated).toContain(
"public partial class ResponseFormatJsonSchema : ResponseFormat"
);
expect(generated).toContain('public override string Type => "json_schema";');
expect(generated).toContain("public ResponseFormat? ResponseFormat");
expect(generated).toContain(
`public ResponseFormat${nullable ? "?" : ""} RequiredFormat`
);
expect(generated).toContain("public required JsonSchemaResponseFormat JsonSchema");
expect(generated).toContain("public JsonElement Schema");
expect(generated).toContain("public bool? Strict");
expect(generated).toContain("[JsonSerializable(typeof(ResponseFormat))]");
expect(generated.match(/public partial class ResponseFormat\b/g)).toHaveLength(1);
}
for (const name of ["ResponseFormat", "ResponseFormatJsonSchema"]) {
const declaration = new RegExp(
`public partial class ${name}\\b[^\\n]*\\n\\{[\\s\\S]*?\\n\\}`
);
expect(code.match(declaration)?.[0]).toBe(futureCode.match(declaration)?.[0]);
}
expect(futureCode).toContain('[JsonDerivedType(typeof(ResponseFormatText), "text")]');
expect(futureCode).toContain(
"public partial class ResponseFormatText : ResponseFormat"
);
}
);
it.each(["anyOf", "oneOf"] as const)(
"preserves referenced enum discriminators and nested %s unions",
(keyword) => {
const code = generateRpcCode({
session: {
configure: {
rpcMethod: "session.configure",
stability: "experimental",
params: {
type: "object",
properties: {
systemMessage: { $ref: "#/definitions/SystemMessage" },
requiredMessage: { $ref: "#/definitions/SystemMessage" },
},
required: ["requiredMessage"],
},
},
},
definitions: {
SystemMessage: {
description: "System message configuration.",
[keyword]: [
{ $ref: "#/definitions/AppendConfig" },
{ $ref: "#/definitions/ReplaceConfig" },
{ $ref: "#/definitions/CustomizeConfig" },
],
},
AppendMode: { type: "string", enum: ["append"] },
ReplaceMode: { type: "string", enum: ["replace"] },
CustomizeMode: { type: "string", enum: ["customize"] },
AppendConfig: {
type: "object",
properties: {
mode: { $ref: "#/definitions/AppendMode" },
content: { type: "string" },
},
},
ReplaceConfig: {
type: "object",
properties: {
mode: { $ref: "#/definitions/ReplaceMode" },
content: { type: "string" },
},
required: ["mode", "content"],
},
CustomizeConfig: {
type: "object",
properties: {
mode: { $ref: "#/definitions/CustomizeMode" },
content: { type: "string" },
sections: {
type: "object",
additionalProperties: {
$ref: "#/definitions/SectionOverride",
},
},
},
required: ["mode"],
},
SectionOverride: {
[keyword]: [
{ $ref: "#/definitions/StaticSectionOverride" },
{ $ref: "#/definitions/MarkerSectionOverride" },
],
},
StaticSectionAction: {
type: "string",
enum: ["replace", "remove", "append", "prepend"],
},
StaticSectionOverride: {
type: "object",
properties: {
action: { $ref: "#/definitions/StaticSectionAction" },
content: { type: "string" },
},
required: ["action"],
},
MarkerSectionOverride: {
[keyword]: ["transform", "preserve"].map((action) => ({
type: "object",
properties: { action: { type: "string", const: action } },
required: ["action"],
})),
},
},
});
expect(code).toContain("public sealed partial class SystemMessage");
expect(code).toContain(
"[Experimental(Diagnostics.Experimental)]\n[JsonConverter(typeof(Converter))]\npublic sealed partial class SystemMessage"
);
expect(code).toContain("System message configuration.");
expect(code).toContain("public SystemMessage? SystemMessage");
expect(code).toContain("public SystemMessage RequiredMessage");
expect(code.match(/public sealed partial class SystemMessage\b/g)).toHaveLength(1);
expect(code).toContain("public AppendConfig? AppendConfig { get; }");
expect(code).toContain("public ReplaceConfig? ReplaceConfig { get; }");
expect(code).toContain("public CustomizeConfig? CustomizeConfig { get; }");
expect(code).toContain("public AppendMode? Mode");
expect(code).toContain("public ReplaceMode Mode");
expect(code).toContain("public IDictionary<string, SectionOverride>? Sections");
expect(code).toContain("public StaticSectionOverride? StaticSectionOverride { get; }");
expect(code).toContain("public MarkerSectionOverride? MarkerSectionOverride { get; }");
expect(code).toContain("public StaticSectionAction Action");
expect(code).toContain(
'!element.TryGetProperty("mode", out _) || (element.TryGetProperty("mode", out _)'
);
for (const mode of ["append", "replace", "customize"]) {
expect(code).toContain(`element.GetProperty("mode").GetString() == "${mode}"`);
}
for (const action of [
"replace",
"remove",
"append",
"prepend",
"transform",
"preserve",
]) {
expect(code).toContain(`element.GetProperty("action").GetString() == "${action}"`);
}
expect(code).toContain('element.GetProperty("mode").ValueKind == JsonValueKind.String');
expect(code).not.toContain("catch (JsonException)");
for (const type of [
"SystemMessage",
"AppendConfig",
"ReplaceConfig",
"CustomizeConfig",
"SectionOverride",
"StaticSectionOverride",
"MarkerSectionOverride",
]) {
expect(code).toContain(`[JsonSerializable(typeof(${type}))]`);
}
expect(code).toContain(
"JsonSerializer.Deserialize(element, RpcJsonContext.Default.AppendConfig)"
);
expect(code).toContain(
"JsonSerializer.Serialize(writer, appendConfig, RpcJsonContext.Default.AppendConfig)"
);
}
);
});