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

Fix OpenApiFilterService.CreateFilteredDocument failing when the Components field is missing. #1734

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
6 changes: 5 additions & 1 deletion src/Microsoft.OpenApi/Services/OpenApiFilterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public static class OpenApiFilterService
public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Func<string, OperationType?, OpenApiOperation, bool> predicate)
{
// Fetch and copy title, graphVersion and server info from OpenApiDoc
var components = source.Components is null
? null
: new OpenApiComponents() { SecuritySchemes = source.Components.SecuritySchemes };

var subset = new OpenApiDocument
{
Info = new()
Expand All @@ -74,7 +78,7 @@ public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Fun
Extensions = source.Info.Extensions
},

Components = new() { SecuritySchemes = source.Components.SecuritySchemes },
Components = components,
SecurityRequirements = source.SecurityRequirements,
Servers = source.Servers
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ public async Task ReturnFilteredOpenApiDocBasedOnOperationIdsAndInputCsdlDocumen
Assert.Equal(expectedPathCount, subsetOpenApiDocument.Paths.Count);
}

[Fact]
public void CreateFilteredDocumentOnMinimalOpenApi()
{
// Arrange

// We create a minimal OpenApiDocument with a single path and operation.
var openApiDoc = new OpenApiDocument
{
Info = new()
{
Title = "Test",
Version = "1.0.0"
},
Paths = new()
{
["/test"] = new OpenApiPathItem()
{
Operations = new Dictionary<OperationType, OpenApiOperation>
{
[OperationType.Get] = new OpenApiOperation()
}
}
}
};

// Act
var requestUrls = new Dictionary<string, List<string>>()
{
{ "/test", ["GET"] }
};
var filterPredicate = OpenApiFilterService.CreatePredicate(null, null, requestUrls, openApiDoc);
var filteredDocument = OpenApiFilterService.CreateFilteredDocument(openApiDoc, filterPredicate);

// Assert
Assert.NotNull(filteredDocument);
Assert.NotNull(filteredDocument.Paths);
Assert.Single(filteredDocument.Paths);
}


[Theory]
[InlineData("UtilityFiles/appsettingstest.json")]
[InlineData(null)]
Expand Down
Loading