Skip to content

Commit

Permalink
Merge branch 'Rob-Hague-fix-nongeneric-has-header'
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed Jan 26, 2024
2 parents 644a570 + bf460ac commit 3f5054a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
30 changes: 18 additions & 12 deletions src/CsvHelper/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0.
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0.
// https://github.com/JoshClose/CsvHelper
using CsvHelper.Configuration;
using CsvHelper.Expressions;
using CsvHelper.TypeConversion;
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Reflection;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
using System.Linq;
using System.Linq.Expressions;
using System.Dynamic;
using System.Threading.Tasks;
using CsvHelper.Expressions;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Buffers;
using System.Threading;
using System.Threading.Tasks;

#pragma warning disable 649
#pragma warning disable 169
Expand Down Expand Up @@ -795,6 +794,7 @@ private bool WriteHeader<T>(IEnumerable<T> records)
if (!isPrimitive && recordType != typeof(object))
{
WriteHeader(recordType);

return hasHeaderBeenWritten;
}

Expand All @@ -803,16 +803,20 @@ private bool WriteHeader<T>(IEnumerable<T> records)

private bool WriteHeader(IEnumerable records)
{
object record = null;
if (!hasHeaderRecord || hasHeaderBeenWritten)
{
return false;
}

foreach (var r in records)
{
if (r != null)
{
record = r;
return WriteHeader(r);
}
}

return WriteHeader(record);
return false;
}

private bool WriteHeader(object record)
Expand All @@ -825,6 +829,7 @@ private bool WriteHeader(object record)
if (record is IDynamicMetaObjectProvider dynamicObject)
{
WriteDynamicHeader(dynamicObject);

return true;
}

Expand All @@ -833,6 +838,7 @@ private bool WriteHeader(object record)
if (!isPrimitive)
{
WriteHeader(recordType);

return true;
}

Expand Down
45 changes: 38 additions & 7 deletions tests/CsvHelper.Tests/CsvWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0.
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0.
// https://github.com/JoshClose/CsvHelper
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Text;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
using Int32Converter = CsvHelper.TypeConversion.Int32Converter;
using System.Dynamic;
using Xunit;
using System.Threading;
using Xunit;
using Int32Converter = CsvHelper.TypeConversion.Int32Converter;

namespace CsvHelper.Tests
{

public class CsvWriterTests
{
public CsvWriterTests()
Expand Down Expand Up @@ -835,6 +835,37 @@ public void WriteInternalConstructorClassTest()
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void WriteRecords_NonGeneric_HasHeaderRecord(bool hasHeaderRecord)
{
var confg = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = hasHeaderRecord
};

using var stringWriter = new StringWriter();
using var csvWriter = new CsvWriter(stringWriter, confg);

IEnumerable records = new object[] {
new TestSinglePropertyRecord { Name = "test" },
new TestRecord { IntColumn = 4 }
};

csvWriter.WriteRecords(records);
var csv = stringWriter.ToString();

if (hasHeaderRecord)
{
Assert.Equal("Name\r\ntest\r\n4,,,,\r\n", csv);
}
else
{
Assert.Equal("test\r\n4,,,,\r\n", csv);
}
}

private class GetOnly
{
internal GetOnly(string someParam)
Expand Down

0 comments on commit 3f5054a

Please sign in to comment.