MapsterMapper / MapsterMapper/Mapster

Generic IEnumerator not disposed

Open
#667 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

improvement
Dominant language
C#
Stars
5.2k
Forks
410
Avg merge
2d 12h
Merged PRs (30d)
6

Description

I'm using Mapster.Tool to generate extension methods from my mapping registrations like so:

```csharp
config.NewConfig().GenerateMapper(MapType.Map | MapType.MapToTarget);
```

When inspecting the generated code I noticed that anywhere GetEnumerator() is used, the generic Enumerator is never disposed.

This is a snippet of what is being generated:

```csharp
...
IEnumerator enumerator = p2.GetEnumerator();

while (enumerator.MoveNext())
{
string item = enumerator.Current;
result.Add(item);
}

return result;
```

I believe what it should be generating is the following:

```csharp
using IEnumerator enumerator = p2.GetEnumerator();

while (enumerator.MoveNext())
{
string item = enumerator.Current;
result.Add(item);
}

return result;
```

I'm not sure if this issue arises in other areas (e.g. generated code from interfaces) as I haven't tested it personally, but I would expect the Mapster.Tool generates the code the same way.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Mapster.Tool and reproduce the generated extension method using the shown NewConfig().GenerateMapper(...) registration. Inspect the GetEnumerator() generation path, including generated code from interfaces if applicable; done means generated generic enumerators are disposed while iteration behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.