Skip to content

Commit

Permalink
Fix microsoft#4998 update handling of intrinsic void
Browse files Browse the repository at this point in the history
  • Loading branch information
markcowl committed Nov 19, 2024
1 parent 0b5909e commit 4e062b7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
5 changes: 4 additions & 1 deletion packages/http-server-csharp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
getCSharpIdentifier,
getCSharpStatusCode,
getCSharpType,
getCSharpTypeForIntrinsic,
getCSharpTypeForScalar,
getModelAttributes,
getModelInstantiationName,
Expand Down Expand Up @@ -200,13 +201,15 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
return this.emitter.result.rawCode(code`System.Text.Json.Nodes.JsonNode`);
case "ErrorType":
case "never":
case "void":
reportDiagnostic(this.emitter.getProgram(), {
code: "invalid-intrinsic",
target: intrinsic,
format: { typeName: intrinsic.name },
});
return "";
case "void":
const type = getCSharpTypeForIntrinsic(this.emitter.getProgram(), intrinsic);
return this.emitter.result.rawCode(`${type?.type.getTypeReference()}`);
}
}

Expand Down
52 changes: 28 additions & 24 deletions packages/http-server-csharp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
Scalar,
Type,
getFriendlyName,
isNullType,
isNumericType,
isTemplateInstance,
isUnknownType,
isVoidType,
} from "@typespec/compiler";
import { StringBuilder } from "@typespec/compiler/emitter-framework";
import {
Expand Down Expand Up @@ -196,31 +199,32 @@ export function getCSharpTypeForIntrinsic(
program: Program,
type: IntrinsicType,
): { type: CSharpType; value?: CSharpValue } | undefined {
switch (type.name) {
case "unknown":
return { type: UnknownType };
case "void":
return {
type: new CSharpType({
name: "void",
namespace: "System",
isBuiltIn: true,
isValueType: false,
}),
};
case "null":
return {
type: new CSharpType({
name: "object",
namespace: "System",
isBuiltIn: true,
isValueType: false,
}),
value: new NullValue(),
};
default:
return undefined;
if (isUnknownType(type)) {
return { type: UnknownType };
}
if (isVoidType(type)) {
return {
type: new CSharpType({
name: "void",
namespace: "System",
isBuiltIn: true,
isValueType: false,
}),
};
}
if (isNullType(type)) {
return {
type: new CSharpType({
name: "object",
namespace: "System",
isBuiltIn: true,
isValueType: false,
}),
value: new NullValue(),
};
}

return undefined;
}

type ExtendedIntrinsicScalarName = IntrinsicScalarName | "unixTimestamp32";
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 @@ -711,3 +711,38 @@ it("Handles user-defined model templates", async () => {
],
);
});

it("Handles void type in operations", 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;
}
@post @route("/foo") op foo(...Toy): void;
}
`,
[
["IMyServiceOperations.cs", ["interface IMyServiceOperations"]],
[
"MyServiceOperationsControllerBase.cs",
["public abstract partial class MyServiceOperationsControllerBase: ControllerBase"],
],
["Toy.cs", ["public partial class Toy"]],
],
);
});

0 comments on commit 4e062b7

Please sign in to comment.