[Bug]: Nullable implicit operator on string extensible enums still throws for a null string

Đang mở Phù hợp với người mới
#12,572 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend, tooling

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ả

bug emitter:client:csharp
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

🤖 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

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/typespec

Tất cả issue của microsoft/typespec

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.