microsoft / microsoft/OpenAPI.NET.OData
IEdmModel.ConvertToOpenApi() throws System.InvalidOperationException when having dollar-count similar path in controller
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C#
- Star
- 240
- Fork
- 70
- Merge trung bình
- 7 giờ 59 phút
- Pull request đã merge (30 ngày)
- 13
Mô tả
I have a Count() operation in an ODataController. When I call ConvertToOpenApi on the IEdmModel a System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' is thrown ( in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs).
Assemblies affected
occurs at least in:
- Microsoft.OpenApi.OData 1.7.4
- Microsoft.OpenApi.OData 2.0.0 preview 8
Steps to reproduce
In an ODataController, add a dollar-count-similar operation, eg:
[HttpGet]
[Description("Shows the total count of entities")]
public async Task<IActionResult> Count()
{
return await CountEntities();
}
Add the function to the ODataConventionModelBuilder, eg:
builder.EntityType<Entity>().Collection.Function(nameof(EntitiesController.Count)).Returns<int>();
Create the IEdmModel and call
var edmModel = odataBuilder.GetEdmModel();
var document = edmModel.ConvertToOpenApi();
Expected result
The OpenApiDocument gets created.
Actual result
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
Following the StackTrace the reason is:
AppendBoundOperationOnNavigationSourcePath() has a foreach on IList, which calls AppendPath(newPath);, which results in a modification of the paths, if a dollar-count-similar path is found, hence resulting in the InvalidOperationException.
Additional detail
Creating a copy of the collection before iterating over it in ODataPathProvider.cs L950 would work in my use-case, but keeps the $count and Count() operations in the openapi specification. Don't know if that is a problem.
foreach (var subPath in value.ToList())
...
As a workaround - not using dollar-similar-paths in your OData operations removes the issue as well ;)
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs, xung quanh AppendBoundOperationOnNavigationSourcePath() ở dòng 950, và tái hiện lỗi với thao tác Count() được mô tả trong issue. Theo dõi cách AppendPath sửa đổi IList, sau đó xác minh rằng ConvertToOpenApi tạo tài liệu mà không xảy ra ngoại lệ sửa đổi collection, đồng thời kiểm tra ảnh hưởng lên các đường dẫn $count và Count().
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- csharp
- Lĩnh vực
- api
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 68/100