|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using Microsoft.AspNetCore.OpenApi; |
| 5 | +using Microsoft.AspNetCore.Routing; |
5 | 6 | using Microsoft.Extensions.ApiDescriptions; |
6 | 7 | using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 9 | +using Microsoft.Extensions.Hosting; |
| 10 | +using Microsoft.Extensions.Hosting.Internal; |
7 | 11 | using Microsoft.Extensions.Options; |
8 | 12 | using Microsoft.OpenApi; |
| 13 | +using Microsoft.OpenApi.Models; |
9 | 14 |
|
10 | 15 | public class OpenApiServiceCollectionExtensions |
11 | 16 | { |
@@ -189,4 +194,85 @@ public void AddOpenApi_WithDuplicateDocumentNames_UsesLastRegistration_ValidateO |
189 | 194 | Assert.Equal(documentName, namedOption.DocumentName); |
190 | 195 | Assert.Equal(OpenApiSpecVersion.OpenApi2_0, namedOption.OpenApiVersion); |
191 | 196 | } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public void AddOpenApi_WithDefaultDocumentName_RegistersIOpenApiDocumentProviderInterface() |
| 200 | + { |
| 201 | + // Arrange |
| 202 | + var services = new ServiceCollection(); |
| 203 | + // Include dependencies for OpenApiDocumentService |
| 204 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 205 | + { |
| 206 | + EnvironmentName = Environments.Development, |
| 207 | + ApplicationName = "Test Application" |
| 208 | + }); |
| 209 | + services.AddLogging(); |
| 210 | + services.AddRouting(); |
| 211 | + |
| 212 | + // Act |
| 213 | + services.AddOpenApi(); |
| 214 | + var serviceProvider = services.BuildServiceProvider(); |
| 215 | + |
| 216 | + // Assert |
| 217 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(Microsoft.AspNetCore.OpenApi.OpenApiConstants.DefaultDocumentName); |
| 218 | + Assert.NotNull(documentProvider); |
| 219 | + Assert.IsType<OpenApiDocumentService>(documentProvider); |
| 220 | + } |
| 221 | + |
| 222 | + [Fact] |
| 223 | + public void AddOpenApi_WithCustomDocumentName_RegistersIOpenApiDocumentProviderInterface() |
| 224 | + { |
| 225 | + // Arrange |
| 226 | + var services = new ServiceCollection(); |
| 227 | + // Include dependencies for OpenApiDocumentService |
| 228 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 229 | + { |
| 230 | + EnvironmentName = Environments.Development, |
| 231 | + ApplicationName = "Test Application" |
| 232 | + }); |
| 233 | + services.AddLogging(); |
| 234 | + services.AddRouting(); |
| 235 | + var documentName = "v1"; |
| 236 | + |
| 237 | + // Act |
| 238 | + services.AddOpenApi(documentName); |
| 239 | + var serviceProvider = services.BuildServiceProvider(); |
| 240 | + |
| 241 | + // Assert |
| 242 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(documentName.ToLowerInvariant()); |
| 243 | + Assert.NotNull(documentProvider); |
| 244 | + Assert.IsType<OpenApiDocumentService>(documentProvider); |
| 245 | + } |
| 246 | + |
| 247 | + [Fact] |
| 248 | + public async Task GetOpenApiDocumentAsync_ReturnsDocument() |
| 249 | + { |
| 250 | + // Arrange |
| 251 | + var services = new ServiceCollection(); |
| 252 | + // Include dependencies for OpenApiDocumentService |
| 253 | + services.AddSingleton<IHostEnvironment>(new HostingEnvironment |
| 254 | + { |
| 255 | + EnvironmentName = Environments.Development, |
| 256 | + ApplicationName = "Test Application" |
| 257 | + }); |
| 258 | + services.AddLogging(); |
| 259 | + services.AddRouting(); |
| 260 | + |
| 261 | + var documentName = "v1"; |
| 262 | + services.AddOpenApi(documentName); |
| 263 | + var serviceProvider = services.BuildServiceProvider(); |
| 264 | + var documentProvider = serviceProvider.GetRequiredKeyedService<IOpenApiDocumentProvider>(documentName.ToLowerInvariant()); |
| 265 | + |
| 266 | + // Act |
| 267 | + var document = await documentProvider.GetOpenApiDocumentAsync(); |
| 268 | + |
| 269 | + // Assert |
| 270 | + Assert.NotNull(document); |
| 271 | + Assert.IsType<OpenApiDocument>(document); |
| 272 | + |
| 273 | + // Verify basic document structure |
| 274 | + Assert.NotNull(document.Info); |
| 275 | + Assert.Equal($"Test Application | {documentName.ToLowerInvariant()}", document.Info.Title); |
| 276 | + Assert.Equal("1.0.0", document.Info.Version); |
| 277 | + } |
192 | 278 | } |
0 commit comments