[Bug]: Nullable implicit operator on string extensible enums still throws for a null string
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 84/100
Hướng nghiên cứu
Bắt đầu tại packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ExtensibleEnumProvider.cs, ở phần phát sinh toán tử implicit nullable quanh các dòng 190–196, sau đó đọc các bài kiểm thử liên quan trong TestProjects/Local.Tests/ExtensibleEnumTests.cs. Thêm bài kiểm thử chuyển một biến chuỗi null thành StringExtensibleEnum? và xác nhận kết quả trả về là null; nhánh null của toán tử được phát sinh nên có kiểu enum nullable.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Java
- Star
- 5.9k
- Fork
- 421
- Merge trung bình
- 2 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 176
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/typespec
-
Make website build cross-platform compatibleCó thể đã có người làm @pcbeingused333 đã nhận hôm nay. Đang mởbug meta:website
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
microsoft/typespec#12550 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Emitter framework: make the language-specific Alloy peer dependencies optionalCó thể đã có người làm @timotheeguerin đã nhận 8 ngày trước. Đang mởemitter-framework feature triaged:core
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
microsoft/typespec#12476 · 1 bình luận · 2 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Upgrade C# generator Roslyn to 5.9.0Có thể đã có người làm @ArthurMa1978 đã nhận 7 ngày trước. Đang mởemitter:client:csharp feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
microsoft/typespec#12445 · 2 bình luận · 1 reaction · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug emitter:client:python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
microsoft/typespec#12360 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: http-client-js reports `unknown-encoding` for a raw `bytes` bodyCó thể đã có người làm @Roxen-AS đã nhận 21 ngày trước. Đang mởbug emitter:client:js
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
microsoft/typespec#12356 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của microsoft/typespec
Issue tương tự
-
GCC 15.2: -Wcast-qual warning - cast discards const qualifier from pointer target type (H5trace.c)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] RemotingHelper.connect swaps the socket send and receive buffer configurationCó thể đã có người làm @amalsaidov đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-area-label
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
area/ai area/backend component/zeebe kind/task
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
spAnser/reacharound#38 ·