forked from Azure/azure-functions-openapi-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyInfoExtensions.cs
More file actions
69 lines (57 loc) · 2.92 KB
/
Copy pathPropertyInfoExtensions.cs
File metadata and controls
69 lines (57 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions
{
/// <summary>
/// This represents the extension entity for <see cref="PropertyInfo"/> class.
/// </summary>
public static class PropertyInfoExtensions
{
/// <summary>
/// Checks whether the given <see cref="PropertyInfo"/> object has <see cref="JsonPropertyAttribute"/> or not.
/// </summary>
/// <param name="element"><see cref="PropertyInfo"/> instance.</param>
/// <returns>Returns <c>True</c>, if the given <see cref="PropertyInfo"/> object has <see cref="JsonPropertyAttribute"/>; otherwise, returns <c>False</c>.</returns>
public static bool HasJsonPropertyAttribute(this PropertyInfo element)
{
var exists = element.ExistsCustomAttribute<JsonPropertyAttribute>();
return exists;
}
/// <summary>
/// Checks whether the given <see cref="PropertyInfo"/> object has <see cref="DataMemberAttribute"/> or not.
/// </summary>
/// <param name="element"><see cref="PropertyInfo"/> instance.</param>
/// <returns>Returns <c>True</c>, if the given <see cref="PropertyInfo"/> object has <see cref="DataMemberAttribute"/>; otherwise, returns <c>False</c>.</returns>
public static bool HasDataMemberAttribute(this PropertyInfo element)
{
var exists = element.ExistsCustomAttribute<DataMemberAttribute>();
return exists;
}
/// <summary>
/// Gets the name from <see cref="JsonPropertyAttribute"/> or <see cref="DataMemberAttribute"/> instance.
/// </summary>
/// <param name="element"><see cref="PropertyInfo"/> instance.</param>
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance.</param>
/// <returns>Returns the name from <see cref="JsonPropertyAttribute"/> or <see cref="DataMemberAttribute"/> instance.</returns>
public static string GetJsonPropertyName(this PropertyInfo element, NamingStrategy namingStrategy = null)
{
if (namingStrategy.IsNullOrDefault())
{
namingStrategy = new DefaultNamingStrategy();
}
if (element.HasJsonPropertyAttribute())
{
var name = element.GetCustomAttribute<JsonPropertyAttribute>().PropertyName ?? namingStrategy.GetPropertyName(element.Name, hasSpecifiedName: false);
return name;
}
if (element.HasDataMemberAttribute())
{
var name = element.GetCustomAttribute<DataMemberAttribute>().Name ?? namingStrategy.GetPropertyName(element.Name, hasSpecifiedName: false);
return name;
}
return namingStrategy.GetPropertyName(element.Name, hasSpecifiedName: false);
}
}
}