Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-server-csharp"
---

add arrayDeclarationContext
15 changes: 14 additions & 1 deletion packages/http-server-csharp/src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
return "TypeSpec.Service";
}

arrayDeclarationContext(array: Model, name: string, elementType: Type) {
const arrayName = ensureCSharpIdentifier(this.emitter.getProgram(), array, name);
const arrayFile = this.emitter.createSourceFile(`generated/models/${arrayName}.cs`);
arrayFile.meta[this.#sourceTypeKey] = CSharpSourceType.Model;
Comment thread
sophia-ramsey marked this conversation as resolved.
arrayFile.meta["skipEmit"] = true;
const arrayNamespace = this.#getOrAddNamespace(array.namespace);
return this.#createModelContext(arrayNamespace, arrayFile, arrayName);
}

arrayDeclaration(array: Model, name: string, elementType: Type): EmitterOutput<string> {
return this.collectionDeclaration(elementType, array);
}
Expand Down Expand Up @@ -1265,6 +1274,10 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
return sourceFile.meta.emitted;
}

if (sourceFile.meta["skipEmit"]) {
return { path: sourceFile.path, contents: "" };
}

const emittedSourceFile: EmittedSourceFile = {
path: sourceFile.path,
contents: "",
Expand Down Expand Up @@ -1426,7 +1439,7 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
break;
}
}
return super.writeOutput(emittedSourceFiles);
return super.writeOutput(emittedSourceFiles.filter((f) => !f.meta["skipEmit"]));
}
}

Expand Down
60 changes: 60 additions & 0 deletions packages/http-server-csharp/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ function assertFileDoesNotContain(
);
}

function assertFileNotEmitted(fs: TestFileSystem, fileName: string): void {
const result = [...fs.fs.entries()].filter((e) => e[0].includes(`/${fileName}`));
assert.strictEqual(result.length, 0, `Expected ${fileName} to not be emitted, but it was`);
}

async function compileAndValidateSingleModel(
tester: TesterInstance,
code: string,
Expand Down Expand Up @@ -3396,6 +3401,61 @@ describe("collection type: defined as emitter option", () => {
});
});

describe("arrayDeclarationContext", () => {
it("emits correct types for array model declarations", async () => {
const fs = await compileAndValidateMultiple(
tester,
`
model Tags is Array<string>;
@route("/tags") @get op getTags(): Tags;
`,
[["IContosoOperations.cs", ["Task<string[]> GetTagsAsync( )"]]],
);
assertFileNotEmitted(fs, "Tags.cs");
});

it("emits correct types for array model with custom namespace", async () => {
const fs = await compileAndValidateMultiple(
tester,
[
`
model Items is Array<int32>;
@route("/items") @get op getItems(): Items;
`,
"My.Custom.Ns",
],
[["INsOperations.cs", ["Task<int[]> GetItemsAsync( )"]]],
);
assertFileNotEmitted(fs, "Items.cs");
});

it("emits correct types for array model with complex element type", async () => {
const fs = await compileAndValidateMultiple(
tester,
`
model Widget {
id: int32;
name: string;
}
model WidgetList is Array<Widget>;
@route("/widgets") @get op getWidgets(): WidgetList;
`,
[
[
"Widget.cs",
[
"public partial class Widget",
"public int Id { get; set; }",
"public string Name { get; set; }",
],
],
["IContosoOperations.cs", ["Task<Widget[]> GetWidgetsAsync( )"]],
Comment thread
sophia-ramsey marked this conversation as resolved.
],
);
assertFileNotEmitted(fs, "WidgetList.cs");
});
});

it("emits class for model extending another model with no additional properties", async () => {
await compileAndValidateMultiple(
tester,
Expand Down
Loading