Skip to content

Commit 57f01ee

Browse files
authored
Merge pull request #137 from damienbod/damienbod/ignore-properties-configuration
Damienbod/ignore properties configuration
2 parents 7c12cb4 + 472818b commit 57f01ee

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed

samples/WebApiContrib.Core.Samples/Model/LocalizationRecord.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using Newtonsoft.Json;
2+
using System.ComponentModel.DataAnnotations;
23

34
namespace WebApiContrib.Core.Samples.Model
45
{
56
public class LocalizationRecord
67
{
8+
//[JsonIgnore]
79
public long Id { get; set; }
810
public string Key { get; set; }
911
public string Text { get; set; }

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.AspNetCore.Mvc.Formatters;
88
using Microsoft.Net.Http.Headers;
99
using System.Text;
10+
using Newtonsoft.Json;
11+
using System.Linq;
1012

1113
namespace WebApiContrib.Core.Formatter.Csv
1214
{
@@ -17,6 +19,8 @@ public class CsvInputFormatter : InputFormatter
1719
{
1820
private readonly CsvFormatterOptions _options;
1921

22+
private readonly bool useJsonAttributes = true;
23+
2024
public CsvInputFormatter(CsvFormatterOptions csvFormatterOptions)
2125
{
2226
SupportedMediaTypes.Add(Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/csv"));
@@ -99,8 +103,11 @@ private object ReadStream(Type type, Stream stream)
99103
{
100104
var itemTypeInGeneric = list.GetType().GetTypeInfo().GenericTypeArguments[0];
101105
var item = Activator.CreateInstance(itemTypeInGeneric);
102-
var properties = item.GetType().GetProperties();
103-
for (int i = 0;i<values.Length; i++)
106+
var properties = useJsonAttributes
107+
? item.GetType().GetProperties().Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>().Any()).ToArray()
108+
: item.GetType().GetProperties();
109+
110+
for (int i = 0; i < values.Length; i++)
104111
{
105112
properties[i].SetValue(item, Convert.ChangeType(values[i], properties[i].PropertyType), null);
106113
}

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

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

1213
namespace WebApiContrib.Core.Formatter.Csv
1314
{
@@ -20,6 +21,8 @@ public class CsvOutputFormatter : OutputFormatter
2021
{
2122
private readonly CsvFormatterOptions _options;
2223

24+
private readonly bool useJsonAttributes = true;
25+
2326
public string ContentType { get; private set; }
2427

2528
public CsvOutputFormatter(CsvFormatterOptions csvFormatterOptions)
@@ -50,6 +53,21 @@ private bool IsTypeOfIEnumerable(Type type)
5053

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

5472
public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
5573
{
@@ -71,22 +89,29 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
7189

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

100+
81101
foreach (var obj in (IEnumerable<object>)context.Object)
82102
{
83-
84-
var vals = obj.GetType().GetProperties().Select(
85-
pi => new
86-
{
87-
Value = pi.GetValue(obj, null)
88-
}
89-
);
103+
var vals = useJsonAttributes
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+
});
90115

91116
string valueLine = string.Empty;
92117

src/WebApiContrib.Core.Formatter.Csv/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ WebApiContrib.Core.Formatter.Csv [![NuGet Status](http://img.shields.io/nuget/v/
44

55
# History
66

7+
2018.05.31: Adding support for ignoring propeties in the CSV DTO
78
2018.04.18: Adding support for customization of the header with the display attribute
89
2018.04.12: Using the encoding from the options in the CsvOutputFormatter, Don't buffer CSV
910
2017.02.14: update to csproj
@@ -26,14 +27,17 @@ namespace WebApiContrib.Core.Samples.Model
2627
{
2728
public class LocalizationRecord
2829
{
29-
public long Id { get; set; }
30+
[JsonIgnore]
31+
public long? Id { get; set; }
32+
33+
[JsonProperty(PropertyName = "CustomKeyName")]
3034
public string Key { get; set; }
35+
3136
public string Text { get; set; }
37+
3238
public string LocalizationCulture { get; set; }
39+
3340
public string ResourceKey { get; set; }
34-
35-
[Display(Name = "Value")]
36-
public string ResourceValue { get; set; }
3741
}
3842
}
3943

src/WebApiContrib.Core.Formatter.Csv/WebApiContrib.Core.Formatter.Csv.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
<AssemblyName>WebApiContrib.Core.Formatter.Csv</AssemblyName>
99
<PackageId>WebApiContrib.Core.Formatter.Csv</PackageId>
1010
<PackageTags>ASP.NET Core;InputFormatter;OutputFormatter;MediaFormatter;CSV;Content-Type;aspnetcore</PackageTags>
11-
<PackageReleaseNotes>Adding support for customization of the header with the display attribute</PackageReleaseNotes>
11+
<PackageReleaseNotes>Adding support for ignoring properties</PackageReleaseNotes>
1212
<PackageIconUrl>https://avatars1.githubusercontent.com/u/1561645?s=140</PackageIconUrl>
1313
<PackageProjectUrl>https://github.com/damienbod/WebAPIContrib.Core/tree/master/src/WebApiContrib.Core.Formatter.Csv</PackageProjectUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<RepositoryUrl>https://github.com/WebApiContrib/WebAPIContrib.Core.git</RepositoryUrl>
1616
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1717
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1818
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
19-
<Version>2.0.4</Version>
20-
<AssemblyVersion>2.0.4</AssemblyVersion>
21-
<FileVersion>2.0.4</FileVersion>
19+
<Version>2.0.5</Version>
20+
<AssemblyVersion>2.0.5</AssemblyVersion>
21+
<FileVersion>2.0.5</FileVersion>
2222
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2323
</PropertyGroup>
2424

0 commit comments

Comments
 (0)