microsoft / microsoft/OpenAPI.NET.OData

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

Ouverte Adaptée aux débutants
#661 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
C#
Étoiles
240
Forks
70
Merge moyen
7 h 59 min
PR mergées (30 j)
13

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez dans src/Microsoft.OpenApi.OData.Reader/Edm/ODataPathProvider.cs autour de AppendBoundOperationOnNavigationSourcePath(), à la ligne 950, et reproduisez l’échec avec l’opération Count() décrite dans l’issue. Suivez la manière dont AppendPath modifie IList, puis vérifiez que ConvertToOpenApi crée le document sans l’exception de modification de collection, tout en contrôlant l’effet sur les chemins $count et Count().

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
csharp
Domaine
api
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
68/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.