IEdmModel.ConvertToOpenApi() throws System.InvalidOperationException when having dollar-count similar path in controller
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Start in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs around AppendBoundOperationOnNavigationSourcePath() at line 950, and reproduce the failure with the Count() operation described in the issue. Trace how AppendPath modifies the IList, then verify that ConvertToOpenApi creates the document without the collection-modification exception while checking the effect on $count and Count() paths.
Written by the indexing model from the issue text.
Description
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 ;)
- Dominant language
- C#
- Stars
- 240
- Forks
- 70
- Avg merge
- 7h 59m
- Merged PRs (30d)
- 13
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/OpenAPI.NET.OData
-
Difficulty 3/5 1-2 days Newbie friendliness 58/100
microsoft/OpenAPI.NET.OData#852 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
microsoft/OpenAPI.NET.OData#833 ·
-
status:needs-more-information
Difficulty 4/5 3-5 days Newbie friendliness 42/100
microsoft/OpenAPI.NET.OData#723 · 1 comment · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
microsoft/OpenAPI.NET.OData#720 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
microsoft/OpenAPI.NET.OData#710 · 2 comments · 1 reaction ·
All issues in microsoft/OpenAPI.NET.OData
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·