Skip to content

Commit

Permalink
Fix microsoft#5124 do not generate user-defined templates
Browse files Browse the repository at this point in the history
  • Loading branch information
markcowl committed Nov 21, 2024
1 parent ebd4251 commit 41bce5b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/http-server-csharp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isErrorModel,
isNeverType,
isNullType,
isTemplateDeclaration,
isVoidType,
} from "@typespec/compiler";
import {
Expand Down Expand Up @@ -1101,7 +1102,9 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
if (!service) service = getService(program, target);
if (service) {
for (const [_, model] of target.models) {
emitter.emitType(model);
if (!isTemplateDeclaration(model)) {
emitter.emitType(model);
}
}
for (const [_, en] of target.enums) {
emitter.emitType(en);
Expand All @@ -1110,14 +1113,18 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
emitter.emitType(sc);
}
for (const [_, iface] of target.interfaces) {
emitter.emitType(iface);
if (!isTemplateDeclaration(iface)) {
emitter.emitType(iface);
}
}
if (target.operations.size > 0) {
// Collect interface operations for a business logic interface and controller
const nsOps: [string, Operation][] = [];

for (const [_, op] of target.operations) {
nsOps.push([op.name, op]);
for (const [name, op] of target.operations) {
if (!isTemplateDeclaration(op)) {
nsOps.push([name, op]);
}
}
const iface: Interface = program.checker.createAndFinishType({
node: undefined as any,
Expand Down
35 changes: 35 additions & 0 deletions packages/http-server-csharp/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,38 @@ it("Generates types and controllers in a service subnamespace", async () => {
],
);
});

it("Handles user-defined model templates", async () => {
await compileAndValidateMultiple(
runner,
`
using TypeSpec.Rest.Resource;
namespace MyService {
model Toy {
@key("toyId")
id: int64;
petId: int64;
name: string;
}
@friendlyName("{name}ListResults", Item)
model ResponsePage<Item> {
items: Item[];
nextLink?: string;
}
op foo(): ResponsePage<Toy>;
}
`,
[
["IMyServiceOperations.cs", ["interface IMyServiceOperations"]],
[
"MyServiceOperationsControllerBase.cs",
["public abstract partial class MyServiceOperationsControllerBase: ControllerBase"],
],
["ToyListResults.cs", ["public partial class ToyListResults"]],
],
);
});

0 comments on commit 41bce5b

Please sign in to comment.