Skip to content

Commit e3aade7

Browse files
committed
Add new tests for DocumentProvider which should also work case-insensitively
1 parent d28a74f commit e3aade7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentProviderTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,60 @@ public async Task GenerateAsync_ReturnsDocument()
3030
});
3131
}
3232

33+
[Fact]
34+
public async Task GenerateAsync_ShouldRetrieveOptionsInACaseInsensitiveManner()
35+
{
36+
// Arrange
37+
var documentName = "CaseSensitive";
38+
var serviceProvider = CreateServiceProvider(["casesensitive"]);
39+
var documentProvider = new OpenApiDocumentProvider(serviceProvider);
40+
var stringWriter = new StringWriter();
41+
42+
// Act
43+
await documentProvider.GenerateAsync(documentName, stringWriter);
44+
45+
// Assert
46+
var document = stringWriter.ToString();
47+
48+
// When we generate an OpenAPI document, we use an OptionsMonitor to retrieve OpenAPI options which are stored with a key equal the requested document name.
49+
// This key is case-sensitive. If the document doesn't exist, the options monitor return a default instance, in which the OpenAPI version is set to v3.
50+
// This could cause bugs! You'd get your document, but depending on the casing you used in the document name you passed to the function, you'll receive different OpenAPI document versions.
51+
// We want to prevent this from happening. Therefore:
52+
// By setting up a v2 document on the "casesensitive" route and requesting it on "CaseSensitive",
53+
// we can test that the we've configured the options monitor to retrieve the options in a case-insensitive manner.
54+
// If it is case-sensitive, it would return a default instance with OpenAPI version v3, which would cause this test to fail!
55+
// However, if it would return the v2 instance, which was configured on the lowercase - case-insensitive - documentname, the test would pass!
56+
Assert.StartsWith("{\n \"swagger\": \"2.0\"", document);
57+
}
58+
59+
[Fact]
60+
public async Task GenerateAsync_ShouldRetrieveOpenApiDocumentServiceWithACaseInsensitiveKey()
61+
{
62+
// Arrange
63+
var documentName = "CaseSensitive";
64+
var serviceProvider = CreateServiceProvider(["casesensitive"]);
65+
var documentProvider = new OpenApiDocumentProvider(serviceProvider);
66+
var stringWriter = new StringWriter();
67+
68+
// Act
69+
await documentProvider.GenerateAsync(documentName, stringWriter, OpenApiSpecVersion.OpenApi3_0);
70+
71+
// Assert
72+
73+
// If the Document Service is retrieved with a non-existent (case-sensitive) key, it would throw:
74+
// https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.serviceproviderkeyedserviceextensions.getrequiredkeyedservice?view=net-9.0-pp
75+
76+
// In this test's case, we're testing that the document service is retrieved with a case-insensitive key.
77+
// It's registered as "casesensitive" but we're passing in "CaseSensitive", which doesn't exist.
78+
// Therefore, if the test doesn't throw, we know it has passed correctly.
79+
// We still do a small check to validate the document, just in case. But the main test is that it doesn't throw.
80+
ValidateOpenApiDocument(stringWriter, document =>
81+
{
82+
Assert.Equal($"{nameof(OpenApiDocumentProviderTests)} | {documentName}", document.Info.Title);
83+
Assert.Equal("1.0.0", document.Info.Version);
84+
});
85+
}
86+
3387
[Fact]
3488
public void GetDocumentNames_ReturnsAllRegisteredDocumentName()
3589
{

0 commit comments

Comments
 (0)