microsoft / microsoft/OpenAPI.NET.OData

IEdmModel.ConvertToOpenApi() throws System.InvalidOperationException when having dollar-count similar path in controller

Offen Anfängerfreundlich
#661 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
C#
Sterne
240
Forks
70
Ø Merge
7 Std. 59 Min.
Gemergte PRs (30 T.)
13

Beschreibung

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 ;)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs bei AppendBoundOperationOnNavigationSourcePath() in der Nähe von Zeile 950 und reproduziere den Fehler mit der im Issue beschriebenen Count()-Operation. Verfolge, wie AppendPath die IList verändert, und überprüfe anschließend, dass ConvertToOpenApi das Dokument ohne die Ausnahme zur Änderung der Collection erstellt, während du die Auswirkungen auf die Pfade von $count und Count() überprüfst.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
api
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
68/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.