Skip to content
Draft
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
9 changes: 9 additions & 0 deletions .chronus/changes/fix-cs1763-1779128907.md
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markcowl can we wait for the csharp alloy PR to merged?

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
changeKind: fix
packages:
- "@typespec/http-server-csharp"
---

Fix invalid constructor generated for `@error` models with numeric literal properties

When an `@error` model has a property with a numeric or floating-point literal type (e.g., `status: 404` or `retryAfter: 1.5`), the emitter now generates a correctly-typed constructor parameter (`int`/`double`) instead of `object`, which previously caused a CS1763 compiler error.
7 changes: 6 additions & 1 deletion packages/http-server-csharp/src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import {
coalesceUnionTypes,
ensureCSharpIdentifier,
ensureCleanDirectory,
findNumericType,
formatComment,
getBusinessLogicCallParameters,
getBusinessLogicDeclParameters,
Expand Down Expand Up @@ -492,8 +493,12 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
}

const type = getCSharpType(program, prop.type);
const typeName =
prop.type.kind === "Number"
? findNumericType(prop.type)[0]
: (type?.type.name ?? "object");
properties.push(
`${type?.type.name} ${prop.name}${defaultValue ? ` = ${defaultValue}` : `${prop.optional ? " = default" : ""}`}`,
`${typeName} ${prop.name}${defaultValue ? ` = ${defaultValue}` : `${prop.optional ? " = default" : ""}`}`,
);
body.push(`\t\t${propertyName} = ${prop.name};`);

Expand Down
37 changes: 37 additions & 0 deletions packages/http-server-csharp/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,43 @@ describe("emit correct code for `@error` models", () => {
],
);
});
it("emits correct constructor parameter type for numeric literal property", async () => {
await compileAndValidateSingleModel(
tester,
`
@error
model NotFoundProblem {
title: string;
status: 404;
}
`,
"NotFoundProblem.cs",
[
"public partial class NotFoundProblem : HttpServiceException {",
`public NotFoundProblem(string title, int status = 404) : base(400,`,
`value: new{title = title,status = status})`,
`public int Status { get; } = 404;`,
],
);
});
it("emits correct constructor parameter type for float literal property", async () => {
await compileAndValidateSingleModel(
tester,
`
@error
model RateLimitError {
message: string;
retryAfter: 1.5;
}
`,
"RateLimitError.cs",
[
"public partial class RateLimitError : HttpServiceException {",
`public RateLimitError(string message, double retryAfter = 1.5) : base(400,`,
`public double RetryAfter { get; } = 1.5;`,
],
);
});
it("emit error constructor properties and defined in body", async () => {
await compileAndValidateSingleModel(
tester,
Expand Down