Skip to content

Commit fb4d9c0

Browse files
committed
Merge branch 'master' into damienbod/ignore-properties-configuration
# Conflicts: # src/WebApiContrib.Core.Formatter.Csv/CsvOutputFormatter.cs
2 parents 924e1ae + 7c12cb4 commit fb4d9c0

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/WebApiContrib.Core.Formatter.Csv/CsvOutputFormatter.cs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Reflection;
99
using Microsoft.AspNetCore.Mvc.Formatters;
1010
using System.ComponentModel.DataAnnotations;
11-
using Newtonsoft.Json;
1211

1312
namespace WebApiContrib.Core.Formatter.Csv
1413
{
@@ -51,21 +50,6 @@ private bool IsTypeOfIEnumerable(Type type)
5150

5251
return false;
5352
}
54-
55-
/// <summary>
56-
/// Returns the JsonProperty data annotation name
57-
/// </summary>
58-
/// <param name="pi">Property Info</param>
59-
/// <returns></returns>
60-
private string GetDisplayNameFromNewtonsoftJsonAnnotations(PropertyInfo pi)
61-
{
62-
if (pi.GetCustomAttribute<JsonPropertyAttribute>(false)?.PropertyName is string value)
63-
{
64-
return value;
65-
}
66-
67-
return pi.GetCustomAttribute<DisplayAttribute>(false)?.Name ?? pi.Name;
68-
}
6953

7054
public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
7155
{
@@ -87,31 +71,22 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
8771

8872
if (_options.UseSingleLineHeaderInCsv)
8973
{
90-
var values = _options.UseJsonPropertyJsonIgnoreAttributes
91-
? itemType.GetProperties().Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>(false).Any()) // Only get the properties that do not define JsonIgnore
92-
.Select(GetDisplayNameFromNewtonsoftJsonAnnotations)
93-
: itemType.GetProperties().Select(pi => pi.GetCustomAttribute<DisplayAttribute>(false)?.Name ?? pi.Name);
94-
95-
await streamWriter.WriteLineAsync(string.Join(_options.CsvDelimiter, values));
74+
await streamWriter.WriteLineAsync(
75+
string.Join(
76+
_options.CsvDelimiter, itemType.GetProperties().Select(x => x.GetCustomAttribute<DisplayAttribute>(false)?.Name ?? x.Name)
77+
)
78+
);
9679
}
9780

98-
9981
foreach (var obj in (IEnumerable<object>)context.Object)
10082
{
10183

102-
//IEnumerable<ObjectValue> vals;
103-
var vals = _options.UseJsonPropertyJsonIgnoreAttributes
104-
? obj.GetType().GetProperties()
105-
.Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>().Any())
106-
.Select(pi => new
107-
{
108-
Value = pi.GetValue(obj, null)
109-
})
110-
: obj.GetType().GetProperties().Select(
111-
pi => new
112-
{
113-
Value = pi.GetValue(obj, null)
114-
});
84+
var vals = obj.GetType().GetProperties().Select(
85+
pi => new
86+
{
87+
Value = pi.GetValue(obj, null)
88+
}
89+
);
11590

11691
string valueLine = string.Empty;
11792

@@ -122,8 +97,11 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
12297

12398
var _val = val.Value.ToString();
12499

125-
//Check if the value contains a comma and place it in quotes if so
126-
if (_val.Contains(","))
100+
//Escape quotas
101+
_val = _val.Replace("\"", "\"\"");
102+
103+
//Check if the value contans a delimiter and place it in quotes if so
104+
if (_val.Contains(_options.CsvDelimiter))
127105
_val = string.Concat("\"", _val, "\"");
128106

129107
//Replace any \r or \n special characters from a new line with a space

0 commit comments

Comments
 (0)