microsoft / microsoft/OpenAPI.NET.OData
IEdmModel.ConvertToOpenApi() throws System.InvalidOperationException when having dollar-count similar path in controller
Nessuno ha ancora preso questa issue.
- Lingua principale
- C#
- Stelle
- 240
- Fork
- 70
- Merge medio
- 7h 59m
- PR unite (30g)
- 13
Descrizione
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 ;)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs, intorno a AppendBoundOperationOnNavigationSourcePath() alla riga 950, e riproduci il fallimento con l'operazione Count() descritta nell'issue. Traccia il modo in cui AppendPath modifica IList, quindi verifica che ConvertToOpenApi crei il documento senza l'eccezione di modifica della collection, controllando al contempo l'effetto sui percorsi $count e Count().
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp
- Ambito
- api
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 68/100