Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SseItem's ctor eventType argument to be nullable #109765

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public readonly partial struct SseItem<T>
private readonly T _Data_k__BackingField;
private readonly object _dummy;
private readonly int _dummyPrimitive;
public SseItem(T data, string eventType) { throw null; }
public SseItem(T data, string? eventType) { throw null; }
public T Data { get { throw null; } }
public string EventType { get { throw null; } }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ namespace System.Net.ServerSentEvents
/// <typeparam name="T">Specifies the type of data payload in the event.</typeparam>
public readonly struct SseItem<T>
{
/// <summary>The event's type.</summary>
internal readonly string? _eventType;

/// <summary>Initializes the server-sent event.</summary>
/// <param name="data">The event's payload.</param>
/// <param name="eventType">The event's type.</param>
public SseItem(T data, string eventType)
public SseItem(T data, string? eventType)
{
Data = data;
EventType = eventType;
_eventType = eventType;
}

/// <summary>Gets the event's payload.</summary>
public T Data { get; }

/// <summary>Gets the event's type.</summary>
public string EventType { get; }
public string EventType => _eventType ?? SseParser.EventTypeDefault;
}
}
28 changes: 28 additions & 0 deletions src/libraries/System.Net.ServerSentEvents/tests/SseItemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.Net.ServerSentEvents.Tests
{
public partial class SseItemTests
{
[Fact]
public void SseItem_Roundtrips()
{
SseItem<string> item;

item = default;
Assert.Null(item.Data);
Assert.Equal(SseParser.EventTypeDefault, item.EventType);

item = new SseItem<string>("some data", null);
Assert.Equal("some data", item.Data);
Assert.Equal(SseParser.EventTypeDefault, item.EventType);

item = new SseItem<string>("some data", "eventType");
Assert.Equal("some data", item.Data);
Assert.Equal("eventType", item.EventType);
}
}
}
14 changes: 0 additions & 14 deletions src/libraries/System.Net.ServerSentEvents/tests/SseParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ public async Task Parse_Async_SupportsOnlyOneEnumeration_Throws()
Assert.Throws<InvalidOperationException>(() => e.MoveNext());
}

[Fact]
public void SseItem_Roundtrips()
{
SseItem<string> item;

item = new SseItem<string>();
Assert.Null(item.EventType);
Assert.Null(item.Data);

item = new SseItem<string>("some data", "eventType");
Assert.Equal("eventType", item.EventType);
Assert.Equal("some data", item.Data);
}

[Theory]
[MemberData(nameof(NewlineTrickleAsyncData))]
public async Task Parse_Empty_NoItems(string newline, bool trickle, bool useAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="SseItemTests.cs" />
<Compile Include="SseParserTests.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\System.Net.ServerSentEvents.csproj"/>
<ProjectReference Include="..\src\System.Net.ServerSentEvents.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\gen\System.Text.Json.SourceGeneration.Roslyn4.4.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>

Expand Down