-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathMetadataProvider.cs
More file actions
292 lines (252 loc) · 12.6 KB
/
MetadataProvider.cs
File metadata and controls
292 lines (252 loc) · 12.6 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using DocsByReflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using WebApiProxy.Core.Models;
namespace WebApiProxy.Server
{
public class MetadataProvider
{
private readonly List<ModelDefinition> models;
private readonly List<string> typesToIgnore = new List<string>();
private readonly HttpConfiguration config;
public MetadataProvider(HttpConfiguration config)
{
this.models = new List<ModelDefinition>();
this.typesToIgnore = new List<string>();
this.config = config;
}
public Metadata GetMetadata(HttpRequestMessage request)
{
var host = request.RequestUri.Scheme + "://" + request.RequestUri.Authority + request.GetRequestContext().VirtualPathRoot;
var descriptions = config.Services.GetApiExplorer().ApiDescriptions;
var documentationProvider = config.Services.GetDocumentationProvider();
ILookup<HttpControllerDescriptor, ApiDescription> apiGroups = descriptions
.Where(a => !a.ActionDescriptor.ControllerDescriptor.ControllerType.IsAbstract
&& !a.RelativePath.Contains("Swagger")
&& !a.RelativePath.Contains("docs"))
.ToLookup(a => a.ActionDescriptor.ControllerDescriptor);
var metadata = new Metadata
{
Definitions = from d in apiGroups
where !d.Key.ControllerType.IsExcluded()
select new ControllerDefinition
{
Name = d.Key.ControllerName,
Description = documentationProvider == null ? "" : documentationProvider.GetDocumentation(d.Key) ?? "",
ActionMethods = from a in descriptions
where !a.ActionDescriptor.ControllerDescriptor.ControllerType.IsExcluded()
&& !a.ActionDescriptor.IsExcluded()
&& !a.RelativePath.Contains("Swagger")
&& !a.RelativePath.Contains("docs")
&& a.ActionDescriptor.ControllerDescriptor.ControllerName == d.Key.ControllerName
select new ActionMethodDefinition
{
Name = a.ActionDescriptor.ActionName,
BodyParameter = (from b in a.ParameterDescriptions
where b.Source == ApiParameterSource.FromBody
select new ParameterDefinition
{
Name = b.ParameterDescriptor.ParameterName,
Type = ParseType(b.ParameterDescriptor.ParameterType),
Description = b.Documentation ?? ""
}).FirstOrDefault(),
UrlParameters = from b in a.ParameterDescriptions.Where(p => p.ParameterDescriptor != null)
where b.Source == ApiParameterSource.FromUri
select new ParameterDefinition
{
Name = b.ParameterDescriptor.ParameterName,
Type = ParseType(b.ParameterDescriptor.ParameterType),
Description = b.Documentation ?? "",
IsOptional = b.ParameterDescriptor.IsOptional,
DefaultValue = b.ParameterDescriptor.DefaultValue
},
Url = a.RelativePath,
Description = a.Documentation ?? "",
ReturnType = ParseType(a.ResponseDescription.ResponseType ?? a.ResponseDescription.DeclaredType),
Type = a.HttpMethod.Method
}
},
Models = models,
Host = (null != host && host.Length > 0 && host[host.Length - 1] != '/') ? string.Concat(host, "/") : host
};
metadata.Definitions = metadata.Definitions.Distinct().OrderBy(d => d.Name);
metadata.Models = metadata.Models.Distinct(new ModelDefinitionEqualityComparer()).OrderBy(d => d.Name);
return metadata;
}
private string ParseType(Type type, ModelDefinition model = null)
{
string res;
if (type == null)
return "";
// If the type is a generic type format to correct class name.
if (type.IsGenericType)
{
res = GetGenericRepresentation(type, (t) => ParseType(t, model), model);
AddModelDefinition(type);
}
else
{
if (type.ToString().StartsWith("System."))
{
if (type.ToString().Equals("System.Void"))
res = "void";
else
res = type.Name;
}
else
{
res = type.Name;
if (!type.IsGenericParameter)
{
AddModelDefinition(type);
}
}
}
return res;
}
private string GetGenericRepresentation(Type type, Func<Type, string> getTypedParameterRepresentation, ModelDefinition model = null)
{
string res = type.Name;
int index = res.IndexOf('`');
if (index > -1)
res = res.Substring(0, index);
Type[] args = type.GetGenericArguments();
res += "<";
for (int i = 0; i < args.Length; i++)
{
if (i > 0)
res += ", ";
//Recursivly find nested arguments
var arg = args[i];
if (model != null && model.IsGenericArgument(arg.Name))
{
res += model.GetGenericArgument(arg.Name);
}
else
{
res += getTypedParameterRepresentation(arg);
}
}
res += ">";
return res;
}
private string GetGenericTypeDefineRepresentation(Type genericTypeDefClass)
{
string res = genericTypeDefClass.Name;
int index = res.IndexOf('`');
if (index > -1)
res = res.Substring(0, index);
Type[] args = genericTypeDefClass.GetGenericArguments();
res += "<";
for (int i = 0; i < args.Length; i++)
{
if (i > 0)
res += ", ";
var arg = args[i];
res += arg.Name;
}
res += ">";
return res;
}
private void AddModelDefinition(Type classToDef)
{
var documentationProvider = config.Services.GetDocumentationProvider();
//When the class is an array redefine the classToDef as the array type
if (classToDef.IsArray)
{
classToDef = classToDef.GetElementType();
}
// Is is not a .NET Framework generic, then add to the models collection.
if (classToDef.Namespace.StartsWith("System", StringComparison.OrdinalIgnoreCase))
{
AddTypeToIgnore(classToDef.Name);
return;
}
//If the class has not been mapped then map into metadata
if (!typesToIgnore.Contains(classToDef.Name))
{
ModelDefinition model = new ModelDefinition();
model.Name = classToDef.Name;
model.Description = GetDescription(classToDef);
if (classToDef.IsGenericType)
{
model.Name = GetGenericTypeDefineRepresentation(classToDef.GetGenericTypeDefinition());
}
model.Type = classToDef.IsEnum ? "enum" : "class";
var constants = classToDef
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(f => f.IsLiteral && !f.IsInitOnly)
.ToList();
model.Constants = from constant in constants
select new ConstantDefinition
{
Name = constant.Name,
Type = ParseType(constant.FieldType),
Value = GetConstantValue(constant),
Description = GetDescription(constant)
};
var properties = classToDef.IsGenericType
? classToDef.GetGenericTypeDefinition().GetProperties()
: classToDef.GetProperties();
// remove if properties or their types has got ExcludeProxy Attribute (or dummy name)
properties = properties.Where(x =>
!x.CustomAttributes.Any(z => z.AttributeType.Name.Contains("ExcludeProxy")) && // Property
!x.PropertyType.CustomAttributes.Any(y => y.AttributeType.Name.Contains("ExcludeProxy")) // Type
).ToArray(); // Clear and Nice :)
model.Properties = from property in properties
select new ModelProperty
{
Name = property.Name,
Type = ParseType(property.PropertyType, model),
Description = GetDescription(property)
};
AddTypeToIgnore(model.Name);
foreach (var p in properties)
{
var type = p.PropertyType;
if (!models.Any(c => c.Name.Equals(type.Name)))// && !type.IsInterface)
{
ParseType(type);
}
}
models.Add(model);
}
}
private void AddTypeToIgnore(string name)
{
if (!typesToIgnore.Contains(name))
{
typesToIgnore.Add(name);
}
}
private string GetConstantValue(FieldInfo constant)
{
var value = constant.GetRawConstantValue().ToString();
return value;
}
private static string GetDescription(MemberInfo member)
{
var xml = DocsService.GetXmlFromMember(member, false);
if (xml != null)
{
return xml.InnerText.Trim();
}
return String.Empty;
}
private static string GetDescription(Type type)
{
var xml = DocsService.GetXmlFromType(type, false);
if (xml != null)
{
return xml.InnerText.Trim();
}
return String.Empty;
}
}
}