Skip to content

fix(http-client-csharp): support int64 enum/union base types#10648

Open
haiyuazhang wants to merge 1 commit into
microsoft:mainfrom
haiyuazhang:fix/csharp-enum-int64
Open

fix(http-client-csharp): support int64 enum/union base types#10648
haiyuazhang wants to merge 1 commit into
microsoft:mainfrom
haiyuazhang:fix/csharp-enum-int64

Conversation

@haiyuazhang
Copy link
Copy Markdown
Member

Fixes Azure/azure-sdk-for-net#59168.

Problem

The C# generator crashes during code generation when a TypeSpec union/enum declares its base type as int64 (or any primitive other than string/int32/float32). The failure surfaces as a TypeInitializationException on ModelReaderWriterContextDefinition with the inner JsonException swallowed, making it hard to diagnose.

Root cause: InputEnumTypeValueConverter.CreateEnumTypeValue only handles String, Int32, and Float32 in its switch. Anything else falls into a bare throw new JsonException() with no message.

Fix

  1. Add an InputPrimitiveTypeKind.Int64 arm using rawValue.Value.GetInt64().
  2. Widen InputEnumTypeIntegerValue.IntegerValue (and its ctor parameter) from int to long so it can hold both Int32 and Int64 values. The class is internal and downstream EnumProvider.IsIntValueType already accepts typeof(long), so no further changes are required.
  3. Improve the default-arm JsonException message to include the offending kind, enum name, and value name so future unsupported kinds are diagnosable from build output.

Other kinds (Int16/Int8/Float64/Decimal/Decimal128/etc.) are intentionally out of scope for this change — int64 is the kind blocking real specs today (see Azure/azure-rest-api-specs#43069). The improved error message makes any remaining unsupported kinds easy to identify.

Extend InputEnumTypeValueConverter to handle InputPrimitiveTypeKind.Int64
by reading values via GetInt64() and widening InputEnumTypeIntegerValue's
IntegerValue from int to long. Also include the offending kind, enum,
and value names in the default-arm JsonException message so unsupported
kinds are diagnosable from build output.

Fixes Azure/azure-sdk-for-net#59168.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label May 12, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 12, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10648

commit: 4b9be83

@github-actions
Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Copy link
Copy Markdown
Contributor

@jorgerangel-msft jorgerangel-msft left a comment

Choose a reason for hiding this comment

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

Can we please add unit tests + trigger a regen preview ?

internal class InputEnumTypeIntegerValue : InputEnumTypeValue
{
public InputEnumTypeIntegerValue(string name, int integerValue, InputPrimitiveType valueType, string? summary, string? doc, InputEnumType? enumType = default)
public InputEnumTypeIntegerValue(string name, long integerValue, InputPrimitiveType valueType, string? summary, string? doc, InputEnumType? enumType = default)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why did we change this to long if it's an int value?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like we are using the same type for both.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Which is causing downstream issues in the tests. Can we also add explicit test cases for long?

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

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Mgmt][CodeGen] http-client-csharp InputEnumTypeValueConverter crashes on int64 (and other non-{string,int32,float32}) enum/union base types

3 participants