This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDefaultPropertyNameResolver.cs
87 lines (71 loc) · 3.14 KB
/
DefaultPropertyNameResolver.cs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Linq;
using System.Reflection;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.ReferenceRegistries;
namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration
{
/// <summary>
/// Used by <see cref="SchemaReferenceRegistry"/> to resolve property name for a given <see cref="PropertyInfo"/>.
/// </summary>
[Serializable]
public class DefaultPropertyNameResolver : IPropertyNameResolver
{
/// <summary>
/// Resolves the property name for the given property info.
/// </summary>
/// <param name="propertyInfo">The property info to resolve property name for.</param>
/// <returns>The property info.</returns>
public string ResolvePropertyName(PropertyInfo propertyInfo)
{
var propertyName = propertyInfo.Name;
var attributes = propertyInfo.GetCustomAttributes(false);
var jsonPropertyAttribute =
attributes?.Where(i => i.GetType().FullName == "Newtonsoft.Json.JsonPropertyAttribute")
.FirstOrDefault();
if (jsonPropertyAttribute == null)
{
return ResolvePropertyNameFromRuntimeSerialization(propertyInfo);
}
var type = jsonPropertyAttribute.GetType();
var propertyNameInfo = type.GetProperty("PropertyName");
if (propertyNameInfo == null)
{
return ResolvePropertyNameFromRuntimeSerialization(propertyInfo);
}
var jsonPropertyName = (string)propertyNameInfo.GetValue(jsonPropertyAttribute, null);
if (!string.IsNullOrWhiteSpace(jsonPropertyName))
{
propertyName = jsonPropertyName;
}
return propertyName;
}
private string ResolvePropertyNameFromRuntimeSerialization(PropertyInfo propertyInfo)
{
var propertyName = propertyInfo.Name;
var attributes = propertyInfo.GetCustomAttributes(false);
var runTimeSerializationAttribute =
attributes?.Where(i => i.GetType().FullName == "System.Runtime.Serialization.DataMemberAttribute")
.FirstOrDefault();
if (runTimeSerializationAttribute == null)
{
return propertyName;
}
var type = runTimeSerializationAttribute.GetType();
var propertyNameInfo = type.GetRuntimeProperty("Name");
if (propertyNameInfo == null)
{
return propertyName;
}
var runTimePropertyName = (string)propertyNameInfo.GetValue(runTimeSerializationAttribute, null);
if (!string.IsNullOrWhiteSpace(runTimePropertyName))
{
propertyName = runTimePropertyName;
}
return propertyName;
}
}
}