Skip to content

Commit 1ff1962

Browse files
Copilotmarkcowl
andauthored
[http-server-csharp] Emit C# class for models extending another model with no additional properties (#10175)
`isEmptyResponseModel` treats any model with zero own properties as empty, skipping emission entirely. This means `model Baz extends Foo {}` never produces a `Baz.cs` file, even though it's a meaningful derived type users may want to extend via partial classes. ```typespec model Foo { id: int32; name: string; } model Baz extends Foo {} // No Baz.cs emitted ``` ### Changes - **`packages/http-server-csharp/src/lib/utils.ts`**: `isEmptyResponseModel` now checks `!model.baseModel` before treating zero-property models as empty — derived models are always emitted. - **`packages/http-server-csharp/test/generation.test.ts`**: Added test case verifying a model extending another with no additional properties produces a class file with correct inheritance. <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/microsoft/typespec/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: markcowl <1054056+markcowl@users.noreply.github.com>
1 parent 0ee3f54 commit 1ff1962

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-server-csharp"
5+
---
6+
7+
Emit C# class for models that extend another model with no additional properties

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ export function getControllerReturnStatement(
10591059

10601060
export function isEmptyResponseModel(program: Program, model: Type): boolean {
10611061
if (model.kind !== "Model") return false;
1062-
if (model.properties.size === 0) return true;
1062+
if (model.properties.size === 0 && !model.baseModel) return true;
10631063

10641064
return (
10651065
model.properties.size === 1 &&

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,3 +3395,30 @@ describe("collection type: defined as emitter option", () => {
33953395
);
33963396
});
33973397
});
3398+
3399+
it("emits class for model extending another model with no additional properties", async () => {
3400+
await compileAndValidateMultiple(
3401+
tester,
3402+
`
3403+
model Foo {
3404+
id: int32;
3405+
name: string;
3406+
}
3407+
3408+
model Baz extends Foo {}
3409+
3410+
@route("/foo/{id}") @get op getFoo(id: int32): Foo;
3411+
`,
3412+
[
3413+
[
3414+
"Foo.cs",
3415+
[
3416+
"public partial class Foo",
3417+
"public int Id { get; set; }",
3418+
"public string Name { get; set; }",
3419+
],
3420+
],
3421+
["Baz.cs", ["public partial class Baz : Foo"]],
3422+
],
3423+
);
3424+
});

0 commit comments

Comments
 (0)