Skip to content

Commit 080a702

Browse files
Copilotmarkcowl
andauthored
fix(http-server-csharp): fix CS1763 - use correct type for numeric literal properties in error model constructors
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/172760bb-1724-46d4-966b-e8e1ed0aaecc Co-authored-by: markcowl <1054056+markcowl@users.noreply.github.com>
1 parent 4609ff8 commit 080a702

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-server-csharp"
5+
---
6+
7+
Fix invalid constructor generated for `@error` models with numeric literal properties
8+
9+
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.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import {
9595
coalesceUnionTypes,
9696
ensureCSharpIdentifier,
9797
ensureCleanDirectory,
98+
findNumericType,
9899
formatComment,
99100
getBusinessLogicCallParameters,
100101
getBusinessLogicDeclParameters,
@@ -492,8 +493,12 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
492493
}
493494

494495
const type = getCSharpType(program, prop.type);
496+
const typeName =
497+
prop.type.kind === "Number"
498+
? findNumericType(prop.type)[0]
499+
: (type?.type.name ?? "object");
495500
properties.push(
496-
`${type?.type.name} ${prop.name}${defaultValue ? ` = ${defaultValue}` : `${prop.optional ? " = default" : ""}`}`,
501+
`${typeName} ${prop.name}${defaultValue ? ` = ${defaultValue}` : `${prop.optional ? " = default" : ""}`}`,
497502
);
498503
body.push(`\t\t${propertyName} = ${prop.name};`);
499504

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,43 @@ describe("emit correct code for `@error` models", () => {
32063206
],
32073207
);
32083208
});
3209+
it("emits correct constructor parameter type for numeric literal property", async () => {
3210+
await compileAndValidateSingleModel(
3211+
tester,
3212+
`
3213+
@error
3214+
model NotFoundProblem {
3215+
title: string;
3216+
status: 404;
3217+
}
3218+
`,
3219+
"NotFoundProblem.cs",
3220+
[
3221+
"public partial class NotFoundProblem : HttpServiceException {",
3222+
`public NotFoundProblem(string title, int status = 404) : base(400,`,
3223+
`value: new{title = title,status = status})`,
3224+
`public int Status { get; } = 404;`,
3225+
],
3226+
);
3227+
});
3228+
it("emits correct constructor parameter type for float literal property", async () => {
3229+
await compileAndValidateSingleModel(
3230+
tester,
3231+
`
3232+
@error
3233+
model RateLimitError {
3234+
message: string;
3235+
retryAfter: 1.5;
3236+
}
3237+
`,
3238+
"RateLimitError.cs",
3239+
[
3240+
"public partial class RateLimitError : HttpServiceException {",
3241+
`public RateLimitError(string message, double retryAfter = 1.5) : base(400,`,
3242+
`public double RetryAfter { get; } = 1.5;`,
3243+
],
3244+
);
3245+
});
32093246
it("emit error constructor properties and defined in body", async () => {
32103247
await compileAndValidateSingleModel(
32113248
tester,

0 commit comments

Comments
 (0)