Skip to content

Commit a1645cb

Browse files
committed
Address feedback
1 parent 794f1ee commit a1645cb

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/OpenApi/src/Services/OpenApiDocumentService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,5 +747,8 @@ private static Type GetTargetType(ApiDescription description, ApiParameterDescri
747747

748748
/// <inheritdoc />
749749
public Task<OpenApiDocument> GetOpenApiDocumentAsync(CancellationToken cancellationToken = default)
750-
=> GetOpenApiDocumentAsync(serviceProvider, null, cancellationToken);
750+
{
751+
cancellationToken.ThrowIfCancellationRequested();
752+
return GetOpenApiDocumentAsync(serviceProvider, httpRequest: null, cancellationToken);
753+
}
751754
}

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Extensions/OpenApiServiceCollectionExtensionsTests.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.OpenApi;
5-
using Microsoft.AspNetCore.Routing;
65
using Microsoft.Extensions.ApiDescriptions;
76
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.Extensions.DependencyInjection.Extensions;
97
using Microsoft.Extensions.Hosting;
108
using Microsoft.Extensions.Hosting.Internal;
119
using Microsoft.Extensions.Options;
@@ -275,4 +273,31 @@ public async Task GetOpenApiDocumentAsync_ReturnsDocument()
275273
Assert.Equal($"Test Application | {documentName.ToLowerInvariant()}", document.Info.Title);
276274
Assert.Equal("1.0.0", document.Info.Version);
277275
}
276+
277+
[Fact]
278+
public async Task GetOpenApiDocumentAsync_HandlesCancellation()
279+
{
280+
// Arrange
281+
var services = new ServiceCollection();
282+
services.AddSingleton<IHostEnvironment>(new HostingEnvironment
283+
{
284+
EnvironmentName = Environments.Development,
285+
ApplicationName = "Test Application"
286+
});
287+
services.AddLogging();
288+
services.AddRouting();
289+
var documentName = "v1";
290+
services.AddOpenApi(documentName);
291+
var serviceProvider = services.BuildServiceProvider();
292+
var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(documentName.ToLowerInvariant());
293+
294+
using var cts = new CancellationTokenSource();
295+
cts.Cancel();
296+
297+
// Act & Assert
298+
await Assert.ThrowsAsync<OperationCanceledException>(async () =>
299+
{
300+
await documentProvider.GetOpenApiDocumentAsync(cts.Token);
301+
});
302+
}
278303
}

0 commit comments

Comments
 (0)