Skip to content

Commit 4940d3c

Browse files
committed
[http-server-csharp] add arrayDeclarationContext
1 parent 755fe9a commit 4940d3c

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

packages/http-server-csharp/src/lib/service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
213213
#getDefaultNamespace(): string {
214214
return "TypeSpec.Service";
215215
}
216+
217+
arrayDeclarationContext(array: Model, name: string, elementType: Type) {
218+
const arrayName = ensureCSharpIdentifier(this.emitter.getProgram(), array, name);
219+
const arrayFile = this.emitter.createSourceFile(`generated/models/${arrayName}.cs`);
220+
arrayFile.meta[this.#sourceTypeKey] = CSharpSourceType.Model;
221+
const arrayNamespace = this.#getOrAddNamespace(array.namespace);
222+
return this.#createModelContext(arrayNamespace, arrayFile, arrayName);
223+
}
216224

217225
arrayDeclaration(array: Model, name: string, elementType: Type): EmitterOutput<string> {
218226
return this.collectionDeclaration(elementType, array);

packages/http-server-csharp/test/generation.test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,136 @@ describe("collection type: defined as emitter option", () => {
33963396
});
33973397
});
33983398

3399+
describe("arrayDeclarationContext", () => {
3400+
it("generates a dedicated file for array model declarations", async () => {
3401+
await compileAndValidateMultiple(
3402+
tester,
3403+
`
3404+
model Tags is Array<string>;
3405+
@route("/tags") @get op getTags(): Tags;
3406+
`,
3407+
[
3408+
[
3409+
"Tags.cs",
3410+
[
3411+
"// Generated by @typespec/http-server-csharp",
3412+
"using System;",
3413+
"using System.Text.Json;",
3414+
"using System.Text.Json.Serialization;",
3415+
"using TypeSpec.Helpers.JsonConverters;",
3416+
"using TypeSpec.Helpers;",
3417+
],
3418+
],
3419+
["IContosoOperations.cs", ["Task<string[]> GetTagsAsync( )"]],
3420+
],
3421+
);
3422+
});
3423+
3424+
it("generates a dedicated file for array model with custom namespace", async () => {
3425+
await compileAndValidateMultiple(
3426+
tester,
3427+
[
3428+
`
3429+
model Items is Array<int32>;
3430+
@route("/items") @get op getItems(): Items;
3431+
`,
3432+
"My.Custom.Ns",
3433+
],
3434+
[
3435+
[
3436+
"Items.cs",
3437+
[
3438+
"// Generated by @typespec/http-server-csharp",
3439+
"using System;",
3440+
],
3441+
],
3442+
["INsOperations.cs", ["Task<int[]> GetItemsAsync( )"]],
3443+
],
3444+
);
3445+
});
3446+
3447+
it("generates a dedicated file for array model with complex element type", async () => {
3448+
await compileAndValidateMultiple(
3449+
tester,
3450+
`
3451+
model Widget {
3452+
id: int32;
3453+
name: string;
3454+
}
3455+
model WidgetList is Array<Widget>;
3456+
@route("/widgets") @get op getWidgets(): WidgetList;
3457+
`,
3458+
[
3459+
[
3460+
"Widget.cs",
3461+
[
3462+
"public partial class Widget",
3463+
"public int Id { get; set; }",
3464+
"public string Name { get; set; }",
3465+
],
3466+
],
3467+
[
3468+
"WidgetList.cs",
3469+
[
3470+
"// Generated by @typespec/http-server-csharp",
3471+
"using System;",
3472+
],
3473+
],
3474+
["IContosoOperations.cs", ["Task<Widget[]> GetWidgetsAsync( )"]],
3475+
],
3476+
);
3477+
});
3478+
3479+
it("generates a dedicated file for named array with union element type", async () => {
3480+
await compileAndValidateMultiple(
3481+
tester,
3482+
`
3483+
model ToolCall {
3484+
id: string;
3485+
type: "function";
3486+
name: string;
3487+
}
3488+
3489+
model CustomToolCall {
3490+
id: string;
3491+
type: "custom";
3492+
payload: string;
3493+
}
3494+
3495+
/** The tool calls generated by the model, such as function calls. */
3496+
model ToolCalls is (ToolCall | CustomToolCall)[];
3497+
3498+
model AssistantMessage {
3499+
role: "assistant";
3500+
tool_calls?: ToolCalls;
3501+
}
3502+
3503+
@route("/chat") op chat(): AssistantMessage;
3504+
`,
3505+
[
3506+
[
3507+
"AssistantMessage.cs",
3508+
[
3509+
`public string Role { get; } = "assistant";`,
3510+
],
3511+
],
3512+
[
3513+
"ToolCalls.cs",
3514+
[
3515+
"// Generated by @typespec/http-server-csharp",
3516+
],
3517+
],
3518+
[
3519+
"IContosoOperations.cs",
3520+
[
3521+
"Task<AssistantMessage> ChatAsync( );",
3522+
],
3523+
],
3524+
],
3525+
);
3526+
});
3527+
});
3528+
33993529
it("emits class for model extending another model with no additional properties", async () => {
34003530
await compileAndValidateMultiple(
34013531
tester,

0 commit comments

Comments
 (0)