Browse Source

Fix DataTransferItem returning wrong data for mismatched format (#21114)

* fix: check format key in DataTransferItem.FindAccessor single-item path

The single-item fast path in FindAccessor returned the stored value for
any format query without checking if the requested format matched.
This caused TryGetRaw to return wrong data when queried with a format
different from the one stored (e.g., querying Bitmap on a text-only item
returned the text value instead of null).

Add the missing singleItem.Key.Equals(format) check, consistent with
the dictionary path (TryGetValue) and RemoveCore.

* chore: retrigger CI

* chore: retrigger CI

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
release/12.0.1
Nathan Nguyen 2 months ago
committed by Julien Lebosquain
parent
commit
7068d3ee70
  1. 2
      src/Avalonia.Base/Input/DataTransferItem.cs
  2. 10
      tests/Avalonia.Base.UnitTests/Input/DataFormatTests.cs

2
src/Avalonia.Base/Input/DataTransferItem.cs

@ -61,7 +61,7 @@ public sealed class DataTransferItem : IDataTransferItem, IAsyncDataTransferItem
if (_accessorByFormat is not null)
return _accessorByFormat.TryGetValue(format, out var accessor) ? accessor : null;
if (_singleItem is { } singleItem)
if (_singleItem is { } singleItem && singleItem.Key.Equals(format))
return singleItem.Value;
return null;

10
tests/Avalonia.Base.UnitTests/Input/DataFormatTests.cs

@ -79,6 +79,16 @@ public sealed class DataFormatTests
Assert.NotEqual<DataFormat>(inProcess, application);
}
[Fact]
public void TryGetRaw_With_Mismatched_Format_Returns_Null_For_Single_Format_Item()
{
var item = DataTransferItem.CreateText("hello");
var result = item.TryGetRaw(DataFormat.Bitmap);
Assert.Null(result);
}
[Fact]
public void InProcess_Format_Works_With_DataTransferItem_Set_And_Get()
{

Loading…
Cancel
Save