Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csv output formatter fails when Linq chain #198

Open
twsouthwick opened this issue Apr 2, 2020 · 1 comment
Open

Csv output formatter fails when Linq chain #198

twsouthwick opened this issue Apr 2, 2020 · 1 comment
Labels

Comments

@twsouthwick
Copy link

twsouthwick commented Apr 2, 2020

I am using v3.0.0 of the Csv output formatter. However, I saw the following issue:

[Route("a")]
[HttpGet]
[Produces("text/csv")]
public IEnumerable<A> GetA()
{
    var b = new[]
    {
        new B { Item2 = "Hello" },
    };
    var result = b.Select(i => new A { Item1 = i.Item2 });

    return result;

    // If I replace the return with the following it works as expected:
    // return result.ToList();
}

public class A
{
    public string Item1 { get; set; }
}

private class B
{
    public string Item2 { get; set; }
}

The result of this is:

Item2
Hello

when I would expect

Item1
Hello
@damienbod damienbod added the CSV label Apr 2, 2020
@lakeman
Copy link

lakeman commented Apr 11, 2022

This test is wrong.

Many linq operations return a class with multiple generic type arguments. Starting with the source type, not the result type.

Rather than .GetType(), you should locate the IEnumerable<T> interface and extract the generic type from there.

.GetType()
.GetInterfaces()
.Where(x =>
  x.IsGenericType &&
  x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.FirstOrDefault()
?.GetGenericArguments()[0];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants