Skip to content

Commit 079cf14

Browse files
committed
Add ChatStats examples and DI support for response examples
- Add ChatStats class and ChatStatsExamplesProvider for response examples - Implement GetChatStatsAsync in ChatHub and IChatHub - Enhance SignalROpenApiDocumentGenerator to support DI scopes for example providers - Add tests verifying response examples in OpenAPI JSON and Swagger UI - Ensure DI-scoped example providers are resolved in tests - Register ExampleHub in test server for integration tests
1 parent 7e8f02f commit 079cf14

7 files changed

Lines changed: 415 additions & 96 deletions

File tree

samples/SignalR.OpenApi.Sample/Hubs/ChatHub.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,16 @@ public async IAsyncEnumerable<int> Countdown(
7676
await Task.Delay(1000, cancellationToken);
7777
}
7878
}
79+
80+
/// <inheritdoc />
81+
[SignalROpenApiResponseExamples(typeof(ChatStatsExamplesProvider))]
82+
public Task<ChatStats> GetChatStatsAsync(string roomName)
83+
{
84+
return Task.FromResult(new ChatStats
85+
{
86+
ActiveUsers = 15,
87+
TotalMessages = 342,
88+
RoomName = roomName,
89+
});
90+
}
7991
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) SignalR.OpenApi Contributors. Licensed under the MIT License.
2+
3+
namespace SignalR.OpenApi.Sample.Hubs;
4+
5+
/// <summary>
6+
/// Represents chat room statistics returned by the hub.
7+
/// </summary>
8+
public class ChatStats
9+
{
10+
/// <summary>
11+
/// Gets or sets the number of currently active users.
12+
/// </summary>
13+
public int ActiveUsers { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the total number of messages sent.
17+
/// </summary>
18+
public int TotalMessages { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the name of the chat room.
22+
/// </summary>
23+
public string RoomName { get; set; } = string.Empty;
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) SignalR.OpenApi Contributors. Licensed under the MIT License.
2+
3+
using SignalR.OpenApi.Examples;
4+
5+
namespace SignalR.OpenApi.Sample.Hubs;
6+
7+
/// <summary>
8+
/// Provides response examples for the GetChatStats method.
9+
/// </summary>
10+
public class ChatStatsExamplesProvider : ISignalROpenApiExamplesProvider<ChatStats>
11+
{
12+
/// <inheritdoc/>
13+
public IEnumerable<SignalROpenApiExample<ChatStats>> GetExamples()
14+
{
15+
yield return new SignalROpenApiExample<ChatStats>(
16+
"Busy",
17+
new ChatStats { ActiveUsers = 42, TotalMessages = 1583, RoomName = "General" })
18+
{
19+
Summary = "A busy chat room",
20+
};
21+
22+
yield return new SignalROpenApiExample<ChatStats>(
23+
"Quiet",
24+
new ChatStats { ActiveUsers = 3, TotalMessages = 27, RoomName = "Watercooler" })
25+
{
26+
Summary = "A quiet chat room",
27+
};
28+
}
29+
}

samples/SignalR.OpenApi.Sample/Hubs/IChatHub.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ public interface IChatHub
7979
/// <returns>A stream of countdown numbers.</returns>
8080
/// <example>10.</example>
8181
IAsyncEnumerable<int> Countdown(int from, CancellationToken cancellationToken);
82+
83+
/// <summary>
84+
/// Gets the current chat room statistics.
85+
/// </summary>
86+
/// <param name="roomName">The name of the chat room.</param>
87+
/// <returns>The chat statistics for the specified room.</returns>
88+
Task<ChatStats> GetChatStatsAsync(string roomName);
8289
}

0 commit comments

Comments
 (0)