Skip to content

fix(http-server-csharp): numeric literal properties in @error models emit correct constructor parameter type#10716

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-cs1763-invalid-constructor
Draft

fix(http-server-csharp): numeric literal properties in @error models emit correct constructor parameter type#10716
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-cs1763-invalid-constructor

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 18, 2026

@error models with numeric literal–typed properties (e.g. status: 404) generated object status = 404 in the constructor signature, which is illegal in C# (CS1763: a reference-type default may only be null). The property itself was correctly emitted as int, making the intra-class typing inconsistent.

Root cause

getExceptionConstructorData resolved constructor parameter types via getCSharpType(program, prop.type). For a Number literal, that function returns the abstract numeric scalar mapping — object — rather than the concrete numeric type.

Fix

  • service.ts: When prop.type.kind === "Number", use findNumericType(prop.type) (already used by getTypeInfo for property declarations) to get the correct C# type (int or double). All other type kinds continue through getCSharpType unchanged.

Before / after

@error
model NotFoundProblem {
  title: string;
  status: 404;
}
// Before — CS1763, does not compile
public NotFoundProblem(string title, object status = 404) : base(400, ...)

// After
public NotFoundProblem(string title, int status = 404) : base(400, ...)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/apt/methods/https /usr/lib/apt/methods/https (dns block)
  • telemetry.astro.build
    • Triggering command: /usr/bin/node node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/bin/astro.mjs build sh k/ty�� ebsite/src/conte--llmstxt ture n-schema/node_mo../../website/src/content/docs/docs/emitters/openapi3/reference . ce n/node sh n-sc�� tspd --enable-experimental gen-extern-signature . .json .2_tmp_4732_0/node_modules/pnpm/dist/node-gyp-bin/node --no-emit iptables n-schema/referendoc node (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

…teral 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>
Copilot AI changed the title [WIP] Fix CS1763 invalid constructor for @error model fix(http-server-csharp): numeric literal properties in @error models emit correct constructor parameter type May 18, 2026
Copilot AI requested a review from markcowl May 18, 2026 18:35
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [http-server-csharp] @error model with a numeric-literal property emits a CS1763-invalid constructor (object x = N default)

3 participants