[Bug]: Nullable implicit operator on string extensible enums still throws for a null string
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 84/100
調査の方向性
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ExtensibleEnumProvider.cs の190~196行付近にある nullable implicit operator の出力箇所から始め、関連するテストを TestProjects/Local.Tests/ExtensibleEnumTests.cs で確認してください。null の文字列変数を StringExtensibleEnum? に変換し、null が返ることを確認するテストを追加してください。出力される operator の null 分岐には nullable enum 型を指定する必要があります。
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
#7966 still reproduces after #7967. Converting a null string to a nullable string extensible enum throws ArgumentNullException instead of returning null. The operator added in #7967 is emitted as:
public static implicit operator StringExtensibleEnum?(string value) => value == null ? null : new StringExtensibleEnum(value);
The conditional's natural type is StringExtensibleEnum, not StringExtensibleEnum?: the untyped null converts to it through the generated implicit operator StringExtensibleEnum(string). So the null branch calls that operator with null, and the constructor's Argument.AssertNotNull throws.
The tests added in #7967 pass because they only use the null literal (NullableExtensibleEnumMethod(null), StringExtensibleEnum? nullFoo = null;). C# converts a null literal to Nullable<T> directly, so neither test calls the new operator.
Every string extensible enum emitted since #7967 has this operator. Azure.ResourceManager.Search hand-wrote a corrected copy for one of its enums to get null handling to work (discussion).
Suggested fix: in ExtensibleEnumProvider.cs, give the null branch the nullable type, e.g. Null.CastTo(Type.WithNullable(true)), so the output becomes value == null ? (StringExtensibleEnum?)null : new StringExtensibleEnum(value).
Reproduction
StringExtensibleEnum below has the same constructor and implicit operators as the generated StringExtensibleEnum.cs, with Argument.AssertNotNull inlined so it runs standalone.
using System;
string value = null;
try
{
StringExtensibleEnum? e = value;
Console.WriteLine($"HasValue={e.HasValue}");
}
catch (ArgumentNullException ex)
{
Console.WriteLine($"{ex.GetType().Name}: {ex.Message}");
}
public readonly struct StringExtensibleEnum
{
private readonly string _value;
public StringExtensibleEnum(string value)
{
_value = value ?? throw new ArgumentNullException(nameof(value));
}
public static implicit operator StringExtensibleEnum(string value) => new StringExtensibleEnum(value);
public static implicit operator StringExtensibleEnum?(string value) => value == null ? null : new StringExtensibleEnum(value);
}
Output on .NET 8:
ArgumentNullException: Value cannot be null. (Parameter 'value')
With the null branch cast to (StringExtensibleEnum?)null, it prints HasValue=False.
A test in TestProjects/Local.Tests/ExtensibleEnumTests.cs that exercises the operator:
[Test]
public void ConvertingNullStringToNullableExtensibleEnumDoesNotThrow()
{
string? value = null;
StringExtensibleEnum? e = value;
Assert.IsNull(e);
}
Checklist
- Follow our Code of Conduct
- Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- The provided reproduction is a minimal reproducible example of the bug.
🤖 m-nash-copilot
- 主要言語
- Java
- スター
- 5.9k
- フォーク
- 421
- 平均マージ
- 2日 3時間
- マージ済み PR(30日)
- 176
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/typespec のほかの issue
-
Make website build cross-platform compatible対応中かも @pcbeingused333 が今日担当しました。 オープンbug meta:website
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
microsoft/typespec#12550 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Emitter framework: make the language-specific Alloy peer dependencies optional対応中かも @timotheeguerin が 8 日前に担当しました。 オープンemitter-framework feature triaged:core
難易度 1/5 1時間未満 初心者へのやさしさ 62/100
microsoft/typespec#12476 · コメント 1 件 · リアクション 2 件 · 担当者 2 名 ·
メンテナーはふだん 1 日以内に返信
-
Upgrade C# generator Roslyn to 5.9.0対応中かも @ArthurMa1978 が 7 日前に担当しました。 オープンemitter:client:csharp feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
microsoft/typespec#12445 · コメント 2 件 · リアクション 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
bug emitter:client:python
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
microsoft/typespec#12360 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: http-client-js reports `unknown-encoding` for a raw `bytes` body対応中かも @Roxen-AS が 21 日前に担当しました。 オープンbug emitter:client:js
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
microsoft/typespec#12356 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
microsoft/typespec の issue をすべて見る
似ている issue
-
GCC 15.2: -Wcast-qual warning - cast discards const qualifier from pointer target type (H5trace.c)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
メンテナーはふだん 1 日以内に返信
-
[Bug] RemotingHelper.connect swaps the socket send and receive buffer configuration対応中かも @amalsaidov が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
needs-area-label
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
area/ai area/backend component/zeebe kind/task
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
spAnser/reacharound#38 ·